Help with this script

Hi,

I have two custom field:

1) Major incident (Selected List): have values of 'None' or 'TRUE'.

2) Comment MI (Selected List): have values of 'Yes' or 'No' or 'None'.

I have one issue created 'PI-1062', that have values of CF 'Major Incident' set to 'TRUE'.

I have this script:

{code}

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

def yes = "Yes";

Issue issue = issueManager.getIssueObject( "PI-1062" );
CustomField major = customFieldManager.getCustomFieldObjectByName( "Major incident" );
def valueMajor = issue.getCustomFieldValue(major);
CustomField commentMI = customFieldManager.getCustomFieldObjectByName( "Comment MI" );


if (valueMajor == "TRUE"){
issue.setCustomFieldValue(commentMI, yes);
}

{code}

I test in Script Runner (Copy & Past) but field 'Comment MI' don't set to 'Yes' in issue 'PI-1062'. Why?

Another question: This script will be a post-function. In this script I test in one specific issue 'PI-1062' but If I want to get instance of issue that execute the post function, how I do this?

Thanks in advance,

Daniel

1 answer

1 accepted

Accepted Answer
1 vote
Henning Tietgens Community Champion Apr 04, 2013

Hi,

to get the issue in a post function you could use "issue" and cast this to a MutableIssue.

Your script must be adapted on two parts. Select Lists values are "Option" objects. So to get the value you have to call getValue() on the value.

For setting a select list, you first have to identify the needed option object and use this as value parameter for setCustomFieldValue().

Henning

Henning Tietgens Community Champion Apr 04, 2013

To find the option object you can use something like this.

optionsManager = ComponentAccessor.getOptionsManager()
fieldConfig = commentMI.getRelevantConfig(issue)
newValue = optionsManager.getOptions(fieldConfig)?.find{it.value == yes}
issue.setCustomFieldValue(commentMI, newValue)

Henning

Henning Tietgens Community Champion Apr 07, 2013

If you run the script in the script runner console, you have to provide "issue" like you did in your question. Only as part of a post function script issue exists.

To get the string values of you custom fields you have to use issue.getCustomFieldValue(srcField).getValue().

Henning

I modify my script but doesn't works...

What I'm doing wrong? I execute in Script Runner and get error:

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issue for class

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor

assert issue instanceof Issue

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
IssueManager issueManager = componentManager.getIssueManager()
def optionsManager = ComponentAccessor.getOptionsManager()

CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Major incident"}
CustomField commentYes = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Comment MI"}

fieldConfig = commentYes.getRelevantConfig(issue)
newValue = optionsManager.getOptions(fieldConfig)?.find{it.value == "Yes"}

cfwt = issue.getCustomFieldValue(srcField)
log.debug("Major Incident value: $cfwt")

//If CustomField 'Major incident == TRUE
if (cfwt == "TRUE"){

            log.debug("YES!")
	    //Set CustomField 'Comment MI'
            issue.setCustomFieldValue(commentYes, newValue)
}




Thanks Henning!

First I test in ScripRunner console with providing an issue and works.

Then I put this post-function in my workflow and works for me! :

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor


//IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
//Issue issue = issueManager.getIssueObject( "PI-1060" );

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

def optionsManager = ComponentAccessor.getOptionsManager()

CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Major incident"}
CustomField commentYes = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Comment MI"}

fieldConfig = commentYes.getRelevantConfig(issue)
newValue = optionsManager.getOptions(fieldConfig)?.find{it.value == "Yes"}


cfwt = issue.getCustomFieldValue(srcField).getValue()

log.debug("Major Incident value: $cfwt")


//If CustomField 'Major incident == TRUE
if (cfwt == "TRUE"){
            log.debug("YES!")
	    //Set CustomField 'Comment MI'
            issue.setCustomFieldValue(commentYes, newValue)
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

471 views 7 5
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