Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

problem in use groovy script in jira 5.0 【script runner】

jeff gao October 13, 2013

hi everyone:

I want to change a custom filed's value after the transition of one workflow,so I install the script runner and write a simple groovy script, the script is as follow:

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

ComponentManager componentManager = ComponentManager.getInstance()

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

CustomField srcField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "凭证号"}//a custom field named in chinese language

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

issue.setCustomFieldValue(cfwt,"11111111111")// when after the transition of the workflow, i want to change the value of this custom field
}

then I save these codes in a file "updateValue.groovy",and upload it to a windows server, then in script runner, i input the absolute directory "c:\updateValue.groovy" and submit it. but when i run the workflow, nothing changed.

because I am a newb in Jira-development, how can i test the code in order to verify them right or not? please help me, thanks very much.

-------------------------------------------------------------

Darly.

the type of the custom field is text.

4 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
RambanamP
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 21, 2013

you need to try something like this

issue.setCustomFieldValue(customField, newValue);
 
customField.updateValue(fieldLayoutItem, issue,  new ModifiedValue(oldValue, newValue),    changeHolder);

https://answers.atlassian.com/questions/223353/script-runner-post-function-reindex-fails-to-update-custom-field-help

RambanamP
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 21, 2013

what is the field type od "srcField "?

jeff gao October 21, 2013

I modified the code, but still can't work

----------------------------------------------------------

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.util.IssueChangeHolder

import org.apache.log4j.Category

import org.apache.log4j.Level

log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

log.setLevel(Level.DEBUG);

log.warn("--------------------start-------------");

ComponentManager componentManager = ComponentManager.getInstance();

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

IssueManager issueManager=componentManager.getIssueManager();

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

Issue issue = issue;

def srcField = customFieldManager.getCustomFieldObjects(issue).find(it.name=="预计出差费用");

cfwt = issue.getCustomFieldValue(srcField);

log.warn "<<BW>> the old value is: '" + cfwt.toString() + "'."

//Random rd = new Random()

//issue.setCustomFieldValue(srcField,"11111111111");

srcField.updateValue(null, issue, new ModifiedValue(cfwt, "1111111"), changeHolder);

def newCfwt = issue.getCustomFieldValue(srcField);

log.warn "<<BW>> the new value is: '" + newCfwt.toString() + "'."

log.warn("---------------end-------------------------")

jeff gao October 21, 2013

the custom field's type is text (<255)

jeff gao October 21, 2013

ok, I'll try.

but the two lines make me feel one of them is redundant.

//issue.setCustomFieldValue(srcField,"11111111111");

srcField.updateValue(null, issue, new ModifiedValue(cfwt, "1111111"), changeHolder);

the two lines all have new value, which can be valid?

RambanamP
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 21, 2013

uncomment this the following line

//issue.setCustomFieldValue(srcField,"11111111111");

if you get any errors then forgot to add here!

0 votes
Darly Senecal-Baptiste
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 21, 2013

Hi Jeff, which custom field type did you get the problem?

0 votes
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.
October 13, 2013

Make this script first in the order of post-functions, then it should work. If it's last, you need to do other stuff.

Also add some logging so you can make sure it's being called - eg, have you published the changed workflow?

jeff gao October 20, 2013

I am not sure the script is first of the post-function. I used it as the instruction of the webpage of sript runner, and now I am using IDEA to develop.

jeff gao October 20, 2013

Jamie.

as your instructions on this page:https://jamieechlin.atlassian.net/wiki/display/GRV/Script+Runner#ScriptRunner-DebuggingandCodingwithIDEA

I want to debug the code in IDEA, but i don't understand the meaning of this words"Put a breakpoint on the last line. Copy the script path and run the script in the groovy admin panel. The breakpoint will be hit. The same will work for workflow functions etc."

so I inputed the absolute path of script in a Jira's workflow , and ran the workflow, but nothing happened, is there anything wrong?

0 votes
Bharadwaj Jannu
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 13, 2013

I think you need to add update statement,

IssueManager issueManager=componentManager.getIssueManager();

JiraAuthenticationContext authContext=componentManager.getJiraAuthenticationContext();

issueManager.updateIssue(authContext.getLoggedInUser(),issue,EventDispatchOption.ISSUE_UPDATED,true);

jeff gao October 20, 2013

thanks for your help,but the code can't work

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events