Forums

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

Script Runner - Validate a field based on another

David Nattrass July 19, 2018

I have two custom fields on my stories that I need to add some validation on.  If the first custom fields value is "Yes" then the second custom field value must also be set to "Accepted" in order for a JIRA transition to be able to take place.

I have tried the two following approaches, both without much luck:

Approach 1:

import com.atlassian.jira.component.ComponentAccessor;

def pciChange =  ComponentAccessor.getCustomFieldManager().getCustomFieldObject("PCI Affecting Change?");

def cabApproval = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("CAB Approval");

def pciValue = (String)issue.getCustomFieldValue(pciChange);

def cabValue = (String)issue.getCustomFieldValue(cabApproval); 

if (pciValue == 'Yes') {

    if (cabValue != 'Accepted') {

        return false

    }

}

 

 

I have tried adding in error logging in this to try and find out the value of my variables during the process but nothing is being logged which would lead me to believe that my custom fields are not being found.  

This approach resulted in the validation rule always being applied no matter what the value of "pciValue".

Approach 2:

cfValues["PCI Affecting Change?"]?.value != "Yes" || cfValues["CAB Approval"] != "Accepted"

This approach resulted in seemingly no validation being applied at all.

 

Any advice or links to documentation that would help solve this would be much appreciated!

1 answer

1 accepted

1 vote
Answer accepted
Mark Markov
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.
July 19, 2018

Hello @David Nattrass

Try this

!(cfValues["PCI Affecting Change?"]?.value == "Yes" && cfValues["CAB Approval"] != "Accepted")
David Nattrass July 19, 2018

Hi @Mark Markov

Thanks for your help!

Nearly there, however it is always failing validation if PCI Affecting Change = Yes; even if CAB Approval is set to Accepted.

If PCI Affecting change = No then it flows through as expected.

Mark Markov
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.
July 19, 2018

Are you sure that you copy condition properly? With && operator. Because this condition must fails in only one situation.

David Nattrass July 20, 2018

Yes, I've got the && in script correctly.  Attached a screenshot of the setup screen so can check for me being dense! :)

Screen Shot 2018-07-20 at 11.18.29 AM.png

Mark Markov
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.
July 20, 2018

Oh, is this select lists custom fields?

If yes we ve got an error here :)

!(cfValues["PCI Affecting Change?"]?.value == "Yes" && cfValues["CAB Approval"]?.value != "Accepted")
David Nattrass July 20, 2018

Ah ha! I clearly missed that bit of useful information in my original post! 

Latest script works perfectly, thanks for all your help!

Mark Markov
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.
July 20, 2018

You re welcome! If it helps you, please mark answer as accpeted. So that, other people will be able to find this answer easily for similar questions.

Suggest an answer

Log in or Sign up to answer