Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner - failed script setting a Group picker field from another custom field

Cary Watkins
June 27, 2018

I am trying to use the below script to pull the value from one field and apply it to a single Group picker. I have seen this script in another discussion and it worked for them, but I am not sure why it is not working for me. I would like to add this script as a listener for whenever the ticket is updated. Is there any way to make the listener only trigger when a specific custom field is updated? Below is the script that is failing for me:

import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()
def assignGroupField = customFieldManager.getCustomFieldObjectByName("Test Group")

def assignedGroupValue = customFieldManager.getCustomFieldObject("customfield_10601")
def assignableGroup = groupManager.getGroup(assignedGroupValue) //jira group
issue.setCustomFieldValue(assignGroupField, assignableGroup)

In this instance, Custom Field 10601 is where the value is currently. This is an Insight Custom field. I need to take the text from that field, and apply it as a group in the field called "Test Group".

As part of this script, is there any way to add a component that manipulates the text before it adds it to Test Group? I was wondering if I could use the 'substringBefore' function to only utilize part of the text?

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
2 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
July 28, 2020

Behavior initializer run before the fields are populated from the data associated with the issue (from the database).

So to check if a field already has a value in the initializer, you want to look at the binding variable "underlyingIssue" and load those values from that issue object

Something like

def fieldName = 'Board Summary Outcome for Applicant'
def outcome
if(underlyingIssue){ 
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(underlyingIssue).find{it.name == fieldName}
def outcome = underlyingIssue.getCustomFieldValue(cf)
} else {
//underlyingIssue is null on issue create screen
outcome = ''
}
if(!outcome){
getFieldByName(fieldName).setFormValue("Dummy Text Here.")
}
Mark Iveson
July 28, 2020

Thanks Peter - worked a treat. I've modified your initial code block slightly as you defined outcome twice and the block is missing an import to enable the use of the ComponentAssessor.

 

import com.atlassian.jira.component.ComponentAccessor;

def fieldName = 'Board Summary Outcome for Applicant'
def outcome

if(underlyingIssue){

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(underlyingIssue).find{it.name == fieldName}
outcome = underlyingIssue.getCustomFieldValue(cf)

} else {
outcome = ''
}
if(!outcome){
getFieldByName(fieldName).setFormValue("Dummy Text Here.")
}
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Champions.
July 28, 2020

Yeah I wrote all that directly in the answer box... not in an actual environment, so it was all from memory. I assumed you might have had the ComponentAccessor import already for other aspects of your script.
The double def was just me deciding to define it outside the if block mid-way through my response and not being very diligent in reviewing.

 

Glad I pointed you in the correct direction. Don't forget to accept the answer.

Mark Iveson
July 28, 2020

Legend.. thank you.

TAGS
AUG Leaders

Atlassian Community Events