Script Runner - Custom Script Post-Function With Conditions

I want to be able to use the contents of a drop down field to populate another drop down field in the same issue on transition using custom script post-function in Script Runner:

If field A contains "Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8", "Option 9" or "Option 10" then field B should be set to "Updated". If field A contains none of these options, field B remains unchanged.

I have tried a number of options using code I have found in different posts but nothing seems to work. Can anyone help me with this please?

2 answers

This widget could not be displayed.

Try this

A more complex example, where if the user user sets the select list field Demo to No, we require them to fill in a text field Reason for no demo:

 

cfValues['Demo']?.value != 'No' || cfValues['Reason for No Demo']

 

c

Hi Saravanan. That's not what I want the script to do. I need it to amend another field automatically rather than present the user with another field to complete as the second field can only be one of 2 things, unchanged or "Updated".

This widget could not be displayed.
Vasiliy Zverev Community Champion Oct 17, 2016

Here code for this:

package CustomFields.getValue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option

List<String> selectedOptions = Arrays.asList("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8", "Option 9", "Option 10")

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZVER-93")

for(Option option: ((List<Option>) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ТестМульВыбор")))){
    selectedOptions.contains(option.toString()){
        issue.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("field B"), "Updated")
        ComponentAccessor.getIssueManager().updateIssue(
                ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
                ,issue
                ,EventDispatchOption.ISSUE_UPDATED
                ,false )
    }
}

Could you provide types of custiom fields?

Thanks Vasiliy. I'm using single choice select lists for each of my fields, will that change anything in the above script you've kindly written?

Just want to check, I assume "ТестМульВыбор" should be the title of field A? Also, what is "ZVER-93" so that I can replace this in my code as I assume this is unique to you?

 

Vasiliy Zverev Community Champion Oct 18, 2016

ZVER-93 is a key for my test issue. Here is a cleared code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
 
List<String> selectedOptions = Arrays.asList("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8", "Option 9", "Option 10")
 
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZVER-93")
 
for(Option option: ((List<Option>) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("ТестМульВыбор")))){
    selectedOptions.contains(option.toString()){
        issue.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("field B"), "Updated")
        ComponentAccessor.getIssueManager().updateIssue(
                ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
                ,issue
                ,EventDispatchOption.ISSUE_UPDATED
                ,false )
    }
}

I would recomed to test it with script console, see this: https://answers.atlassian.com/questions/32982259

Thanks Vasiliy.

Sorry, just want to confirm a couple of things:

  • Should "ТестМульВыбор" should be the title of field A for me?
  • Should I replace "ZVER-93" with $issue so that it refers to the issue I am transitioning? If not, should this line be removed or updated?
Vasiliy Zverev Community Champion Oct 18, 2016

Sorry, I am so careless last days. issue - is a varialbe, already defined into a postfunction and represents current issue.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option

List<String> selectedOptions = Arrays.asList("Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8", "Option 9", "Option 10")

for(Option option: ((List<Option>) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("field A")))){
    selectedOptions.contains(option.toString()){
        issue.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("field B"), "Updated")
        ComponentAccessor.getIssueManager().updateIssue(
                ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
                ,issue
                ,EventDispatchOption.ISSUE_UPDATED
                ,false )
    }
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Tuesday in Jira

What modern development practices are at the heart of how your team delivers software?

Hey Community mates! Claire here from the Software Product Marketing team. We all know software development changes rapidly, and it's often tough to keep up. But from our research, we've found the h...

195 views 1 3
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