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

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

Daniel Perdisatt July 12, 2017

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

Suggest an answer

Log in or Sign up to answer
2 votes
Joshua Yamdogo @ Adaptavist
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.
July 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.

Daniel Perdisatt July 26, 2017

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 @ Adaptavist
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.
July 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.

Daniel Perdisatt July 27, 2017

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 @ Adaptavist
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.
July 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.

Kumar
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.
January 17, 2019

Hi Joshua Yamdogo @ Adaptavist 

I have an Similar requirement My requirement is when the issue assignee changes to another user in the group field has to display the user belonged group

The above script i have tried on issue create transition postfunction and its worked in one way

Can you please help me here

to add how to add groups list that i have and the script has to work when theh issue creating and issue updating 

can you please help to achieve this

Thanks,

Kumar

TAGS
AUG Leaders

Atlassian Community Events