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
ssousanssousan 

Stop APEX code at a certain line

How can i stop APEX from executing the code at a certain line,

 

For example:

 

In C we have

exit(-2);

 

In Fortran we have

Stop

 

Whats the the equivalent command in APEX

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

There are only two "exit()" type functions in Apex Code: System.assert and governor limits. The former will stop a transaction dead in its tracks: System.assert(false). Note that any effects of the transaction, such as data being saved, will be rolled back. The latter method can be done like this: while(true) contact[] c = [select id from contact limit 1]. This method will blow the SOQL limit and cause a guaranteed failure. Note that both methods end in failure, not success, so while you can emulate "exit(-1)", you cannot simulate "exit(0)", as the transaction must naturally and fully run its course without an error.

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Use return; -> it will exit your program execution

sfdcfoxsfdcfox

There are only two "exit()" type functions in Apex Code: System.assert and governor limits. The former will stop a transaction dead in its tracks: System.assert(false). Note that any effects of the transaction, such as data being saved, will be rolled back. The latter method can be done like this: while(true) contact[] c = [select id from contact limit 1]. This method will blow the SOQL limit and cause a guaranteed failure. Note that both methods end in failure, not success, so while you can emulate "exit(-1)", you cannot simulate "exit(0)", as the transaction must naturally and fully run its course without an error.

This was selected as the best answer