Help with this script

DanielG
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.
April 4, 2013

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

1 vote
Answer accepted
Henning Tietgens
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.
April 4, 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
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.
April 4, 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
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.
April 7, 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

DanielG
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.
April 7, 2013

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)
}




DanielG
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.
April 7, 2013

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