Wednesday 16 January 2019

Trigger

1. What is trigger?
Apex can be invoked by using triggers.
Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.

2. What are different types of Triggers in sfdc?
A trigger is Apex code that executes before or after the following types of operations:
insert.
update.
delete.
merge.
upsert.
undelete

3. What are Trigger Context variable? 
Variable Usage
isExecuting Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsert :         Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdate : Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDelete :       Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBefore  :      Returns true if this trigger was fired before any record was saved.
isAfter : Returns true if this trigger was fired after all records were saved.
isUndelete : Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
new : Returns a list of the new versions of the sObject records.
This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.
newMap : A map of IDs to the new versions of the sObject records.
This map is only available in before update, after insert, after update, and after undelete triggers.
old : Returns a list of the old versions of the sObject records.
This sObject list is only available in update and delete triggers.
oldMap : A map of IDs to the old versions of the sObject records.
This map is only available in update and delete triggers.
size : The total number of records in a trigger invocation, both old and new.

4. What is the difference between Trigger. New and :Trigger.NewMap
newMap A map of IDs to the new versions of the sObject records.
This map is only available in before update, after insert, after update, and after undelete triggers.
new Returns a list of the new versions of the sObject records.
This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.

5. Can we call the Batchapex from the trigger?
1.As Daniel has said, you can only have five batch job running so calling batch from a trigger is almost certainly not a good idea.
These answers are somewhat valid, but since you can query the state of a running batch you can ensure that your batch is a singleton
2. @future is also possible

A batch apex can be called from a class as well as from trigger code.
In your Trigger code something like below :-
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);

6. What are the problems you have encountered when calling batch apex from the trigger.
future method call that performs more processing on the same record(s).
The issue is that this entire process becomes recursive in nature and you receive the error "System.AsyncException: Future method cannot be called from a future method.

7. Can we call the callouts from triggers?
yes we can. It is same as usual class method calling from trigger.
The only difference being the method should always be asynchronous with @future

8. What are the problems that you encountered while calling apex callouts in triggers.
 Too many Future calls".

7. What is the recursive triggers?
Recursion occurs in trigger if your trigger has a same DML statement and the same dml condition is used in trigger firing condition on the same object(on which trigger has been written)

8. What is bulkifying triggers?
By default every trigger is a bulk trigger which is used to process the multiple records at a time as a batch. For each batch of 200 records.

9. What is the use of future methods in triggers?
Using @Future annotation we can convert the Trigger into a Asynchrinous Class and we can use a Callout method.

10. What is the order of executing the trigger apex?
1. Executes all before triggers.
2. Validation rules.
3. Executes all after triggers.
4. Executes assignment rules.
5. Executes auto-response rules.
6. Executes workflow rules.
7. If there are workflow field updates, updates the record again.
8. If the record was updated with workflow field updates, fires before and after triggers one more time. Custom validation rules are not run again.
9. Executes escalation rules.
10. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure.
11. If the parent record is updated, and a grand-parent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Grand-parent record goes through save procedure.
12. Executes Criteria Based Sharing evaluation.
13. Commits all DML operations to the database.
14. Executes post-commit logic. Ex: Sending email.

11. What is trigger handler?

Trigger:
Trigger:

    trigger AccountTrigger on Account(before insert, before update) 
    {
        AccountTriggerHandler handler = new  AccountTriggerHandler();
       
        handler.updateUser(Trigger.new);  // Call method for update account field.

    }
Trigger Handler
    public class AccountTriggerHandler
    {
           public void updateUser(List<Account> accList)
           {
                  Set<Id> masterIds = new Set<Id>();
                 
                  for(Account accObj : accList)
                  {
                    if(accObj.Master__c != null)
                       masterIds.add(accObj.Master__c);
                  }
           
                    Map<Id,Master__c> masterMap = new Map<Id,Master__c>([Select Id, test_user1__c from Master__c where Id IN : masterIds]);
                   
                   
                  for(Account acct : accList)
                  {
           
                    if(masterMap.containsKey(acct.Master__c))
                    {
                       Master__c masterObj = masterMap.get(acct.Master__c);
                 
                        if(acct.test_user1__c == null)
               
                           acct.test_user1__c = masterObj.test_user1__c;
                    }
                  }
           }
    }

12. How do you avoid-recursive triggers?
Use a static variable in an Apex class to avoid an infinite loop.
Static variables are local to the context of a Web request.

13. HOW many triggers we can define on a object?
We can write more than one trigger But it is not recommended .
Best practice is One trigger On One object.

14. How many time workflow filed update will be called in triggers?
If the record was updated with workflow field updates, fires before and after triggers one more time (and only one more time).
Note:The before and after triggers fire one more time only if something needs to be updated.
If the fields have already been set to a value, the triggers are not fired again.

No comments:

Post a Comment

Batch Apex

1. What are transaction limits in apex? Total number of SOQL queries issued1 - 100 Total number of records retrieved by SOQL queries - 50...