How to update the "Priority" field based another single choice custom field Value in jira.
Asked by Lakshmi S:

Hi Team,

I did not find the solution in our community and tried with behaviors also. We have field "Escalation?". If we change the "Escalation?" to Yes, the priority should change to Critical (after ticket creation). I tried below code behaviors, but still not updating when we changed Escalation is yes. Could you please help me on this.

-------------------------------------------------------------

def escalationfield = getFieldByName("Escalation?")  
def prio = getFieldByName("Priority")

if (escalationfield.getValue() == "Yes")  
{  
prio.setFormValue("Critical")  
}

------------------------------------------------------------


Answers:
author: Leo
body: Hi [@Lakshmi S](/t5/user/viewprofilepage/user-id/11952),

Below is the script for behaviour, I tested in project too

Note: you should use "Escalation" field as source field \& mapped to it

```
if (getFieldById(getFieldChanged()).getValue() == "Yes") {
     getFieldById("priority").setFormValue("Critical")
}
```

And below script used for the same but in Script Listener

```
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.Issue

def issue = event.issue as Issue
def constantsManager = ComponentAccessor.getConstantsManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = new IssueInputParametersImpl()

def cfField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Custom Field Name")
def value = issue.getCustomFieldValue(cfField) 
if(value == "Value 1"){
     issueInputParameters.setPriorityId(constantsManager.getPriorityObjects().findByName("P1").id)
}else if(value == "Value 2"){
     issueInputParameters.setPriorityId(constantsManager.getPriorityObjects().findByName("P2").id)
}else{
     issueInputParameters.setPriorityId(constantsManager.getPriorityObjects().findByName("P3").id)
}

def validationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)

if (validationResult.isValid()) {
     issueService.update(user, validationResult)
} else {
     log.warn validationResult.errorCollection.errors
}
```

Hope it gives you some idea

BR,  
Leo

comments: author: Lakshmi S
body: Perfect. This is working as expected. Thank you [@Leo](/t5/user/viewprofilepage/user-id/2367914)

```
if (getFieldById(getFieldChanged()).getValue() == "Yes") {
     getFieldById("priority").setFormValue("Critical")
}
```


author: balaji
body: Hi [@Leo](/t5/user/viewprofilepage/user-id/2367914) Leo,

We have same scenario but diff custom field

If user select the value "minor" from impact custom field and "likely" from the probability custom field automatically priority must set to "medium"

Will this possible to achieve in behavior

Waiting for the your reply

Thanks!


author: Leo
body: Hi [@balaji](/t5/user/viewprofilepage/user-id/3852315),

I would say YES. that is possible

As I mentioned above you'll have to map/write this behaviour with your custom field(Impact) and then the same script in server side script

```
if (getFieldById(getFieldChanged()).getValue() == "Minor") {
     getFieldById("priority").setFormValue("Medium")
}
```

BR,

Leo


author: balaji
body: hi [@Leo](/t5/user/viewprofilepage/user-id/2367914)

Thanks for your reply,

As i mentioned above there are two fields called impact and probablity

based on the value priority system fields should change ,

Is that possible ?


author: Leo
body: [@balaji](/t5/user/viewprofilepage/user-id/3852315)

you can map behaviour with single field which will be executed when the associated field changed/updated

Of course you can validate 2 field values in priority, but the drawback is below

1. If you map with 1st field, the script will be executed only when 1st field changed. if you change your 2nd field after 1st one. the validation will fail and priority will not be applied

The same goes for if you map with 2nd one.

If you really want to go with behaviour, you'll have write 2, and map each one with each field but with same script(I won't suggest that personally)

In this situation you can consider custom listener. where you can map with your project with issue created \& updated event. here you can validate both fields values and update priority

you can refer my listener script above

BR,

Leo


author: Drishti Maharaj
body: Hi [@Leo](/t5/user/viewprofilepage/user-id/2367914) , I know this is a bit old but I came across this and it might help me. But I was wondering would this work if the custom field is a single choice list?

Would you need to add the name of the field and the choice or would it just be the choice alone?

<br />

if (getFieldById(getFieldChanged()).getValue() == "**Cyber Incident Categorisation - Account Compromise** ") {  
getFieldById("priority").setFormValue("P4")  
}

I tried both ways but it did not seem to update accordingly.

Any help would be appreciated.

Thanks!


author: Fallon Rohllf
body: When trying to set priority value on create screen a warning pops up:

**These fields have default values for this project and issue type combination. We have preserved the existing values in case you need them.**

**Priority**: 1


Tags: confluence-server

jira-software

scriptrunner

server