Apex New Version of Questions
Apex New Version of Questions
Apex New Version of Questions
Apex Triggers
2. What Are The Two Types Of Triggers In Salesforce?
There are two types of triggers in Salesforce.
Before triggers are used to perform a task before a
record is inserted or updated or deleted in Salesforce.
These are used to update or validate record values before
they are saved to the database.
After triggers are used if we want to use the information
set by Salesforce system and to make changes in the
other records. The records that fire the after trigger are
read-only.
3. What Is The Use Of Trigger Class In Salesforce?
Use the Trigger class to access run-time context
information in a trigger, such as the type of trigger or the list
of sObject records that the trigger operates on.
4. What Are Different Events Available In Triggers?
An Apex trigger is a set of the statement which can be
executed on the following events. We can add the below
events with comma-separated. Here is a list of trigger events
in Salesforce.
Trigger Events
5. When We Should Use Trigger Or Automation?
Use Salesforce flow for most of the automation in salesforce
and use Apex Trigger for complex logic which can’t be done
using Flow.
6. Best Practice And Consideration For Trigger?
Here is a list of all apex trigger best practices that we should
while creating the trigger in Salesforce.
1. One Trigger Per Object
2. Logic-less Triggers
3. Context-Specific Handler Method
4. Bulkify your Code
5. Avoid SOQL Queries or DML statements inside FOR
Loops
6. Using Collections, Streamlining Queries, and
Efficient For Loops
7. Querying Large Data Sets
8. Use @future Appropriately
9. Avoid Hardcoding IDs
7. How Many Times Trigger Execute On An Upsert Event
Upsert trigger fires on 4 different events :- before(insert,
update), after (insert, update)
8. How Many Times Trigger Execute On An Merge Event
Merge triggers are fired on both events on the delete.
9. Order Of Execution For Trigger
Check out our Order of execution in the Salesforce post on the
same.
10. When You Will Choose Before The Event And When
You Will Choose After The Event?
Use before the event to update the record which executes the
Apex Trigger. Use After the event to use related or child
records.
11. What Is The Difference Between Trigger.New And
Trigger.NewMap?
Trigger.New return the list of SObject which invoke the
trigger. Trigger.newMap return the Map of Id and SObject.
12. When We Should Use Trigger.Old?
It is always good and recommended to check the old and new
values before making any updates in Trigger. You can use
Trigger.Old to check the old value of the record. It can help
you to stop recursion in Trigger.
13. How To Void Recursion In Trigger?
There are different way to stop the recursion in Trigger
1. Use Static Boolean Variable: Create a class with a static
Boolean variable with a default value of true.
o This is good for less than 200 records.
What is a Trigger?
Apex triggers enable you to perform custom actions before or
after events to records in Salesforce, such as insertions,
updates, or deletions. Just like database systems support
triggers, Apex provides trigger support for managing records.
before insert
before update
before delete
after insert
after update
after delete
after undelete
What are different type of Triggers?
There are two types of triggers:
operations.
If we will not follow above point we may hit governor limit
when records are created/updated/deleted in mass using data
loader or other tool.
available in once insert, before and once the update and once
undelete.
How is Trigger.new totally different from Trigger.old?
WHAT IS TRIGGER?
complex.
Benefits of using Trigger Framework/Patterns.
need/requirements.
o Reusable code.
o Easy to Maintain.
DIAGRAM:
APEX TRIGGER:
routing purpose.
Keep Triggers Simple and easy to understand
Sharing Re-calculation
Better Scalability
limits.
The system itself divides the number of batches for
records
If one batch fails, the other batches will continue to be
The trigger should not add more batch jobs than the limit
Methods declared as future aren’t allowed in classes that
Apex class.
All methods in the class must be defined
as global or public.
Minimize web service callout times.
availability.
Future methods are not allowed.
Queueable Apex
Queueable Apex can be used to run processes that will take a
long time, such as extensive database operations or external
web service callouts. Queueable Apex is a more advanced
and enhanced version of the future method with a few added
features which are mentioned below
1. Chaining jobs
2. Getting an Id for a job
3. Using non-primitive types
Limitations of Queueable Apex
Queueable can’t handle millions of records in one job.
Only one job can be scheduled at a time.
background
Best suitable for long-running processes as it will create