add user in Request Participants with Script Runner

I need help


I need to create a script that verifies that the reporter of the issue is part of a particular group, if yes, all people who participate in this group must be inserted in the request participants.

This is my second contact with scriptrunner / groovy (I can not write this script alone)


JIRA 7.1.7
Request participants (customfield_11200)
Script Runner 4.3.18

 

Thank you all for your help.

1 answer

0 votes

You can use the following script in a scripted post-function as the last one on the create transition or as a listener on the issue created event which will run when the issue is created.

You'll need to replace the group name with your one.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.getIssueService()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def issue = issue as Issue

// replace this with your group
def groupName = "special-group"

if (groupManager.isUserInGroup(issue.reporter, groupName)) {
    def users = groupManager.getUsersInGroup(groupName)

    def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_10001")

    users.each { user ->
        def issueInputParameters = issueService.newIssueInputParameters()
        issueInputParameters.with {
            addCustomFieldValue(requestParticipantsField.idAsLong, users*.id*.toString().toArray())
        }
        def updateValidationResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
        if (updateValidationResult.isValid()) {
            issueService.update(currentUser, updateValidationResult)
        } else {
            log.warn updateValidationResult.errorCollection.errors
        }
    }
}

Thank you @Adam Markham [Adaptavist],

But it did not work.

I inserted this script in the last post function of the creation transition.
I changed the 'special-group' information to the group I want to check

The issue is open, but the request participants field is empty.

Did I do something wrong?

One thing I noticed in the script when I put it on the console is that line 9 was highlighted:

Script Console - Service Support Tool - Google Chrome.jpg

But when I put in the post-funtion does not

 

(Is there any log that I can send to try to better understand the error?)

That line you get the error on can you remove it please. The issue should already be in the binding so you don't have to declare it there.

Try and see if it works after that.

As for it not working. Do you see anything in the atlassian-jira.log after creating the issue? If it didn't update the request participants then I'd expect to see an error in the logs.

@Adam Markham [Adaptavist]

Even removing the line that presented the error, the script still does not work.

This is the error message that appears in the log.

 

Time (on server): Mon Mar 20 2017 07:36:02 GMT-0300 (Hora oficial do Brasil)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2017-03-20 07:36:02,592 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-03-20 07:36:02,592 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FLEX-4495, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.addCustomFieldValue() is applicable for argument types: (java.lang.Long, java.util.ArrayList) values: [11200, [dante.labate(dante.labate), fidelity(fidelity), ricardo.maganhati(ricardo.maganhati)]]
	at Script39$_run_closure1$_closure2.doCall(Script39.groovy:22)
	at Script39$_run_closure1.doCall(Script39.groovy:21)
	at Script39.run(Script39.groovy:19)

 

Apparently it lists the people who are in the group, but does not appear in the request participants.

I've updated that line of the script. It should work now.

@Adam Markham [Adaptavist]

Not yet

 

Time (on server): Mon Mar 20 2017 09:34:55 GMT-0300 (Hora oficial do Brasil)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2017-03-20 09:34:55,408 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-03-20 09:34:55,408 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FLEX-4497, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.addCustomFieldValue() is applicable for argument types: (java.lang.Long, [Ljava.lang.Object;) values: [11200, [dante.labate(dante.labate), fidelity(fidelity), ricardo.maganhati(ricardo.maganhati)]]
	at Script54$_run_closure1$_closure2.doCall(Script54.groovy:22)
	at Script54$_run_closure1.doCall(Script54.groovy:21)
	at Script54.run(Script54.groovy:19)

 

The execution of the script is soon after the creation of the issue in the post function

Is correct?

@Adam Markham [Adaptavist]

Any idea?
Users are not added in the request participants field

Tanks

There's actually a better example here: https://confluence.atlassian.com/jirakb/how-to-automatically-add-request-participants-when-creating-an-issue-777026955.html

I've updated my version of the script so that it should now work.

Please ensure the custom field id we are using matches up with the id you have for your request participants field.

@Adam Markham [Adaptavist]

 

Error occurred

 

Time (on server): Wed Mar 22 2017 08:34:10 GMT-0300 (Hora oficial do Brasil)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2017-03-22 08:34:10,066 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-03-22 08:34:10,066 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FLEX-4508, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.addCustomFieldValue() is applicable for argument types: (java.lang.Long, [Ljava.lang.Object;) values: [10001, [12300, 10801, 12304]]
	at Script7$_run_closure1$_closure2.doCall(Script7.groovy:23)
	at Script7$_run_closure1.doCall(Script7.groovy:22)
	at Script7.run(Script7.groovy:20)

 

 

@Adam Markham [Adaptavist]

I checked and the ID of the request participants is 11200.

I only change these two information:
I put the name of the group I want to check
I put the correct value in customfield_xxxx

The call is created, but no one in the field requests participants.

 

I was testing so far with the post-function script.

Test putting scritp on Listerns with event creation issue

The error is different in log.

Time (on server): Thu Mar 23 2017 09:44:17 GMT-0300 (Hora oficial do Brasil)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2017-03-23 09:44:17,397 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2017-03-23 09:44:17,398 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException: Cannot get property 'reporter' on null object
	at Script142.run(Script142.groovy:15)

 

This is the script that is running.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
 
def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.getIssueService()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
def issue 
    issue = issue as Issue
 
// replace this with your group
def groupName = "Clients"
 
if (groupManager.isUserInGroup(issue.reporter, groupName)) {
    def users = groupManager.getUsersInGroup(groupName)
 
    def requestParticipantsField = customFieldManager.getCustomFieldObject("customfield_11200")
 
    users.each { user ->
        def issueInputParameters = issueService.newIssueInputParameters()
        issueInputParameters.with {
            addCustomFieldValue(requestParticipantsField.idAsLong, users*.id*.toString().toArray())
        }
        def updateValidationResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
        if (updateValidationResult.isValid()) {
            issueService.update(currentUser, updateValidationResult)
        } else {
            log.warn updateValidationResult.errorCollection.errors
        }
    }
}

I do not know what to do anymore

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in United States

Atlassian acquires AgileCraft

         Good Day, Bad bad traffic, not sure why!!!! 1/2 hour commute took me 2 hours today 🤯 What helped me is that I kept browsing LinkedIn until...

51 views 4 0
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you