Forums

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

Scripted fields difference between " and '

AbrahamA
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 26, 2021

Hello

When I write this script in a scripted field

def pliance = getCustomFieldValue("pliance")

log.warn(".."+pliance+"..")
if (pliance.equals("No")){ return 1}
else return 5

 

pliance is single select drop down. No matter what I select Yes or No, I get value 5 

I tried 

  • if (pliance.equals("No"))
  • if (pliance.equals('No'))

Log value shows ..No.. properly as expected.

 

Couple of questions:

1. What is difference of using " vs ' in the if statement

2. Any idea why it is not retuning 1

 

Thanks

Abe

1 answer

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.
September 26, 2021

For Groovy, you can use " and ' interchangeably, as long as you are consistent with each use - "sheep" or 'sheep', but not "sheep' or 'sheep"

Your if statement is not working because the content of a select list field is not a simple string. 

When your select list contains "No" for an issue, then pliance.equals('No') is going to be false because the field does not contain "No", it contains an "option" object that represents "No" for you. 

The way the option represents something to us is with a "Name", which is a string that the field shows to us on display.  So, you can say

if ("No".equals(pliance.getName())

instead, and that will be then doing a string comparison.  (swap " for ' if you feel like it)

AbrahamA
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 26, 2021

 

Hello Nic

Thanks for the response.

If I use .getName it is not working. When I use .getValue() it is working.

 

It is a bit confusing

def riskCompliance = getCustomFieldValue("pliance")

I thought when I say getCustomFieldValue it will get me value of selected option

 

So when I say 

def pliance = getCustomFieldValue("pliance")  // This is basically getting me a option object which has value "No"

 

if (pliance.equals('No'))   // Is comparing with Option object, hence always goes to else

 

if ("No".equals(pliance.getValue())){ return 1}  // It is actually getting me value of option object and hence check passes.

 

optionName.PNG

Suggest an answer

Log in or Sign up to answer