Script Runner - Custom Script Post-Function With Conditions

Bryan Harte October 17, 2016

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

0 votes
Vasiliy Zverev
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 Leaders.
October 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?

Bryan Harte October 17, 2016

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
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 Leaders.
October 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

Bryan Harte October 18, 2016

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
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 Leaders.
October 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 )
    }
}
0 votes
saravanan subramanian October 17, 2016

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

Bryan Harte October 17, 2016

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".

Suggest an answer

Log in or Sign up to answer