Forums

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

Trying to use behaviours simple script so that if Priority = 1 then must enter a biz justification

Ed Richard April 18, 2022

this script will not work and I can't think of another thing to try:. Basically, there are 2 fields in play (Priority and Business Justification).. If Priority is set to P1, I want the multi-line text field called Business Justification to be required. I have gotten very similar scripts to work in the past but this one for some reason is driving me crazy... when I do select P1, nothing happens to the Business Justification field)... very frustrating...likely a simple thing but I would appreciate the help.

def formField = getFieldByName("Priority")
def descField = getFieldByName("Business Justification")

def priority1 = formField.getValue()
if (priority1.getValue() == "P1") {
descField.setRequired(true)
}
else {
descField.setRequired(false)
}

1 answer

1 accepted

0 votes
Answer accepted
Sam Bartolome
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.
April 19, 2022

Hi @Ed Richard ,

Can you try the below?

I don't access to a Server environment so not sure it will work, but it should help you to achieve what you are looking for:

By the way, I prefer to use Ids instead of names in case someone decides to change the name. The id will always remain the same while the name can change.

 

 

def prio= getFieldById("customfield_XXXXX")
def businessJustification= getFieldById("customfield_XXXXX")

def selectedOption = businessJustification.getValue() as String


if (selectedOption .getValue() == "P1") {
businessJustification.setRequired(true)
}
else {
businessJustification.setRequired(false)
}

Ed Richard April 19, 2022

thanks for reply....using server version.. tried above but not working.. it did give me some other things to try though... playing with it and experimenting while checking other related posts... nothing yet.. curious if it has to do with the fact that Priority field is a Jira system field and not a custom field??

Ed Richard April 19, 2022

seems I can't get the value out of the Priority field

Ed Richard April 19, 2022

not even this worked:

def prio = getFieldByName("Priority")
def selectedPriority = prio.getValue()
def BusinessJustification= getFieldById("customfield_13900")

if (selectedPriority == "P1") {
BusinessJustification.setRequired(true)
}
else {
BusinessJustification.setRequired(false)
}

Sam Bartolome
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.
April 19, 2022

Any errors?

You may need the libraries to access, https://library.adaptavist.com/entity/set-behaviour-hidden

 

import com.atlassian.jira.issue.IssueFieldConstants
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

 

Also try easy from the beginning like to test the Prio value only as it seems to fail there

Ed Richard April 19, 2022

I agree on where its failing...I don't see any errors (do I go somewhere to see them or a log of some type?) I did move the setting of the Business Justification outside of the if statement temporarily to verify I can make that field required (which works so there is no problem there)... its all about getting the value from Priority now it seems

Ed Richard April 19, 2022

Through much experimentation and community searching, I got it.. please don't as how.. after 12 hours of trying Im just going to take it and use it for now...thanks for the help... it ended up being this:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueConstantImpl

def prio = getFieldById(getFieldChanged())
def selectedPriority = ((IssueConstantImpl) prio.getValue()).getName()
def BusinessJustification = getFieldById("customfield_13900")

if (selectedPriority == "P1") {
BusinessJustification.setRequired(true)
}
else {
BusinessJustification.setRequired(false)
}

Like Oussama Brik likes this
Oussama Brik August 28, 2024

@Ed Richard thanks so much, I had the same prob, and the only difference I have with you is the definition of "selectedPriority" that I missed.

GREAT HELP.

Suggest an answer

Log in or Sign up to answer