+ Start a Discussion

Ternary operator is not working


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;
    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.
it should be resName =  (obj.order_number__c == 1 ? obj.Response : ' NA');
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' }