I have installed Scriptrunner to resolve the following issue:
In my JIRA core instance I have created a workflow. When moving an issue from "open" to the next step in the workflow I have defined a screen. In that screen, the user has to enter values to a number of custom text fields. When I close the screen I would like to run a post function that calculates the concatenation (with a space as separator) of two separate custom text fields into a third custom text field.
An example:
Custom Text Field 1 value: "abcd"
Custom Text field 2 value: "efgh"
"calculated" Customer Text field 3 value: "abcd efgh"
In this example, the user has provided values for Customer Text fields 1 and 2 in the screen but not for Customer Text field 3.
Although I have some experience with coding (mostly in the R domain, so mostly functional programming) I am a newby to JIRA and Scriptrunner. I have looked at various examples but for instance fail to understand how to concatenate the fields and update the DB.
And would such code also work if the 'calculated' field would be the summary field?
Any help, or sample scripts, would be very helpful.
I am using JIRA Core v7.2.1 and Scriptrunner 4.3.11
Many thanks!
Hi Paul,
This works for me!!
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.ModifiedValue IssueManager issueManager = ComponentAccessor.getIssueManager() CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() CustomField field1 = customFieldManager.getCustomFieldObjectByName("Custom field 1 Name") CustomField field2 = customFieldManager.getCustomFieldObjectByName("Custom field 2 Name") String field1Value = (String)issue.getCustomFieldValue(field1) String field2Value = (String)issue.getCustomFieldValue(field2) def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 3 Name"} def changeHolder = new DefaultIssueChangeHolder(); tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value+" "+field2Value),changeHolder);
Cheers!!
Hi Mahesh,
With a small adjustment, realizing that the the "Custom field 1" and "Custom field 2" needed to be derived from the current issue, I got the following to work correctly:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.ModifiedValue IssueManager issueManager = ComponentAccessor.getIssueManager() CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 1"} CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 2"} String field1Value = (String)issue.getCustomFieldValue(field1) String field2Value = (String)issue.getCustomFieldValue(field2) def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Custom field 3"} def changeHolder = new DefaultIssueChangeHolder(); tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field1Value + " " + field2Value),changeHolder);
This code is positioned as a post function in one of the transitions in my work flow.
Thanks for pointing me in the right direction.
Paul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Into script postfunction you have issue varialbe which represents curretn issue. Namely an instance of MutableIssue class.
There are two types of issue values: in-build one and custome. There are special method for in-build values into MutableIssue class and the onle one for custom field: setCustomFieldValue.
If you have deal with create transition you should cal IssueManager.updateIssue() method to store changes into a DB. In other cases place your function before in-build post-function that stores issue into a DB.
Here is code for your question:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.MutableIssue CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager(); issue.setCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Text field 3") , customFieldManager.getCustomFieldObjectByName("Text field 1") + customFieldManager.getCustomFieldObjectByName("Text field 2") ) ComponentAccessor.getIssueManager().updateIssue( ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser() ,issue , EventDispatchOption.ISSUE_UPDATED , false )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks much Vasiliy!
I created a test custom field called "RMA scripted test field" and copied your code in the "Script fields" page of the Script-runner add-in. I get several error messages. Apologies to ask for your help once more.
2016-10-24 16_56_34-Script Fields - JIRA Manufacturing - WSS.png
and:
2016-10-24 16_57_39-Script Fields - JIRA Manufacturing - WSS.png
Paul
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hm, here is for JIRA 7:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.MutableIssue CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager(); issue.setCustomFieldValue( customFieldManager.getCustomFieldObjectByName("Text field 3") , customFieldManager.getCustomFieldObjectByName("Text field 1") + customFieldManager.getCustomFieldObjectByName("Text field 2") ) ComponentAccessor.getIssueManager().updateIssue( ComponentAccessor.getJiraAuthenticationContext().getUser() ,issue , EventDispatchOption.ISSUE_UPDATED , false )
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.