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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

131 views 0 2
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