function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nag22Nag22 

Ternary operator is not working

Hi

I am working with ternary operator , but it always giving false value

string resName';

resName =  (obj.order_number__c == 1) ?obj.Response : ' NA';

Here order number type is number
Response is Text
But above ternary expression always gives 'NA'.

I tried with native IF like

if(obj.order_number__c == 1)
     resName = obj.Response;
else
    resName = 'NA';

the above code is working fine, but it is not working by using ternary operator.
Anyone explain me the why it is not working.
enforcer118enforcer118
it should be resName =  (obj.order_number__c == 1 ? obj.Response : ' NA');
kibitzerkibitzer
Hmm... I don't really see why changing the paranthesis in this case would make any difference. I don't see any reason your original syntax wouldn't work. Are you absolutely certain that it didn't work and that there were no other factors in play during your testing?
David "w00t!" LiuDavid "w00t!" Liu
I've surrendered trying to use ternary operators, I just write out the if statement in one line:
if (obj.order_number__c == 1) { resName = obj.Response; } else { resName = 'NA' }