anusha(salesforce developer)

Triggers

1) What the are events of triggers?
- After insert,After Update & After Delete


Why Use Triggers? 

Triggers are used to perform immediate actions based on previous action e.g. field update. 

An apex language is used to write triggers
Eg: Account merges

2) Upsert trigger can call which all events?
-After Update & After Insert

3) What is merge operation?

4) Which events will be called during merger operation?
- Ans Delete n Update

5) In After Undelete, we cannot see trigger.old, only trigger.new can be seen.True or False?
       -TRUE

6) Static variable declarion in trigger is possible,True or false?
     -TRUE but it will not work.

7) In After update trigger event to Save a record do we need DML statement?
- Yes

8) In before insert trigger event can we have trigger.new and trigger.newmap?
- we can only have trigger.new, Trigger.newmap is not possible because still the record is not inserted             and record Id is not generated.

9) Can we have batch/asynchronous method called from a Trigger?
- YES

10) Can we manually call triggers?
- No triggers cannot be called manually. It will automatically called for Insert/delete/update DML         
          operations only.

11) Can we write trigger for Profile object?
- No, generally there will be no scenarios to call trigger on Profile Object.

12) Will trigger fire after Workflow rule?
-Only Update triggers will fire once.

13) You have picklist field with 3 values :New, Latest,Old. Now you fire a trigger which will update the picklist field with 'Deleted' value for a record. Is this possible?
- YES

14) Can you update formula field through Trigger?
- NO

15) I need to update Trigger.Old records how can i do this?
- This is not possible because Trigger.Old is read only list.

16) Can we display Validation Error messages in After events of a Trigger?
- We Can display Error messages in After events, but there will be no purpose serverd. Because after inserting/Updating the record the error message will be dispalyed.

17)Consider a User logsin by a profile which does not have eidt access to Account object. But he/she tries insert a record to Account object, which in turns fires a trigger which updates  record in the Account Object.
Will the Trigger Update the Record?

- Yes the Trigger can update the Record because Trigger always runs is System context.


To capture the runtime information we use trigger context variables.
Below context variables will return either true or false.
  1. Trigger.isBefore (returns true if the trigger context is Before Mode)
  2. Trigger.isAfter (returns true if the trigger context is After Mode)
  3. Trigger.isInsert (returns true if the trigger context is Insert)
  4. Trigger.isUpdate (returns true if the trigger context is Update)
  5. Trigger.isDelete (returns true if the trigger context is Delete)
  6. Trigger.isUndelete (returns true if the trigger context is Undelete)
  7. Trigger.isExecuting (returns true if the apex class method is getting call from Apex Trigger)
Below context variables will store records at runtime.
  1. trigger.old (stores history (old versions) of the records.)
  2. trigger.oldMap (stores history (old versions) of the records along with id.)
  3. trigger.new (stores new version of the records.)
  4. trigger.newMap (stores new version of the records along with id.)

It will throw the validation error because after the workflow field update before triggers fire one more time.
Read/Write access over the trigger collections on different events -
Eventstrigger.oldtrigger.oldMaptrigger.newtrigger.newMap
before insertNANARead/WriteNA
after insertNANARead OnlyRead Only
before updateRead OnlyRead OnlyRead/WriteRead Only
after updateRead OnlyRead OnlyRead OnlyRead Only
before deleteRead OnlyRead OnlyRead OnlyRead Only
after deleteRead OnlyRead OnlyRead OnlyRead Only
after undeleteRead OnlyRead OnlyRead OnlyRead Only

Apex Trigger Collections availability for the different events -
Eventstrigger.oldtrigger.oldMaptrigger.newtrigger.newMap
before insert
after insert
before update
after update
before delete
after delete
after undelete
Validation rules will fire first then workflow rules will fire. So, the answer is 100 (Even though there is a validation rule because of the workflow rule it will accept 100 in the amount field).


Relationship between ChildObject__c and ParentObject__c objects is Lookup relationship.
In case of lookup relationship if we delete the parent object record in child object only the relationship field value will be removed (child records won't be deleted).
But client has a requirement to delete the child object records. How to achieve this?

Write an apex trigger to achieve this functionality.
We should take before delete event. If we take after delete relationship will broke up b/w parent and child object records.
Trigger ParentTrigger on ParentObject__c(before delete) {
 /*
 Note:
 - For delete events only trigger.old and triggger.oldMap will be available.
 - trigger.old holds old versions of the records but if we mention that in SOQL query salesforce will automatically convert those records into ids.
 */
 List<childobject__c> childLst = [select id, Parent_Object__c from ChildObject__c where Parent_Object__c in: trigger.old];  
 delete childLst;
}
 </childobject__c>



We are creating validation rules, workflow rules, assignment rules, auto-responsive rules, escalation ruels and apex triggers etc..
If the condition is same for all the rules which will execute first and which will execute next, for this salesforce provide the order.
Order of execution in salesforce - Order of execution in Salesforce:
  1. Prepare the record for insert/update operation.
  2. It will replace all the old field values with new field values.
  3. If the request is coming form UI all the system validations will execute -
    • DataType
    • Length
    • Required
    • unique
    • pageLayot level validations
  4. before triggers
  5. Custom Validations and again system validation rules will fire (pageLayot level validations won't fire at this point of time).
  6. record will be saved into the database but doesn't not commit.
  7. after triggers.
  8. assignment rules.
  9. auto-responsive rules.
  10. Workflow Rules
  11. incase of Workflow Rule Field updates again before triggers and after triggers fire one more time. System validation ruels for duplicate chcek.
  12. Escalatoin Rules.
  13. Rollup-Summary fields will be calculated.
  14. Grant parent Rollup-Summary fields will be calculated.
  15. Criteria base sharing evaluation.
  16. Record will be commited into the database.
  17. Post Commit logic (Sending emails).

No comments:

Post a Comment