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
Ben MorchBen Morch 

Shorthand If-Then Statement

Hi all,

I am reveiwing some code in our instance that I did not write.  It appears to be a simple if-then statement but I want to make sure I understand the syntax.
cqr = acc.Credit_Quality_Rating__c == null ? 0 : acc.Credit_Quality_Rating__c;

I believe this is saying:
If acc.Credit_Quality_Rating__c is null, then set cqr = 0, else set it to acc.Credit_Quality_Rating__c.

I also have this is in the code that is confusing me:
ReviewDate = ReviewDate || oldAcc.Credit_Review_Date__c != acc.Credit_Review_Date__c ? true : false;

I was having a hard time figuring this one out.  I am just looking for an explanation of the syntax.

Thanks for any help
Best Answer chosen by Ben Morch
Amit Chaudhary 8Amit Chaudhary 8
Means if 
"ReviewDate " is true OR Old "Credit_Review_Date__c " and New "Credit_Review_Date__c" are not equals then true elase false

if( ReviewDate || oldAcc.Credit_Review_Date__c != acc.Credit_Review_Date__c )
{
 ReviewDate = true;
}
else
{
ReviewDate = falsel
}

I hope this will help you. Please mark this as solution if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Means if 
"ReviewDate " is true OR Old "Credit_Review_Date__c " and New "Credit_Review_Date__c" are not equals then true elase false

if( ReviewDate || oldAcc.Credit_Review_Date__c != acc.Credit_Review_Date__c )
{
 ReviewDate = true;
}
else
{
ReviewDate = falsel
}

I hope this will help you. Please mark this as solution if this will help you
This was selected as the best answer
ManojjenaManojjena

Hi Ben,

You are  right ,Basically this is called ternary operator .  Term before question mark is condition if it will true then value retrun After quesstion mark and before colon else value after colon .

 

result = testCondition ? value1 : value2
 

I think you are clear now.

Let me know if it helps !!
Thanks 

Manoj