Hi,
I have written a post function groovy script that I use to populate a custom field with the issue.getId() and it works quite nicely on "normal" transitions.
I now need to populate the custom field on issue creation and have added it to the "Create" transition. If my script is run before the "Creates the issue originally" post function issue.getId() is not yet populated and therefore successfully populates the custom field with "null". If I run my post function after the "Creates the issue originally" post function I can see that the issue.getId() value is determined via the catalina.out logs - but the change never appears in the custom field.
I suspect this has to do with storing the changes to the custom field (ie the database) but I am running out of ideas. Please let me know if there is any further information I can provide. The script is below:
import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.ComponentManager import org.apache.log4j.Category log = Category.getInstance("com.onresolve.jira.groovy.PRID") log.setLevel(org.apache.log4j.Level.DEBUG) CustomFieldManager customFieldManager = componentManager.getCustomFieldManager() Issue issue = issue def cfPrId = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'PR ID'} if ( !issue.getCustomFieldValue(cfPrId) ) { log.debug "PR ID not set for "+issue.getKey()+" setting it to: "+issue.getId() issue.setCustomFieldValue(cfPrId, issue.getId().toString()) log.debug "PR ID now set to: " +issue.getCustomFieldValue(cfPrId) } else { log.debug "PR ID is already set for "+issue.getKey()+": "+issue.getCustomFieldValue(cfPrId) }
Community moderators have prevented the ability to post new answers.
So the problem here is that, in your post-function on the create step, you need to do two things - 1) read a CF value from the issue and 2) update a different CF value.
If you were just updating a value, you could put your function before "create the issue originally" and use:
issue.setCustomFieldValue(tgtCustomField, "some value")
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.util.IssueChangeHolder ... IssueChangeHolder changeHolder = new DefaultIssueChangeHolder(); tgtField.updateValue(null, issue, new ModifiedValue("", "some new value"), changeHolder);
The correct order for the functions is:
Interesting. Think groovy is saving the day for inline scripts for now!.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jamie,
Thanks again! I don't know if I ever would have ever found this solution without your help.To get it working I had to add 'import com.atlassian.jira.issue.ModifiedValue'.
For anyone interested - here is the full code based on the example given in the question with Jamie's solution incorporated:
import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.util.IssueChangeHolder import com.atlassian.jira.issue.ModifiedValue import org.apache.log4j.Category log = Category.getInstance("com.onresolve.jira.groovy.PRID") log.setLevel(org.apache.log4j.Level.DEBUG) CustomFieldManager customFieldManager = componentManager.getCustomFieldManager() IssueChangeHolder changeHolder = new DefaultIssueChangeHolder(); Issue issue = issue def cfPrId = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'PR ID'} if ( !issue.getCustomFieldValue(cfPrId) ) { log.debug "PR ID not set for "+issue.getKey()+" setting it to: "+issue.getId() cfPrId.updateValue(null, issue, new ModifiedValue("", issue.getId().toString()), changeHolder); log.debug "PR ID now set to: " +issue.getCustomFieldValue(cfPrId) } else { log.debug "PR ID is already set for "+issue.getKey()+": "+issue.getCustomFieldValue(cfPrId) }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problem. Yep sorry, missed that import.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, thanks for the examples. I need to do almost the same thing but it doen't work. The following code works fine as a custom field (if I try it on an issue) but when I try the same code as a post function it does not update the field, why? import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.util.IssueChangeHolder import com.atlassian.jira.issue.ModifiedValue CustomFieldManager customFieldManager = componentManager.getCustomFieldManager() CustomField cf = customFieldManager.getCustomFieldObjectByName("CQ Timestamp") IssueChangeHolder changeHolder = new DefaultIssueChangeHolder(); Issue issue = issue if (issue.issueTypeObject.name == 'Epic') { cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), ""), changeHolder); }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
check your logs... any errors? What type of field is CQ Timestamp?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
CQ Timestamp is a regular text field. Atlassian-jira.log show some errors from scriptrunner if I actually have written the wrong code but when I got it right (checking it in the scripted field) no entries in the log appear. Very strange, it just doesn't do anything. I installed the Jira Suite Utilities now and there it works without problems.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jamie I am also new to groovy and calling groovy script as post functions. My problem is I have also tried this basic script but cant see any statement in log. I have checked catalina.out as well as atlassian-jira.log Can you please help.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Set your log levels by running the script here: https://jamieechlin.atlassian.net/wiki/display/GRV/Getting%20Help#GettingHelp-Supportrequests - you should then see log output.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My first guess is that since the issue object hasnt been created yet. You dont have access to that object. I had problems with this too in the past. Take a look at the following code and i hope it helps you.
import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.IssueManager import com.opensymphony.workflow.InvalidInputException import groovy.time.* import org.codehaus.groovy.runtime.TimeCategory import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem import static java.lang.Math.* Issue issue = issue def result = "" def duration = "" customFieldManager = ComponentManager.getInstance().getCustomFieldManager() issueManager = ComponentManager.getInstance().getIssueManager() //Getting custom fields objects cfStartDate = issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Start Date") ) cfEndDate = issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("End Date") ) cfAllocation = issue.getCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Allocation") ) cfLOE = customFieldManager.getCustomFieldObjectByName("Level Of Effort") if ( (cfStartDate == null) || (cfEndDate == null ) || (cfAllocation == null ) || cfAllocation == "None" ) { invalidInputException = new InvalidInputException("'Start Date','End Date' and 'Allocation' fields are mandatory") } else { startDate = cfStartDate.toString() endDate = cfEndDate.toString() allocation = cfAllocation.toString()[0..-2] dStartDate = new Date().parse("yyyy-MM-dd", startDate) dEndDate = new Date().parse("yyyy-MM-dd", endDate) use(groovy.time.TimeCategory) { duration = dEndDate - dStartDate result = Math.round( ((duration.days) * allocation.toInteger()*5/7)) / 100 //LOE Formula } //Assigning values issue.setCustomFieldValue(cfLOE,result.toString()) issue.summary = "Resource Type - Assignee - LOE = " + result.toString() + " days" log.debug "NRECA LOE DEBUG: Days=> ${duration}" + ",Allocation => ${allocation}, LOE=>" + result }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Louis,
I think you are referring to the issue.getId() not being generated yet? This is true for the first scenario I described where the custom field is successfully populated with "null", however if I run my script after "
Creates the issue originally" I can see in the logs that it is populated. The debugging statement:
log.debug "PR ID now set to: " +issue.getCustomFieldValue(cfPrId)
even shows that not only is
issue.getId() working but
the custom field is actually set to the right value - BUT it never gets fed back to the database or appears in the web interface.
Boil it down and your code is almost identical to mine - Do you run your script on the Create transition?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
Our solution...
we have created a "Set Defaults" transition on the initial status with all the field adjustment post functions - mostly build with Adaptavist ScriptRunner
At the end of the the Create post functions we use JIRA Misc. Workflow Extensions to fire the "Set Defaults" transition automatically. This now has access to all the fields and we can manipuate anything we want.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Another potential problem I frequently run into is the fact that fields are not editable on the corresponding screen. Due to the fact that custom fields are not on the edit screen, the setCustomFieldValue fails silently.
Best regards
Maba
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Would the setCustomFieldValue failing not be be reflected in the output from the debugging line immediately following?
log.debug "PR ID now set to: " +issue.getCustomFieldValue(cfPrId)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I can't see that this could be the issue. Yes it's an issue when using WorkflowTransitionUtil, but not when setting a customfield value through the API. Screens just don't come in to play here.
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.