Request Participants Script in JIRA 7.2.4 with Script Runner 4.3.13

Vivek Janjrukiya November 24, 2016

Hi Atlassian Team,

We tried with https://confluence.atlassian.com/jirakb/how-to-automatically-add-request-participants-when-creating-an-issue-777026955.html in JIRA 7.2.4 with Script Runner 4.3.13 with JIRA SD 3.2 and we are getting 

error on Script Console as:

[Static type checking] - Cannot find mathing method com.atlassian.jira.component.ComponentAccessor#getIssueIndexManager(). Please check it the declated type is right and it the method exists.

Please help, we need to add certain people into request participants once issue is created.

3 answers

1 vote
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 24, 2016

I refactor given code for scriptRunner. Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserUtil()
def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_10001")
def getUsersAsArray = {
    def users = ["ben", "steve"]
    ArrayList<ApplicationUser> userList = new ArrayList<ApplicationUser>()
    users.each {
        def user = userManager.getUserByName(it)
        if(user)
        { userList.add(user) }
    }
    return userList
}

MutableIssue myIssue = issue
ArrayList<ApplicationUser> applicationUsers = getUsersAsArray()
myIssue.setCustomFieldValue(requestParticipantsField, applicationUsers
Vivek Janjrukiya November 24, 2016

Hi,

This script is fine there are no errors on script console now, but getting a warning against line 13Err.jpg

also when script runs. it gives me below error.

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2016-11-25 09:06:15,407 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-11-25 09:06:15,407 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SS-672, actionId: 11, file: <inline script>
java.lang.NullPointerException

i just thought it might be because the "Request Participant" field is locked.

any clues ?

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 24, 2016

Have you changed custom field name here:

def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_10001")
Vivek Janjrukiya November 24, 2016

Yes, i have updated as 

def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_17393")

17393 is field id

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 25, 2016

Could you also provide full stack from atlassian-jira.log.

Vivek Janjrukiya November 28, 2016

Hi Vasiliy,

Thanks for the solution, i am very much new to JIRA Service Desk and i don't know how i can take this full stack log. but this has been resolved now. i was using wrong custom field ID. i set it to correct value and now the script shared by you is working flawlessly. but after giving this solution, there was one more need arise that before adding the request participants to the list there should be issue match condition on Reporter. let's say. if Reporter is A,B,C then add request participants else don't do anything. i am not sure if it's possible or not. i guess you are expert in this. can you help me add this condition to the existing script. 

Like Darius Mikelevičius likes this
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2016
Vivek Janjrukiya November 30, 2016

This is new script, i modified to match issue reporter and then update request participant but some how it's giving me an error.

2016-11-30 15:30:10,199 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2016-11-30 15:30:10,199 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SS-915, actionId: 11, file: &lt;inline script&gt;
java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$1.call(Unknown Source)
at Script757.run(Script757.groovy:33)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserUtil()
def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_17806")
def getUsersAsArray =
{
    def users = ["pravin.mori"]
    ArrayList&lt;ApplicationUser&gt; userList = new ArrayList&lt;ApplicationUser&gt;()
    users.each
    {
        def user = userManager.getUserByName(it)
        if(user)
        {
          userList.add(user)
        }
    }
    return userList
}
  
CustomField reporter = customFieldManager.getCustomFieldObjectByName('Reporter')
if (issue.getCustomFieldValue(reporter) == "vivek.janjrukiya")
{
MutableIssue myIssue = issue
ArrayList&lt;ApplicationUser&gt; applicationUsers = getUsersAsArray()
myIssue.setCustomFieldValue(requestParticipantsField, applicationUsers)
}
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2016

It seems that variable issue is not initialised. Where do you run this script? Into post-funciton this variable is defined, but not into a script console.

Vivek Janjrukiya November 30, 2016

Yes, i am running this script in Post-function as custom scrip port-function. and writing this script in script console. any idea why it's not running ?

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 30, 2016

If you want to run this script into script console you should to create variable issue like that

//comment it into a postfunction
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issue key")
Vivek Janjrukiya December 1, 2016

it's dynamic, i mean, when issue is created and reporter is from the list of reporter (A,B,C,D...) then add (X,Y,Z...) users into request participant.

it's not for issue specific.

i am struggling since week to make to work. but somehow it's failing in each try.

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 1, 2016

I just mean that at first it is better to test script into script console. For this it is requred to define variable issue. Do it manually for existing issue. When this script will work into script console then it will be possible to adopt it for a postfunction on create postfunction.

If this script for create transition then add save issue to database postfunciton

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 1, 2016

Also check if you have build-in postfunction to save issue to databese after script one.

0 votes
Maro Hamamjyan February 23, 2021

This code is working, please check.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.IssueChangeHolder

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserUtil()
def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_10000")
def getUsersAsArray = {
def users = ["user1", "user2"]
ArrayList<ApplicationUser> userList = new ArrayList<ApplicationUser>()
users.each {
def user = userManager.getUserByName(it)
if(user)
{ userList.add(user)}
}
return userList
}

MutableIssue myIssue = issue
ArrayList<ApplicationUser> applicationUsers = getUsersAsArray()
log.warn applicationUsers

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();


requestParticipantsField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(requestParticipantsField), applicationUsers),changeHolder)

0 votes
belphegro February 6, 2019

The problem with this script is that when they been copied the < and > been swapped to &lt" and &gt" in the code. Please see below codes that will work

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserUtil()
def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_10001")
def getUsersAsArray = {
    def users = ["ben", "steve"]
    ArrayList<ApplicationUser> userList = new ArrayList<ApplicationUser>()
    users.each {
        def user = userManager.getUserByName(it)
        if(user)
        { userList.add(user) }
    }
    return userList
}

MutableIssue myIssue = issue
ArrayList<ApplicationUser> applicationUsers = getUsersAsArray()
myIssue.setCustomFieldValue(requestParticipantsField, applicationUsers)

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserUtil()
def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_17806")
def getUsersAsArray =
{
    def users = ["pravin.mori"]
    ArrayList<ApplicationUser> userList = new ArrayList<ApplicationUser>()
    users.each
    {
        def user = userManager.getUserByName(it)
        if(user)
        {
          userList.add(user)
        }
    }
    return userList
}
  
CustomField reporter = customFieldManager.getCustomFieldObjectByName('Reporter')
if (issue.getCustomFieldValue(reporter) == "vivek.janjrukiya")
{
MutableIssue myIssue = issue
ArrayList<ApplicationUser> applicationUsers = getUsersAsArray()
myIssue.setCustomFieldValue(requestParticipantsField, applicationUsers)
}

Suggest an answer

Log in or Sign up to answer