Forums

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

Scriptrunner code for validator, is one field or another populated?

Michael Johnson
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.
September 10, 2018

Hello Community,

Disclosure, I'm not a coder and have limited experience with Scriptrunner. Having said that, I'd like to create a validator on the create transition in a new Service Desk project that checks to see if at least one of two fields is populated. I found a previous thread that mentioned this might work but it does not for me.

cfValues['Budget Department'] || cfValues['Project\Program ID']

Any help is appreciated.

Thanks, Michael J

3 answers

1 accepted

0 votes
Answer accepted
Michael Johnson
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.
September 10, 2018

So... I made an error when testing. First, I was trying to use the 'Scripted (Groovy) Validator (JMWE add-on)' instead of the Simple Scripted Validator. Although, once I realized that, I still could not get one of the above solutions to work. Here is the code that worked for me. Maybe it will help someone else. 

cfValues['Budget Department'] != null || cfValues['Project\\Program ID'] != null

This is currently working for a mixed set of a single select and a text box but will eventually be two single select fields. 

0 votes
Tarun Sapra
Community Champion
September 10, 2018

Hello @Michael Johnson

Can you try this (change the name of my select list with name of your custom field)

if (cfValues['My Select List']?.value || cfValues['My Select List']?.value ) {
return true;
} else {
return false;
}
Michael Johnson
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.
September 10, 2018

Hi Tarun,

Thank for your help, the code you provided returned the error below when I tried to create an issue with it in place.

if (cfValues['Budget Department']?.value || cfValues['Project\\Program ID']?.value ) {
return true;
} else {
return false;
}

Validator.JPG

0 votes
Mohamed Benziane
Community Champion
September 10, 2018

Hello Michael,

 

Can you try this:

cfValues['Budget Department'] || cfValues['Project\Program ID']!= ' '

If it's worked you will have to populate the two customfield to go forward

Hope this helps

Regards

Michael Johnson
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.
September 10, 2018

Hi Mohamed,

Thanks for your reply but I don't need to have both fields populated, At least one needs to be populated but not both at the same time.

Best, Michael J

Mohamed Benziane
Community Champion
September 10, 2018

Sorry Michael,

I read too fast

I'm not a groovy coder but you can try this:

if (cfValues['Budget Department']!='' && cfValues['Project\Program ID']==''){
return true
}
else if (cfValues['Budget Department']=='' && cfValues['Project\Program ID']!=''){
return true
}
else return false

Maybe who will have an error with the \ character.

Capture.PNG

Hope this helps.

Michael Johnson
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.
September 10, 2018

Hi Mohamed,

Here's the code I tried and it gave the error below.

if (cfValues['Budget Department']!='' && cfValues['Project\\Program ID']==''){
 return true
}
else if (cfValues['Budget Department']=='' && cfValues['Project\\Program ID']!=''){
 return true
}
else return false

Validator.JPG 

Suggest an answer

Log in or Sign up to answer