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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Fill User picker field based on Security group Script fix

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

You are calling CustomFieldManager instead of customFieldManager. 

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

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

So now I'm left with this

 

Screenshot 2019-09-03 at 10.14.36.png

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())

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.

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. 

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 

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. 

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.

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?

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 :) 

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
Community showcase
Published in Jira Service Desk

The Complete Guide to Atlassian for ITSM

Hi Atlassian Community! This is Teresa from the Atlassian team. My colleague Paul Buffington @Buff and I are excited to share a brand new ITSM resource we’ve created – "The Complete Guide to At...

2,186 views 15 22
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you