Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Fill User picker field based on Security group Script fix

Steve Letch September 2, 2019

Hi

 

I've been able to grab this example script but am receiving errors so unable to get it working

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def group = groupManager.getGroup("Automations") // get the CM group
def usersInGroup = groupManager.getUsersInGroup(group) // get the users in that group
def fieldToSet = CustomFieldManager.getCustomFieldObjectsByName("Approvers.") // get the CM Approvers custom field
fieldToSet.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldToSet), usersInGroup), new DefaultIssueChangeHolder()) // update CM Approvers with users

 

I've receiving these errors:

Screenshot 2019-09-02 at 10.50.16.pngScreenshot 2019-09-02 at 10.50.22.pngUnfortunately having to do this because Atlassian still hasn't added support for security groups for Approvals

1 answer

0 votes
Zoi Raskou
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.
September 2, 2019

You are calling CustomFieldManager instead of customFieldManager. 

Also, is your custom field is "Approvers." (with a full stop) or "Approvers"?

Steve Letch September 3, 2019

Yeh it is the one with the full stop, someone before my time created a custom field for approvers instead of using the native one and we already have a screen set up on that one so leaving it in. Pretty sure I changed it to c and received an equal amount of errors, will try now

Steve Letch September 3, 2019

So now I'm left with this

 

Screenshot 2019-09-03 at 10.14.36.png

Zoi Raskou
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.
September 3, 2019

I have tried your code and it works if you change it to a small c and also define the issue (if you don't use the script in a post function or listener).

Also, is the field you want to update a User Picker (multiple options)? In that case you will need to get the fields configuration.

def cf = customFieldManager.getCustomFieldObject("Approvers")
def cfConfig = cf.getRelevantConfig(issue)
def customFieldOptions = ComponentAccessor.optionsManager.getOptions(cfConfig)

def changeHolder = new DefaultIssueChangeHolder()

def fieldOption = customFieldOptions.find {it.value == userInGroup[0]} // the user in this case will need to be retrieved from the usersInGroup list
cf.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf), [userInGroup]), new DefaultIssueChangeHolder())
Steve Letch September 3, 2019

I intend to use it in a Post function as part of our change management overhaul, I want to use the native Service Desk approval functionality instead of permissions and custom emails.

 

I really detest that you have to be a Groovy expert in order to use Scriptrunner with any kind of efficiency.

 

Wish they'd make it easier, I expect they're keeping people in jobs with the knowledge barrier.

Zoi Raskou
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.
September 3, 2019

In this case the context of the object will be passed on by the post function so you don't need to define it in the script.

To be fair there is an extensive knowledge base and lots of examples of how to use it but I understand that it might be difficult to gauge how much effort is involved into automating tasks with Scriptrunner. 

Steve Letch September 3, 2019

Well the thing is it's always a case of having to Google your ass off, eventually find some example, then spend hours more trying to find out what else it needs to modify it to your needs.

 

Would you be able to post the completed example of how to use it in a post function? I noticed it moaning about the usersInGroup line so I assume that needs defining too 

Zoi Raskou
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.
September 3, 2019

I have a similar example but it's for Single User Picket custom field. I changed it slightly to use your variables: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.MutableIssue

def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def group = groupManager.getGroup("Automations")
def usersInGroup = groupManager.getUsersInGroup(group)
def cf = customFieldManager.getCustomFieldObject(10233) //Custom field ID
def cfConfig = cf.getRelevantConfig(issue)
def customFieldOptions = ComponentAccessor.optionsManager.getOptions(cfConfig)

def changeHolder = new DefaultIssueChangeHolder()

def fieldOption = customFieldOptions.find {it.value == usersInGroup[0]}
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), usersInGroup[0]), new DefaultIssueChangeHolder()) // update CM Approvers with users

 

Basically this add the first user in the group in the Approvers custom field. 

The solution will need to be modified if you are adding multiple users. 

Steve Letch September 3, 2019

Looks like it's still failing even without groovy error.

 

Should this one be a lister or post function, currently trying to do it as a post function.

Zoi Raskou
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.
September 3, 2019

Hi Steve, as I mentioned before this script will update a single user picker field. If you are trying to update a multi user picker we will need to make some small changes.

Is there anything in the logs that lets you know it's failing?

Steve Letch September 4, 2019

Hi

 

Jira Misc Workflow Extension plugin has come to the rescue here. I raised it with them and they simply told me to use a set field value post function

 

Groovy Expression

ComponentAccessor.groupManager.getUsersInGroup("my-group")

and it's now doing exactly what I want :) 

Zoi Raskou
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.
September 4, 2019

Happy you got it working. 

Not sure how this is different from the code I sent you though, as this just returns the members and doesn't update the field. 

Please also note that you provided us with a code snippet and the errors and not specified the requirements.  

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events