Forums

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

Scriptrunner Listener to update custom field value based on keyword

Matt Dejene November 19, 2019

The code below is supposed to change a custom field (single list) value (approval group) if the summary and description (combined into a string) contains either string, keywords1 and keywords2 (the list of keywords) that correspond to each group defined in the first couple lines. I'm not sure if I'm having a syntax issue or if there is something fundamentally wrong with my listener because it is not firing off. 

import com.atlassian.jira.component.ComponentAccessor;

import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.MutableIssue;

import com.atlassian.jira.issue.Issue;

import com.atlassian.jira.issue.ModifiedValue;

import com.atlassian.jira.issue.customfields.manager.OptionsManager;




var string keywords1 = ["value1","value2",value3"]

var string keywords2 = ["value1","value2",value3"]

def issue = event.issue as Issue

def full_data = issue.Summary + " " + issue.description

def customFieldName = "Assigned Group"

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def optionsManager = ComponentAccessor.getOptionsManager()

       

if (full_data.contains(keywords1))

issueToUpdate.setCustomFieldValue(customFieldName,"Team 1")




if (full_data.contains(keywords2))

issueToUpdate.setCustomFieldValue(customFieldName,"Team 2")

 

1 answer

0 votes
Antoine Berry
Community Champion
November 20, 2019

Hi @Matt Dejene ,

Your code seems correct, please try to use this script to update the select list value (update the id) : 

def customFieldId = 12503
def customField = customFieldManager.getCustomFieldObject(customFieldId)
def customFieldValue = issue.getCustomFieldValue(customField)
def customFieldConfig = customField.getRelevantConfig(issue)

def cfNewValue = ComponentAccessor.optionsManager.getOptions(customFieldConfig)?.find { it.toString() == "Team 1" }
customField.updateValue(null, issue, new ModifiedValue(customFieldValue, cfNewValue), new DefaultIssueChangeHolder())

 Antoine

Antoine Berry
Community Champion
November 22, 2019

Hi @Matt Dejene ,

Did you figure this out ?

Matt Dejene November 26, 2019

@Antoine Berry Im still getting a syntax error for “EOF”, when using var or def for defining the variables for matching keywords. Everything else looks fine. Am I missing something simple? 

Antoine Berry
Community Champion
November 27, 2019

@Matt Dejene is it a warning or an error ? In the console you will get warnings since its a basic java interpreter, but the code will work fine. 

Have you tried executing it ? Could you please provide a screenshot if it fails ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events