Forums

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

Update the summary value if the chosen priority is "High" when creating issue

mikel May 22, 2023

Hey! My goal is to update the summary by adding a word "Important" at the beginning of the summary if the selected priority is "High". I am doing this with ScriptRunner by creating a behaviour. I create a mapping for the behaviour with choosing all "issue types". I did not add any field and just add a initialiser scpt. Please see the script below. It has no error but it is failing to update the summary when I set the priority to High when creating issue. 

```

import com.onresolve.scriptrunner.runner.util.UserMessageUtil

def priorityField = getFieldById("priority")
def summaryField = getFieldById("summary")

log.debug(priorityField.getValue()?.toString())

// Check if the chosen priority is "High"
if (priorityField.getValue()?.toString() == "High") {
def summaryValue = summaryField.getValue() as String
log.debug(summaryValue)

// Add "Urgency: " prefix to the summary
def updatedSummary = "Urgency: $summaryValue"
summaryField.setFormValue(updatedSummary)
} else {
// Show a warning message if the priority is not "High"
UserMessageUtil.warning("This behavior only applies when Priority is set to High.")
}

```

2 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
May 22, 2023

Hi @Naheem Olaniyan

After reviewing your code, it will not work as expected. If you look through your if/else conditions, you cannot get the Priority's name directly. You will have to use its Priority ID, i.e. a numeric value if you are using your approach.

You will need to configure a Server-Side Behaviour for the Priority field and modify your slightly to:-

import com.atlassian.jira.issue.IssueConstant
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def priorityField = getFieldById(fieldChanged)
def priorityValue = priorityField.value as IssueConstant
def summaryField = getFieldById("summary")

log.debug(priorityValue)

// Check if the chosen priority is "High"
if (priorityValue.name == "High") {
def summaryValue = summaryField.getValue() as String
log.debug(summaryValue)
// Add "Urgency: " prefix to the summary
def updatedSummary = "Urgency: $summaryValue"
summaryField.setFormValue(updatedSummary)
} else {
// Show a warning message if the priority is not "High"
UserMessageUtil.warning("This behavior only applies when Priority is set to High.")
}

If you notice the updated code above, instead of using priorityField.getValue()?.toString() == "High", I am converting the Priority field's value into an IssueConstant.

From there I am able to use the name and compare it against either High, Low, Lowest, Blocker etc in the if/else condition.

Below is a screenshot of the Behaviour configuration:-

behaviour_config-1.png

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

mikel May 22, 2023

This is really helpful. Thanks

0 votes
Sune Vester Lindhe
Contributor
May 22, 2023

Hi Mikel,

The initialiser script is called when the dialog is shown - and at that point the user has not selected a priority. What you need to do is to add the 'Priority' field and add your script as a server-side script for that field. That way your code will get executed whenever the user selects a value for the'Priority' field. 

Br, Sune

mikel May 22, 2023

Thanks for the clarification

Suggest an answer

Log in or Sign up to answer