Frequently asked trigger interview questions:
1. Explain Trigger and trigger types?
A trigger is a special kind of a store procedure that executes in response to certain action performed on
the table like insertion, deletion or updating of data
Basically there are two logical types
a. DDL triggers
b. DML triggers
DDL triggers you can categorize:
1. Transact-SQL DDL Trigger
2. CLR DDL Trigger
DML trigger types are:
1. INSTEAD OF DML trigger
2. AFTER DML trigger
There is one special type trigger called logon trigger in sql server.
2. What are DDL Triggers and types of DDL trigger?
DDL triggers fire in response to an event happening on the server. However, DDL triggers do not fire in
response to UPDATE, INSERT, or DELETE (DML) statements on a table or view. Instead, they fire in
response to Data Definition Language (DDL) statements that start with the keywords CREATE, ALTER,
and DROP.
DDL triggers you can categorize:
1. Transact-SQL DDL Trigger
2. CLR DDL Trigger
3. What are DML triggers and types of DML triggers?
DDL triggers fire in response to UPDATE, INSERT, or DELETE (DML) statements on a table or view.
DML trigger types are:
1. INSTEAD OF DML trigger
2. AFTER DML trigger
4. What is Transact-SQL DDL Trigger?
A special type of Transact-SQL stored procedure that executes one or more Transact-SQL statements in
response to a server-scoped or database-scoped event. For example, a DDL Trigger may fire if a
statement such as ALTER SERVER CONFIGURATION is executed or if a table is deleted by using DROP
TABLE.
5. What is CLR DDL Trigger?
Instead of executing a Transact-SQL stored procedure, a CLR trigger executes one or more methods
written in managed code that are members of an assembly created in the .NET Framework and
uploaded in SQL Server.
Example:
CREATE TRIGGER safety
ON DATABASE
FOR DROP_TABLE , ALTER_TABLE
AS
PRINT 'You must disable Trigger "safety" to drop or alter tables!'
ROLLBACK ;
6. What is AFTER DML trigger?
AFTER triggers are executed after the action of the INSERT, UPDATE, MERGE, or DELETE statement is
performed successfully.
7. What is INSTEAD OF DML trigger?
DML trigger is executed instead of the triggering SQL statement, therefore, overriding the actions of the
triggering statements. Therefore, they can be used to perform error or value checking on one or more
columns and the perform additional actions before insert, updating or deleting the row or rows.
8. What is logon trigger?
Special type of trigger fire when LOGON event of Sql Server is raised is called lo logon trigger. You can
use this trigger to audit Sql server activities, control server sessions, such as to track login activity or limit
the number of sessions for a specific login.
9. Why the Trigger Fires Multiple Times in Single Login?
Reason multiple SQL Server services are running as well as intelligence is turned on.
10. Is it possible to create trigger on views?
Yes, only InsteadOf trigger.
11. How many triggers are possible per table?
One InsteadOfTrigger and any After Triggers.
12. When multiple after triggers are attached to sql table, how to control the order of execution?
Using sp_settriggerorder procedure.
13. What is the difference between For Trigger and after trigger?
It’s same.
QUESTIONS AND ANSWERS
Triggers
========
1.what is an workflow?
Ans:- Workflow Rules are automated process to send E-mails alerts,
assign tasks, update fields on trigger criteria based requirements.
2.When will the Workflows invoke?
Ans- Workflows are invoked
when ever the given W/F criteria is met during the DML operations(Insert (or) update) on particular
object
3. What is a trigger?
Ans:- Triggers are the automated actions when ever any DML operation is performed on particular
object
4.what are the differences between Workflow and triggers?4
Ans:-
Triggers Workflows
1. You can perform all DML operatios 1. You can perform only update operation, you cannot perform
insert ,
(insert,update,delete,undelete) delete and undelete operations
2. You can perform any logic on any 2.You can perform update operation on
object in entire organization only that particular object and corresponding child object
5.what are the Triggers Events?
Ans:- There are seven trigger events ,this events will specify when the trigger has to be fired
1.Before insert
2.Before update
3.Before delete
4.After insert
5.After update
6.After delete
7.After undelete
6. What are the Trigger Context varibles?
Ans:- These are the varibles where the data will stored in triggers
1.Trigger.New
2.Trigger.Old
3.Trigger.NewMap
4.Trigger.OldMap
5.Trigger.isBefore
6.Trigger.isAfter
7.Trigger.isInsert
8.Trigger.isUpdate
9.Trigger.isdelete
10.Trigger.isundelete
7.If we insert 100 records how many times will Trigger fire?
Ans:- The Trigger will fire only once but the logic written in the trigger will run 100 times (i.e, on each
record once)
8.what is bulk Trigger?
Ans:- All triggers are bulk triggers by default, and can process multiple records at a time.
You should always plan on processing more than one record at a time.
An Event object that is defined as recurring is not processed in bulk for insert, delete, or update triggers.
9.what is the difference between Trigger.New and Trigger.Old in update Triggers?
Ans:- Before Update
Trigger.New:- It contais the records which you are going to update with the modified values
We can perform Read and Write operation
Trigger.Old:- It contais the recors which you are going to modify with their old values
we can perform read and SOQL
After Update
Trigger.New:- It contais the records which you are going to update with the modified values
We can perform Read ,SOQL and DML
Trigger.Old:- It contais the recors which you are going to modify with their old values
we can perform only read operation
10.what is the difference between Trigger.New and Trigger.NewMap in update Triggers?
Ans:- Trigger.New:-It contais the records which you are going to update with the modified values
Trigger.NewMap:-It contais Map<Id,Sobject> where "ID" of the records as key and records as "VALUE";
11.when you can perform operation using Before Trigger and After Trigger which one you prefer?
Ans:- Before Trigger.
Because if we want to perform any modifications on dats in after Trigger we have to perform DML
operations on data
where as in Before trigger we can perform write operation on data(Bcz data is not yet stored) which will
not considere as DML .
12.Can we write SOQL query to fetch the data in Trigger.New in BeforeTrigger?
Ans:- NO, we cannot write SOQL query on Trigger.New in Before Triggers because the data is
not yet saved to Sobject.
F4c1
NOTE:-we will write SOQL queries to fetch the data was stored in Sobject.
13.Can we write DML on List of records which are in Trigger.New in BeforeTrigger?
Ans:- NO, we cannot write DML operations on records which are Trigger.New because the data is not yet
saved to Sobject.
NOTE:-we can only write DML operations on those records which were saved in Sobject.
14.what are Resursive triggers?
Ans:-If any trigger calls itself then such triggers are called Recursive Triggers.
15. How to handle Recursive triggers?
Ans:- We can handle these recursive Triggers using "Static Boolean".
Example:
public class TriggerHandler{
public Static Boolean flag=true;
public Static void show(){
if(flag==true){
flag=false;
Account a = new Account(Name='sameer');
insert a;
16.How many Triggers we can write on same object with same Trigger event ?
Ans:-yes
17.What is the order of execution of triggers?
Ans:- There is no order of execution of Triggers we cant say which will fire in which sequence.
18.Will field update on the workflow calls the Upadte Trigger?4
Ans:-Yes on the WorkFlow update the Brfore And After Update Triggers will fire.
19.Can we call batchApex from Trigger?
Ans:-Yes you can,
In your Trigger code something like below :-
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);
My only concern is that the Trigger would fire on the DML events and that would invoke the Batch.
20.Can we call schedule apex from Trigger?
Ans:-yes we can call
21.Can we call future methods from Triggers?
Ans:-yes we can call future methods from Triggers,future methods are used in Triggers to avoid mixed
DML exception
22.Can we send Emails using Triggers?
Ans:-Yes we can send Emails using Apex Triggers.
23.What are the good Pratices that you have to mantain while writing Triggers?
Ans:- 1.It has to obey all the Governing Limits
2.While invoking Batch Apex we should see that not more than 5 batch jobs are invoked.
3.we should not write SOQL queries within a loop
4.we should not write DML operations within a Loop
5.At max try to write only one Trigger per Object .(By number of trigger within the
Trigger by using if conditions)
24.How to disable the trigger for a Particular User?
Ans:- By using the CustomSettings (Hirearchy)
25.Are the Triggers are fired while loading the data using DataLoader?
Ans:- yes Triggers will fire while loading the data from Data loader (Dpends on how many batchs in data
loader while loading data).
26.How run the Triggers only when data comes from External System?
Ans:-