Forums

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

Custom fields -> If Custom Field 'A' field = John Smith, then Custom Field 'B' cannot be John Smith

Arne Pulle October 4, 2021

Hello! 

Pulling my hair out on this one trawling all over @Adaptativst etc. to get an answer

Common problem i feel, but no luck finding Script for Sciptrunner

Two fields in Jira Software, cloud based.. Lets call Field 1 Developer and Field 2 QA. Both are multi select lists. 

The logic I need is if Field 1 'Developer' = 'John Smith', then Field 2 'QA' != 'John Smith' 

I thought Scriptrunner would be able to solve this? 

(Apologies in advance if this is the wrong forum!)

3 answers

2 accepted

0 votes
Answer accepted
Zoryana Bohutska _SaaSJet_
Atlassian Partner
October 6, 2021

Hi @Arne Pulle 

Also, you can look at the other solution Dynamic Filterimgpsh_fullsize_anim (1).png

0 votes
Answer accepted
Phill Fox
Community Champion
October 5, 2021

You can try putting the following on the transition between Status 5 and Status 6 as a validator. 

 

cfValues['Developer'] != cfValues['QA'] 

 

Note this assumes both fields have the same field type. 

Hope this helps 

Phill

Arne Pulle October 5, 2021

Thanks Phil.. Both are multi select fields with names (so yes, the same field type). 

i have tested this as well in Sandbox.. Workflow offers you a chance to validate the change using an issue where I know this is an issue and I get 'false' (the right result).

Then I test it against an issue where I know it should be true (ie: developer != QA) and I still get 'False' 

Phill Fox
Community Champion
October 5, 2021

As your fields are both multi-select fields with names the solution is a little more complex. As you are potentially comparing multiple names with multiple names. 

You need to work out what the logic should be for what is true vs false. 

So if Developer contains
Alfie, Brenda, Charlie

and QA contains
Charlie, David, Emily

I would assume that this would be false as Charlie is present in both. 

Whereas

Developer contains
Alfie, Brenda, Charlie

and QA contains
David, Emily

Should be true.

I am not in front of my development machine at the moment but here is an outline script to help you do this.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def developersCF = customFieldManager.getCustomFieldObjectByName("Developer")
def developers = issue.getCustomFieldValue(cfMultiSelectUser)


passCondition = true
//set initial condition to be true and then check each developer in turn to confirm they are not also in the QA field
for (int i = 0; i < developers.size(); i++) {
if (users.get(i) in cfValues['QA']*.value) {
passCondition = false
}
}

return passCondition

Like Arne Pulle likes this
Phill Fox
Community Champion
October 5, 2021

For reference for anyone who finds this but only wants single select list comparison the answer is 

cfValues['Developer'].value != cfValues['QA'].value

Arne Pulle October 5, 2021

Hi Phill

Tested the script vs a known bad issue (ie: developer = QA..)

"Jira expression failed to parse: line 1, column 1: expression expected, reserved keyword 'import' encountered."

What did I mess up?

Phill Fox
Community Champion
October 6, 2021

Hi @Arne Pulle 

I have now been able to build a test environment to match your requirements and here is a fuller block of code. This will go in the custom script validator option offered by ScriptRunner. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.opensymphony.workflow.InvalidInputException

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def developersCF = customFieldManager.getCustomFieldObjectByName("Dev-select")
def developers = issue.getCustomFieldValue(developersCF)?:[]
log.error("Devs =${developers}")

def qaCF = customFieldManager.getCustomFieldObjectByName("QA-select")
def QA = issue.getCustomFieldValue(qaCF)?:[]
log.error ("QA = ${QA}")

//set initial condition to be true
passesCondition = true
log.error ("passesCondition+${passesCondition}")

//check both QA and developers are not null
if (QA && developers)
{
log.error ("result: ${developers.any{it.toString() in QA.collect{it.toString()}}}")
passesCondition= !(developers.any{it.toString() in QA.collect{it.toString()}})
}

log.error ("passesCondition at end: ${passesCondition}")
//If there are 1 or more entries matching in the QA or Developer fields then throw an exception with an error message
if(!passesCondition)
throw new InvalidInputException("No separation of duty between developers in QA")

In this you will see both comments and logging to help you understand the process in place.  Once you are comfortable with the execution path you can either comment (using //) these log lines or remove entirely. 

Like Arne Pulle likes this
0 votes
Nic Brough -Adaptavist-
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.
October 4, 2021

Absolutely the right place to start asking (it might end up as a support call with Adaptavist, but let's start here with the wider audience!)

At what point do you want to enforce this rule? 

Arne Pulle October 5, 2021

Agree.. i've trawled the Adaptavist site, tried manipulating some Groovy scripts to see if I can make it work, with no luck..

We have a workflow with 7 statuses.. this would need to be enforced between status 5 and status 6.. Failing there, i am happy for the rule to fire if the wrong name is populated, akin to the way Validations fire in Jira Software  

Suggest an answer

Log in or Sign up to answer