Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

2 problems with script in listener (Jira, Scriptrunner, Groovy). No error but it doesnt work as well Edited

Morning! I'm stuck totally and need help as advice or working script.

Im trying to write script-listener wich will be update fields, which fields will be depends of each other, more information you will find in script comments, code, and block with 2 moments

 

important notes: pls, dont pay attention on warns if field_1, field_2, field_3. Beacause i have simmilar script but with easier logic and it works.

 

2 moments:

First dificult: it will be 20 kind of IF-Values - if (newValue_DigitalPlatform.toString() in "[ЦФТ]".... "[CRM]" .... "[1C]" and ect. NOW this part of code is triggered if match in field 100%. How to write script next way: i have 3 values in field...how to trigger ech of those parts [ЦФТ], [CRM], [1C] CRM-part and 1C-part i will write later.

 

Second dificult: Example my field 4 has value [ЦФТ]. IF value 4 == [ЦФТ]THEN value_3==[ЦФТ XS]. But... when they are triggered both values gets null-value. How? WHat is wrong? :(

And i think one more moment for future: how to update em kind of add one more value, but dont clear previous value?

Listener script (also, i put screen of the script, cause its more readeble):

 

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
import com.atlassian.jira.issue.customfields.option.Option

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

//field_1 - select (single)
//field_2 - select (single)
//field_3 - select (multiple)
//field_4 - select (multiple)
def field_1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Тип запроса")
def field_2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Предварительный размер")
def field_3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Размер (ы) заявки")
//deigital platform, here i put ID, cause we have 2 fields with similar name
def field_4 = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(15903)

def newValue = issue.getCustomFieldValue(field_1)
def getStat = issue.getStatus().name

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

//part 2: lets construct the logic between field_3 and field_4
def newValue_DigitalPlatform = issue.getCustomFieldValue(field_4)
def fieldConfig_forSizes = field_3.getRelevantConfig(issue)

def optionValue_CFT = ComponentAccessor.optionsManager.getOptions(fieldConfig_forSizes)?.find { it.toString() == '[ЦФТ XS]' }

if (newValue.toString() in "Изменение ПО" && getStat in ["Выбрано", "Реализация", "Реализован", "Доставка клиенту", "Закрыт"]){
log.warn("TEST1")
field_2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field_2), optionValue ), new DefaultIssueChangeHolder())
// First dificult it will be 20 kind of IF-Values - if (newValue_DigitalPlatform.toString() in "[ЦФТ]".... "[CRM]" .... "[1C]" and ect.
// NOW this part of code is triggered if match in field 100%. How to write script next way: i have 3 values in field...how to trigger ech of those parts [ЦФТ], [CRM], [1C] CRM-part and 1C-part i will write later
if (newValue_DigitalPlatform.toString() in "[ЦФТ]" && getStat in ["Выбрано", "Реализация", "Реализован", "Доставка клиенту", "Закрыт"]){
log.warn("TEST2")
//second dificult. Example my field 4 has value [ЦФТ]. IF value 4 == [ЦФТ]THEN value_3==[ЦФТ XS]. But... when they are triggered both values gets null-value. How? WHat is wrong? :(
//and i think one more moment for future: how to update em kind of add one more value, but dont clear previous value?
field_4.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field_4), optionValue_CFT ), new DefaultIssueChangeHolder())

}

}


//reindex script
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);

image-2021-06-22-10-55-02-507.png
 

1 answer

1 accepted

0 votes
Answer accepted

Hi @Call me Danya 

I'm having a hard time decoding your English. No judgment, I'm sure you tried hard.

What might be helpful is if you provide some screen examples of each of the values available for each field.

Then provide an image of what the 4 fields look like BEFORE the listener executes, then a separate image of what the 4 fileds should look like AFTER the script listener is finished.

Meanwhile ... I think I can point you in the right direction for your second question:

Multi-Select custom fields want to get new values as an Array of all the options you want to have in the field.  

I also prefer the issue.setCustomFieldValue(cfObject, value) approach over the cfObject.updateValue() approach.

So an example to add a new value to field_4 would be:

//the options currently selected
def currentSelectedOptions = issue.getCustomFieldValue(field_4) as ArrayList
//add the new option you found using options manager
def updatedOptions = currentSelectedOptions + optionValue_CFT
//apply the new arraylist of options to the custom field
issue.setCustomFieldValue(field_4, updatedOptions)

//later after all custom field are updated, we must store the issue again before indexing
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false))

it's a good point of view! I will remember that for next time (especially about bad English :)) ) Thank you for the script-block, i will integrate it to my script. Thank you very much!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

115 views 1 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you