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

Accepted Answer
0 votes
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
Community showcase
Posted Tuesday in Jira

Looking for anyone who made the switch to Data Center

The Jira Marketing team is putting together an ebook on migrating to Data Center. We're looking for pro tips on how you staffed your project team and organized your Proof of Concept. Share yo...

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