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!)
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For reference for anyone who finds this but only wants single select list comparison the answer is
cfValues['Developer'].value != cfValues['QA'].value
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.