Groovy automation (Scripted field)

Sumit Kumar
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.
May 14, 2014

I have two custom field

1--- MLCS field with Options

A ---> i

B ---> ii


2--- Multiselect Field with options

a, b, c, d, e, f

***********************************

While creating issue if I select

B for field 1 & [a,b,c] for field 2

Though a scripted field I want to add [e] as a selected option along with [a,b,c]

so that final options selected in field 2 will be [a,b,c,e]

While in other case where A is selected values should remain as it is

*********************************

Thanks in advance,

Sumit

4 answers

1 accepted

0 votes
Answer accepted
Sumit Kumar
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.
May 14, 2014
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Level 
import org.apache.log4j.Logger 
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.config.FieldConfig





Logger log = Logger.getLogger("<Some Value>") 
log.setLevel(Level.DEBUG)

ComponentAccessor comAcc=new ComponentAccessor()
CustomFieldManager cfm = comAcc.getCustomFieldManager()
optionsProgManager = componentManager.getComponentInstanceOfType(OptionsManager.class) 


CustomField A= cfm.getCustomFieldObject('customfield_XXXX') // id to be changed
CustomField B= cfm.getCustomFieldObject('customfield_YYYYY') // id to be changed
bool=false
fieldConfig = functionalAffetedAreas.getRelevantConfig(issue)
option = optionsProgManager.getOptions(fieldConfig).find {it.value =="<some value>"}

BValue=issue.getCustomFieldValue(A)
AValue=issue.getCustomFieldValue(B)
log.debug("A:::: ${AValue}".toString())
log.debug("B:::: ${BValue}".toString())
log.debug("option:::: ${option}".toString()) categoryValue.findAll {it if (it.toString()=="<Some Value>") { functionalAffetedAreasValue.find {  
if (it.value =="<Some Value>"){
                        	bool=true
                        }
                        
                        }
                        
                        
                        if(bool!=true){
                        log.debug("True".toString())
                        issue.setCustomFieldValue(A, AValue+[option])
BValueNew=issue.getCustomFieldValue(functionalAffetedAreas) log.debug("BValueNew:::: ${BValueNew}".toString())
ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(B), BValueNew);
functionalAffetedAreas.updateValue(null, issue, mVal, new DefaultIssueChangeHolder()); } else{ log.debug("False".toString()) } } } return null

1 vote
JamieA
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.
May 15, 2014

Use a listener for this. Script fields should be used to show values derived from the issue's fields, or other issues or whatever, but should not modify the itself.

Sumit Kumar
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.
May 15, 2014

Thanks Jamie

~Sumit

0 votes
Sumit Kumar
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.
May 14, 2014

If I see in the log values are getting updated but they are not being displayed on issue

or not getting updated on issue.

0 votes
Sumit Kumar
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.
May 14, 2014

I am facing issue mainly when trying to set values for multiselect field along with existing values.

Suggest an answer

Log in or Sign up to answer