+ Start a Discussion

Stop trigger execution if certain parameters are not met.

Is there a way to stop a trigger from updating or inserting a record if certain parameters are not met?
I have an if statement inside a For loop, and am testing values. If the conditions are not met, I don't want the record updated or inserted.
Here's my code if you need it:

public class clsAbstractRatingTeam {

public static void updtEventID(Abstract_Rating_Team__c[] abstRT) {

List<Events__c> eventID = new List<Events__c>([select Id from Events__c where Current_Event__c = true]);

Integer lstSize = eventID.size();

if(lstSize == 1) {

String eID = eventID[0].Id;

for(Abstract_Rating_Team__c ar : abstRT) {

ar.Event_ID__c = eID;


} else {

// ? stop execution and do not continue.





Thanks for your help.

To accomplish this behavior, just simply do not contain an "else" logic. In your trigger, if the code doesn't go into the IF statement, the trigger will skip the if statement and complete (therefore stopping and exiting). There is no explicit "stop" or "exit" method in Apex, but just make sure to not apply any logic for the scenario you want to skip/ignore.

If you are inside of a loop, and you want to abandon processing for just the current record, use the continue method. It will break you out of the loop for the current record, and start processing again at the next record.  One caveat I would warn of, if you are looping on a list of lists, "continue" or "break" will cause you to drop to the next list of objects, not the next object in the list, so be careful of where in your logic these structures are called from.
Also, if you meant that the record should not get inserted or updated at all, not just with your apex code, you could use the adderror function.
you look for conditions to be a certain value, if they are not, you can use the adderror method to prevent the record from being inserted or updated.  Instead an error will appear in the UI, either at the top of the record or on the field in question, depending on how you use the code.

Message Edited by JimRae on 01-12-2009 04:22 PM
nice. Thanks for the advice and help.
I went ahead and used the "addError" method if the parameters are not met properly. This will work for what I'm trying to do.
Thanks for the help once again.