How to change value via listener?

NewByatl
Contributor
September 6, 2021

Hello!

I use listener (scriptrunner)

Events: issue updated

 

Task: I have 2 values 1. Dropdown selector  (def field_2) 2. Multiple selecor (def field_3)

 

I need to do something like that

if ( CRM S or CRM M or CRM L in field_3) { field_2 = "не XS"}

else { field_2 ="XS"}

 

 

This is my script:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue = event.issue


def field_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Предварительный размер")
//provisional size - dropdown selector
def field_3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Размер (ы) заявки")
//issue size - multiple value field

def newValueSize = issue.getCustomFieldValue(field_3) as List
log.warn("*****")
log.warn(field_3 )
log.warn(newValueSize )
log.warn("*****")

def fieldConfig = field_2.getRelevantConfig(issue)
def optionValue = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'XS' }
def optionValueNoXS = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'не XS' }

//def giantBrap = newValueSize.contains("CRM S").toString() || newValueSize.contains("CRM M").toString() || newValueSize.contains("CRM L").toString()

log.warn("Test1 issue ${issue.key} ${issue.getCustomFieldValue(field_3)}")
// !!! if (newValueSize.contains("CRM S") - doesnt triggered !!!
//newValueSize.contains("VALUE").toString() - doesnt work correct, it ALWAYS triggered if-part ot this code ( i have no idea why, but .toString() ALWAYS trigered if-part)
if (newValueSize.contains('CRM S')|| newValueSize.contains('CRM M')||newValueSize.contains('CRM L')){
log.warn("Test2 issue ${issue.key}")
field_2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field_2), optionValueNoXS), new DefaultIssueChangeHolder())
log.warn("Test3 issue ${issue.key} ${issue.id}")
}
else {
field_2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field_2), optionValue), new DefaultIssueChangeHolder())
}


//Ниже представлен блок реиндекса задачи, возможно, следует проработать такой кейс, чтобы вывести его в отдельный файл, а потом подгружать во все необходимые скрипты, чтобы не дублировать код
//reindex issue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category


def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);
Picture:
z-wLwV7X-mo.jpg

1 answer

0 votes
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 7, 2021

Hi @NewByatl,

I don't see any major error with this code except couple of warnings

1. what is the script doing now?

   Does not update field_2 value at all?

    or always goes to else part?

 

also I've modified script to clear that warning as below

def field_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Предварительный размер")[0]
//provisional size - dropdown selector
def field_3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Размер (ы) заявки")[0]
//issue size - multiple value field 


//this is find & fetch field options using it's value instead of string method
def optionValue = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.value == 'XS' }
def optionValueNoXS = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.value == 'не XS' }

and how about using if as below?

if ("CRM S" in newValueSize) 

  of course both are same. but just another try.

 

also can you pass your logged result to understand the current executions

 

Regards,

Leo

NewByatl
Contributor
September 7, 2021

Greetings, Leo. Thank you, got it. Will be back soon with feedback!

 

PS: I find myself thinking - maybe I have another automation rule in script editor, which ruins my effort to successful complete this rule. I ll check it too.

NewByatl
Contributor
September 7, 2021

I'm back.

what is the script doing now? answer: It always run ELSE-block (except i put in if block something like that: if (newValueSize.contains("CRM S").toString())

I have no idea why it happens, nobody can answer yet.

THanks for modif. i think it will be usefull, but it result is the same :(

 

and how about using if as below?

if ("CRM S" in newValueSize)

yea, I tried to use this construction. but it did not help (

NewByatl
Contributor
September 7, 2021

@Leo 

Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 8, 2021

@NewByatl,

below snippet will work at least

def isValue = false
newValueSize.each{
if(it.toString() in ["value 1","value 2"]){
isValue = true
}
}
if(isValue){
//if part code here
}else{
//2nd part here
}

 

BR,

Leo

Suggest an answer

Log in or Sign up to answer