Set Custom Filed value (grouppicker) if creator/report is in a specific group

Hi,

I'm looking to create a script post function that will set a custom field(group picker) if the user is a member of a specfic group

Group Name = GroupA

Users in GroupA = A,B,C

CustomFiled = Participants (Group Picker (Single Group))

If the report/creator is a member of GroupA i want to set the Participants field to that group name so as that all users in that group can view the ticket. 

thanks,

Dan

 

1 answer

1 votes
Joshua Yamdogo Community Champion Jul 24, 2017

Hey Daniel,

The script would look something like this:

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

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

if (groupManager.isUserInGroup(issue.reporter?.name, 'jira-administrators')) { // Check if the user in Group A
def cf = customFieldManager.getCustomFieldObjectByName("Participants") // Participants Custom Field

def correctGroup = groupManager.getGroup("jira-administrators") // Get the Group we want to set the custom field to
List<Group> groupList = new ArrayList<Group>() // We need to make a list to add to the group to
groupList.add(correctGroup) // You have to pass a list to the GroupPicker custom field

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), groupList), changeHolder) // Update the issue
}

First, the script checks to see is the reporter of the issue in a specific group. Next, we get the custom field object for Participants. Then, we get the group object for Group A. We then add the Group object to a group list. Finally, we update the Participants field and set it to Group A.

Hi Joshua,

Really appreciate the reply, So i have tried the above and it doesnt appear to work, I've replaced my group name in place of 'jira-administrators', you'll have to excuse my ignorance on this bit, but do i need to put something in place of <group>?

 

thanks,

Dan

Joshua Yamdogo Community Champion Jul 26, 2017

Hi Dan,

You're correct that you need to replace your group name instead of 'jira-administrators'. Make sure you replace that in both places in the script, I believe lines 8 and 11. I would ensure that you have the right group name. In JIRA, if you go to User Management -> Groups, it will give you the exact group name. You do not need to replace <Group> with anything. 

The second potential issue here is having the correct name of the custom field you want to set. I would verify the name is correct. In my script, it is "Participants". I would go to Custom Fields page in JIRA and double check you have the right field name.

The third potential issue here is that you don't have your post-function in the correct order. After adding the post-function to the workflow step, did you make sure you put it in the right execution order? See attached image:

Screen Shot 2017-07-26 at 9.50.22 AM.pngPost functions should go after the "Fire" step. You can use the up and down arrows when in edit mode to move the post function to the correct order. Remember to publish your workflow when finished editing.

Also, what JIRA and ScriptRunner versions are you running? That might be useful to know if you still can't get it to work.

Hi Joshua,

So i have tried the above and still no joy, I have checked and copied the name from both the Group and Custom Field.

We are currently using Jira 6.3.15 and script runner 3.1.4, saying that we are in the process of upgrading our system to Jira 7.3.8 and the latest compatable version of script runner, we have run into trouble with the scripts as part of the upgrade, thats holding up the upgrade, we are waiting on support for that. 

 

thanks,

Dan

Joshua Yamdogo Community Champion Jul 27, 2017

Hi Daniel,

Apologies for not getting that information from you sooner. Just to clarify, you are testing this script on an instance of JIRA 6.3.15 with SR 3.1.4? If so, I am not sure that the script I gave you will work at all. I wrote and tested that script with JIRA 7.4.1 and SR 5.0.15. I unfortunately don't think I have a way to test the script for SR 3.1.4.

A lot changed from JIRA 6 to JIRA 7. I would recommend taking a look at this documentation page on our website. If you're evaluating the new version of ScriptRunner or have already purchased it, you can gladly come to the Adaptavist support portal and we can help you with upgrading your scripts if you're having trouble.

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

262 views 0 12
Read article

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot