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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
Post 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.