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
sp13sp13 

Trigger - Check if IF statement is successful

Is it possible to check in the trigger if an IF statement is successful or not?
If I have this code in a before update trigger in Account:
for(Account acc : Trigger.New) {
	if(acc.Name = "Test1") {
		acc.TextField1__c = "Test1";
	} else if(acc.Name = "Test2") {
		acc.TextField1__c = "Test2";
	}
}
the problem here is when Test1 and Test2 fails, I get the error msg. Is there a way to check first in the IF Statement if the Test1 update is successful and if not then it would just go to Test2 until it updates successfully?



 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Sp13,
If your logic permits you may use another else block to cover rest of ur code.
With limited information you have given, if you want to know check whether it went into any particular block, you may use some flag variable.

Please try something like,

Boolean notInIfBlock ;
for(Account acc : Trigger.New) {
notInIfBlock  =  True;
    if(acc.Name = "Test1") {
        acc.TextField1__c = "Test1";
        notInIfBlock  = false;
    } else if(acc.Name = "Test2") {
        acc.TextField1__c = "Test2";
       notInIfBlock  = false;
    }

if (notInIfBlock ) 
{  
//Logic if it has not entered in any of the if condition
}
}

Hope this helps!
Please mark it as solved if this helps.