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.
Community moderators have prevented the ability to post new answers.
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
what is the field type od "srcField "?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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-------------------------")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
uncomment this the following line
//issue.setCustomFieldValue(srcField,"11111111111");
if you get any errors then forgot to add here!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jeff, which custom field type did you get the problem?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.