Hello,
we are trying to change the priority of an issue depending on other custom fields with Script Runner ( https://marketplace.atlassian.com/plugins/com.onresolve.jira.groovy.groovyrunner/versions ).
Unfortunately we cannot find a single working code example, all suggestions for changing the priority field with script runner seem to have stopped working after 2013:
https://answers.atlassian.com/questions/158546/automatically-change-priority-based-on-due-date
https://answers.atlassian.com/questions/224538/need-a-script-groovy-to-change-issues-priority
https://answers.atlassian.com/questions/171074/what-is-the-proper-syntax-to-update-jira-issue-priority-in-a-workflow-post-function
https://answers.atlassian.com/questions/221119/setting-priority-from-another-custom-field-value-using-groovy-post-function
https://confluence.atlassian.com/jira/setting-priority-field-value-based-on-customfield-value-223904500.html
https://answers.atlassian.com/questions/27052/customize-priority-based-on-custom-field
https://answers.atlassian.com/questions/187178/is-it-possible-to-automatically-change-the-priority-when-a-custom-field-changes-to-a-specific-value
Our JIRA Version is 6.4.11 with Script Runner Version 4.1.3.
Does anyone have a working code to change the priority by script for JIRA 6.4?
Kind regards
Community moderators have prevented the ability to post new answers.
You can use this custom post-function script below which will set the priorities according to the matrix in the Atlassian Documentation.
You need to go to your workflow add post-function and then choose scripted post-function of which you need to select "Custom script post-function"
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueInputParametersImpl import com.atlassian.jira.issue.customfields.option.Option // This is a two dimensional map to represent the matrix // Keys along left is urgency and second dimension keys is the impact def priorityMatrix = [ Critical: [Extensive: "Critical", Significant: "Critical", Moderate: "High", Minor: "Medium"], High : [Extensive: "Critical", Significant: "High", Moderate: "Medium", Minor: "Medium"], Medium : [Extensive: "High", Significant: "Medium", Moderate: "Medium", Minor: "Low"], Low : [Extensive: "Medium", Significant: "Medium", Moderate: "Low", Minor: "Low"], ] def customFieldManager = ComponentAccessor.getCustomFieldManager() def constantsManager = ComponentAccessor.getConstantsManager() def issueService = ComponentAccessor.getIssueService() def user = ComponentAccessor.getJiraAuthenticationContext().getUser() def issue = issue as Issue def urgencyField = customFieldManager.getCustomFieldObjectByName("Urgency") def impactField = customFieldManager.getCustomFieldObjectByName("Impact") def urgency = issue.getCustomFieldValue(urgencyField) as Option def impact = issue.getCustomFieldValue(impactField) as Option def priorityName = priorityMatrix[urgency.value][impact.value] def priority = constantsManager.getPriorityObjects().findByName(priorityName) def issueInputParameters = new IssueInputParametersImpl() issueInputParameters.setPriorityId(priority.id) issueInputParameters.setSkipScreenCheck(true) def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters) if (validationResult.isValid()) { issueService.update(user, validationResult) } else { log.warn validationResult.errorCollection.errors }
You should add this as the last post-function on the create transition and assuming you have added the correct priorities and the two custom fields urgency and impact, this will work.
You can use:
mutableIssue.setPriorityId(priorityId);
but it depends on where you place the post-function, above we use the issue service which will do the update in the database and the reindexing.
Hope this helps.
Adam
Thank you Very much Adam it works fine with me when i create new issue
but can you please guide me how can i do the same when i want to update an existing issue where should i put the post function in this case
Appreciate your 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.
Hi adam,
sorry for bothering you , did you find anything regarding my last question
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You should place this as the last post-function on any other transition. It should work for both create and update.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Adam Markham [Adaptavist] i added the post function as a last of create trasition and it works fine , but my problem is when i want to update the severity and urgency later , the priority is not changing because the status of the issue doesnt change (workflow transitions are not triggered here ) check the attached image which will show you what i mean by update the issue
image2016-11-24 9:46:27.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You should in addition to the post-function have a Scripted Listener "custom listener" for the "Issue Updated" event for that project. Admin -> Script Listeners -> "custom listener".
Instead of this line in the script:
def issue = issue as Issue
you need to replace it with:
def issue = event.issue as Issue
That should set the priority when you update them two fields.
More about custom listeners here: https://scriptrunner.adaptavist.com/latest/jira/listeners.html#_custom_listeners
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you @Adam Markham [Adaptavist]
i created the listener but it didnt work with me i found the below error in the script
Time (on server): Fri Nov 25 2016 14:34:04 GMT+0300 (Arab Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2016-11-25 11:34:04,474 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2016-11-25 11:34:04,476 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896) at com.atlassian.jira.issue.Issue$getCustomFieldValue$10.call(Unknown Source) at Script49.run(Script49.groovy:27)
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.
It looks like one of the custom fields is null. Try adding:
log.warn(impactField) log.warn(urgencyField)
to debug the problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear @Adam Markham [Adaptavist]
i got the below errors
Time (on server): Tue Nov 29 2016 12:29:18 GMT+0300 (Arab Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2016-11-29 09:29:18,402 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2016-11-29 09:29:18,403 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896) at com.atlassian.jira.issue.Issue$getCustomFieldValue$10.call(Unknown Source) at Script60.run(Script60.groovy:27)
Time (on server): Tue Nov 29 2016 12:29:16 GMT+0300 (Arab Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2016-11-29 09:29:16,494 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2016-11-29 09:29:16,494 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896) at com.atlassian.jira.issue.Issue$getCustomFieldValue$10.call(Unknown Source) at Script60.run(Script60.groovy:27)
Time (on server): Tue Nov 29 2016 12:29:18 GMT+0300 (Arab Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2016-11-29 09:29:18,402 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2016-11-29 09:29:18,403 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896) at com.atlassian.jira.issue.Issue$getCustomFieldValue$10.call(Unknown Source) at Script60.run(Script60.groovy:27)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Adam Markham [Adaptavist] please try to help me in this ASAP
your continues support is highly appreciated
thanks again
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Adam Markham [Adaptavist] Please let me know if you will help me in this or not
i need your support
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I can not reproduce the same problem you have with the listener. I think its due to you having different options for Impact and Urgency in your single select lists or different priorities. I have the same ones as in this table here along with all the priorities and it works: https://confluence.atlassian.com/servicedeskcloud/automating-the-calculation-of-priority-based-on-impact-and-urgency-values-on-issue-creation-827107708.html
Your listener should look like:
Screen Shot 2016-12-13 at 10.36.11.png
Your script for the listener should also look like:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueInputParametersImpl import com.atlassian.jira.issue.customfields.option.Option // This is a two dimensional map to represent the matrix // Keys along left is urgency and second dimension keys is the impact def priorityMatrix = [ Critical: [Extensive: "Critical", Significant: "Critical", Moderate: "High", Minor: "Medium"], High : [Extensive: "Critical", Significant: "High", Moderate: "Medium", Minor: "Medium"], Medium : [Extensive: "High", Significant: "Medium", Moderate: "Medium", Minor: "Low"], Low : [Extensive: "Medium", Significant: "Medium", Moderate: "Low", Minor: "Low"], ] def customFieldManager = ComponentAccessor.getCustomFieldManager() def constantsManager = ComponentAccessor.getConstantsManager() def issueService = ComponentAccessor.getIssueService() def user = ComponentAccessor.getJiraAuthenticationContext().getUser() def issue = event.issue as Issue def urgencyField = customFieldManager.getCustomFieldObjectByName("Urgency") def impactField = customFieldManager.getCustomFieldObjectByName("Impact") def urgency = issue.getCustomFieldValue(urgencyField) as Option def impact = issue.getCustomFieldValue(impactField) as Option def priorityName = priorityMatrix[urgency.value][impact.value] def priority = constantsManager.getPriorityObjects().findByName(priorityName) def issueInputParameters = new IssueInputParametersImpl() issueInputParameters.setPriorityId(priority.id) issueInputParameters.setSkipScreenCheck(true) def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters) if (validationResult.isValid()) { issueService.update(user, validationResult) } else { log.warn validationResult.errorCollection.errors }
Also check your fields are called "Urgency" and "Impact" and have the same values as in the table.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Dear @Adam Markham [Adaptavist] please check the attached all what you mentioned is correct
image2016-12-13 14:31:55.pngimage2016-12-13 14:33:16.pngimage2016-12-13 14:32:31.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The table in the link shows different values for Urgency and Severity to what you have, you need to change these to what I have below:
Urgency | ||||
---|---|---|---|---|
Impact | Critical | High | Medium | Low |
Extensive | Critical | Critical | High | Medium |
Significant | Critical | High | Medium | Medium |
Moderate | High | Medium | Medium | Low |
Minor | Medium | Medium | Low | Low |
For Impact I have "Extensive, Significant, Moderate, Minor"
For Urgency I have "Critical, High, Medium, Low"
If you want to use just Low, Medium and High you will have to update the "priorityMatrix" values.
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.
Hello,
your solution is working.
Thank you very much, finally this problem is solved. :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I assume your problem is actually updating the priority field and is a post function or listener and not a Behaviour? .. if so please see my answer from a week ago.
https://answers.atlassian.com/questions/32018886
You will need your own code/logic to get the value you want to update the priority to but this example shows how to update the priority. If this part is your problem, please post what you have so far and I can help.
if you are doing this using Behaviours.. that that is another case all together.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just as a side if this is on the create transition in particular, you should place the scripted post-function as your last one in the list.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@adammarkham The priorityMatrix doesn't really comply with the commandline
def priorityName = priorityMatrix[urgency.value][impact.value]
basically the priorityName is null
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
i want to Automate the calculation of priority on my jira software exactly like what described in the below link but the steps in the link is for jira service desk while i want it for jira and i cant find the automation can you please help me
my priorities table matrix below
Urgency/Severity | High | Medium | Low |
---|---|---|---|
High | Blocker | Urgent | High |
Low | High | Medium | Low |
Medium | Urgent | High | Medium |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
i want to Automate the calculation of priority on my jira software exactly like what described in the below link but the steps in the link is for jira service desk while i want it for jira and i cant find the automation can you please help me
my priorities table matrix below
Urgency/Severity | High | Medium | Low |
---|---|---|---|
High | Blocker | Urgent | High |
Low | High | Medium | Low |
Medium | Urgent | High | Medium |
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.