You need to sign in to do that
Don't have an account?
If condition
I have the following code, that works on most occasions, and fails on a few occassions for reasons I cannot figure out.
pricebook 1 if (pb_id == '01s30000000FkSlAAK')
{ store_link = 'https://www.mindstreamshealth.com/store/ADW9P2A';
system.debug('store link :' + store_link);
mail.setPlainTextBody(std_US_plain_txt);
mail.setHtmlBody(std_US_html);
system.debug('std_US_html');
}
pricebook 2
if (pb_id == '01s30000000FkHcAAK')
{ store_link = 'https://www.mindstreamshealth.com/store/MXDF71H-01';
system.debug('store link :' + store_link);
mail.setPlainTextBody(disc1_US_plain_txt);
mail.setHtmlBody(disc1_US_html);
system.debug('disc1_US_html'); }
pricebook 3
if (pb_id == '01s30000000FkHhAAK')
{ store_link = 'https://www.mindstreamshealth.com/store/N28Y43X-02';
system.debug('store link :' + store_link);
mail.setPlainTextBody(disc2_US_plain_txt);
mail.setHtmlBody(disc2_US_html);
system.debug('disc2_US_html'); }
pricebook 4
if (pb_id == '01s30000000FkHmAAK')
{ store_link = 'https://www.mindstreamshealth.com/store/PLI67WR-03';
system.debug('store link :' + store_link);
mail.setPlainTextBody(disc3_US_plain_txt);
mail.setHtmlBody(disc3_US_html);
system.debug('disc3_US_html'); }
pricebook 5
if (pb_id == '01s30000000FkHdAAK')
{ store_link = 'https://www.mindstreamshealth.com/store/QTXRK72-04';
system.debug('store link :' + store_link);
mail.setPlainTextBody(disc4_US_plain_txt);
mail.setHtmlBody(disc4_US_html);
system.debug('disc4_US_html'); }
pricebook 6
if (pb_id == '01s30000000FpbRAAS')
{ mail.setPlainTextBody(TP1_US_plain_txt);
mail.setHtmlBody(TP1_US_html);
system.debug('TP1_US_html'); }
pricebook 7
if (pb_id == '01s30000000Ff94AAC')
{ mail.setPlainTextBody(old_US_plain_txt);
mail.setHtmlBody(old_US_html);
system.debug('old_US_html'); }
else
{ system.debug('Email not Sent. Pricebook match not found' + pb_id);
return('Email Not Sent: Pricebook match not found');
}
pb_id is a string containing the pricebook id that is passed to the apex class containing the above code. I have checked to see if there is any error while passing the pricebook id, but pricebook id passed to this class is accurate. And yet, on occassion, it skips all if statements and goes to the else statement and I get the error stated there. The location of the if statement seems to affect the flow of code, which I don't understand why. For example, the pricebook 7, when placed above all the if statements, will not work even it matches the pricebook id, but by placing it at the end , it seemed to work.
Is there a flaw in the logic? If so, can anyone point it out to me? Also, I would like to know if there is a better way of writing this logic instead of the numerous if statements, something like a Select statement in C.
Thanks,
Sand
Thanks Doug. That makes sense!!
-Sand
All Answers
Normally when you have to check a bunch of stuff like that I would say use a Switch but Apex doesn't support Switch (or not last time I looked).
The issue I see with what you have is you have 7 separate If statements with only the last one having an else.
With logic like this that else is going to be executed when ever any of the previous 6 if statements is executed (or really just when the 7th if isn't executed).
so you have
if() do something
if() do something
if() do something
...
if() do something
else() do something
You should use else if so the ifs and the else are "connected"
if () do something
else if() do something
else if() do something
....
else () do something
Like this you won't ever "fall through" your logic.
Hope that makes sense
Doug
Thanks Doug. That makes sense!!
-Sand
Try rewriting your code as if/else if/ else statements to see if that helps
Example:
if (Condition A) { //Code Here } else if (Condition B) { //Code Here } else if (Condition C) { //Code Here } else { //Code Here }
To my knowledge a siwtch-case is not available as of yet.
I'm not sure the above code will help, but it will increase runtime performance.
Let me know if it helps i'd be interested to know.