Contents
When does a trigger execute in apex business?
A trigger executes before and after an event occurs on record. Following are the events on which we can fir the trigger − Suppose we received a business requirement that we need to create an Invoice Record when Customer’s ‘Customer Status’ field changes to Active from Inactive.
Which is the context variable in apex triggers?
There are other context variables which are available in the context – trigger.old, trigger.newMap, trigger.OldMap. The above trigger will execute when there is an update operation on the Customer records.
When to insert an invoice record in apex?
Suppose, the invoice record needs to be inserted only when the Customer Status changes from Inactive to Active and not every time; for this, we can use another context variable trigger.oldMap which will store the key as record id and the value as old record values.
Is it good to have handler class in apex?
Also keep in mind that while working on the Apex trigger, it’s good to have that helper/handler class so we can call it from the trigger. Having handler/helper classes will help make trigger code reusable as well more manageable. One more thing to avoid: hardcoding Salesforce ID in a class or trigger.
What are the best practices for Apex code?
Store important details, including which class and line has thrown an error, to make necessary debugging easier. Another important best practice surrounding Apex Code is the need to follow a proper naming convention. For example, if we are writing a trigger on a case object, then the trigger name should be CaseTrigger.
When to use flow control statement in apex?
Flow control statement is beneficial when you want to execute some lines of the code based on some conditions. The above snippet of code is querying account records from the database and checking the list size. DML stands for data manipulation language. DML statements are used to manipulate data in the Salesforce database.