Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to change Priority based on Select List (Single Choice) Custom Field?

Sha A. September 7, 2020

Hi!

I'm new here and also a new user of the Adaptavist Scriptrunner.

I'm trying to setup our create issue screen that when the 'Project Group' is Emergency, ticket priority will be set to High when the ticket gets created (Priority is not in the list that user needs to fill out).

I researched all over and came up with this. I am not sure where the problem may be because I'm not receiving any errors. **sorry if it looks bad, it's my first time to try this**

 

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.PrioritySystemField

def ProjectGroup = getFieldById("customfield_14701")
def Priority = getFieldById("Priority")

if (ProjectGroup.getValue() == "Emergency") {
Priority.setFormValue("High")

}
else {
Priority.setFormValue("Medium")

}

 

I hope someone can help me. Thank you :)

1 answer

1 accepted

0 votes
Answer accepted
Gustavo Félix
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.
September 7, 2020

Hi @Sha A. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = issue as MutableIssue
IssueManager issueManager = ComponentAccessor.getIssueManager()
def constantsManager = ComponentAccessor.getConstantsManager()
def projectGroupField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Your Field")
def projectGroup = issue.getCustomFieldValue(projectGroupField)
Priority priority = constantsManager.getPriorities().find{it.name =="High} 
if(projectGroup == "Emergency"){
issue.setPriorityId(priority.id)
}
issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,false)

Just add the ifs/else that you need, but that should work 

Hope that helps you.

Sha A. September 8, 2020

Hi @Gustavo Félix ,

 

Thank you for you help on this. I really appreciate it.

Unfortunately I am getting an error without any details.

Correct me please if I am wrong this is what I did:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = issue as MutableIssue
IssueManager issueManager = ComponentAccessor.getIssueManager()
def constantsManager = ComponentAccessor.getConstantsManager()
def projectGroupField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Project Group")
def projectGroup = issue.getCustomFieldValue(projectGroupField)
Priority priority = constantsManager.getPriorities().find{it.name =="High}
if(projectGroup == "Emergency"){
issue.setPriorityId(priority.id)
}
issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,false)

 

Line with Priority priority gives a red x error but no message.

 

Thank you again. :)

Gustavo Félix
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.
September 8, 2020

@Sha A.  At the end of High you need to add the " . 
I saw that I missed it on my example.
Priority priority = constantsManager.getPriorities().find{it.name =="High"}

Like Sha A. likes this
Sha A. September 9, 2020

Hi @Gustavo Félix ,

 

Thanks for confirming. I tried that and added the " after High but the red x without a message still shows up for the line.

 

Thank you. :)

Gustavo Félix
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.
September 9, 2020

If you still have the error , can you add your code so I can see it ? 

Like Sha A. likes this
Sha A. September 9, 2020

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = issue as MutableIssue
IssueManager issueManager = ComponentAccessor.getIssueManager()
def constantsManager = ComponentAccessor.getConstantsManager()
def projectGroupField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Project Group")
def projectGroup = issue.getCustomFieldValue(projectGroupField)
Priority priority = constantsManager.getPriorities().find{it.name =="High"}
if(projectGroup == "Emergency"){
issue.setPriorityId(priority.id)
}
else {
Priority.setFormValue("Medium")
}
issueManager.updateIssue(user,issue,EventDispatchOption.DO_NOT_DISPATCH,false)

Gustavo Félix
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.
September 9, 2020

Do you have the import related to Priority ? 
In my test is :
import com.atlassian.jira.issue.priority.Priority

Sha A. September 15, 2020

Yes I have an import for priority. same as in your test. but still getting error.

Gustavo Félix
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.
September 15, 2020

Ok, another question.

Are you using this code as a postfunction or a behaviour?

Sha A. September 15, 2020

I am using behaviors. I'm not really sure how to configure it via postfunction. I am not able to figure out which postfunction would work the way we need it to.

Gustavo Félix
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.
September 15, 2020

I think it fits better with a postfunction.

You can follow this.

https://scriptrunner.adaptavist.com/latest/jira/tutorials/scripted-post-functions-tutorial.html

The transition that it mentions, is the create transition.

The post function that you are going to add is a "Script post Function" -> Custom script post function.

You can paste the code that I put you, and it should work. Just add the other ifs/else that you need for the other priorities.

Let me know if you have any problems.

Like Sha A. likes this
Sha A. September 29, 2020

Thank you @Gustavo Félix I will try that and see how it goes.

Like Gustavo Félix likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events