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

Adam Gaudry December 20, 2017

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 votes
Answer accepted
Mikael Sandberg
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 20, 2017

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

0 votes
Adam Gaudry December 20, 2017

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