Using Scriptrunner to update group picker based on answer in text custom field

Hi everyone

I have a custom field, which offers users one of five options. When they choose one of these options, I map their choices to the names of notification groups in another custom field. The idea is that I use this custom field to send emails to the groups, but I can't fire an event because it is only a text box, not a group picker. 

When I try to use the script to update a group picker, I get errors informing me I can't update the group field with string.

I am using this script so far:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.MutableIssue;

MutableIssue issueToUpdate = (MutableIssue) issue;

// create the map of Change Area names to approval groups
def approvalGroupMap = [
"Option1" : "tech-approval-group1",
"Option2" : "tech-approval-group2",
"Option3" : "tech-approval-group3",
"Option4" : "tech-approval-group4",
"Option5" : "tech-approval-group5"
];

// get the change area value
def changeAreaFieldObject = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_15502");
def changeArea = issue.getCustomFieldValue(changeAreaFieldObject).toString();
def group = approvalGroupMap[changeArea];

// return the newly set field value to confirm
return group

 This code lets me get the scripted field the name of the right mapped group, but I fail to then update the group picker with this result in the same script. Any ideas how I do this with scriptrunner?

2 answers

1 accepted

0 vote
Mikael Sandberg Community Champion Dec 20, 2017

Have a look at this how-to article, you need to assign the group variable the result from groupManager.getGroup

Mikael, thank you so much!

I have followed the teachings in the guide, and have created the following script, which seems to work well.

I am still new to coding, so this has been a brilliant help. Thanks again

Adam

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;

MutableIssue issueToUpdate = (MutableIssue) issue;

// create the map of Change Area names to approval groups
def approvalGroupMap = [
"Option1" : "tech-approval-group1",
"Option2" : "tech-approval-group2",
"Option3" : "tech-approval-group3",
"Option4" : "tech-approval-group4",
"Option5" : "tech-approval-group5"
];

// get the value of the "Change Area" field
def changeAreaFieldObject = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_XXXXX");
def changeArea = issue.getCustomFieldValue(changeAreaFieldObject).toString();

//configure and define the value taken from the "Change Area" field as a group, rather than a simple string (this is necessary)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def group = groupManager.getGroup(approvalGroupMap[changeArea])

//taking the group value defined immediately above, use it to set Techical Approvers Group field
def tgtField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects(issue).find {it.name == "Custom Field Name"}
def changeHolder = new DefaultIssueChangeHolder();
def mv = new ModifiedValue(issue.getCustomFieldValue(tgtField), [group]);
tgtField.updateValue(null, issue, mv,changeHolder);

//test with a return group to make sure it is working.
return group

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,253 views 14 20
Join discussion

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