Salesforce Apex Trigger Best Practice

1) Write One Trigger per one Object You can have n number of triggers on single object, but as apex best practice we have  to write  event operations into the singe trigger with the if conditions. Example code like below if (Trigger.isInsert) { ..... ...... } if (Trigger.isUpdate) { ........ ........ } A single Apex [...]


Order of Execution – Salesforce

1) Loads the  record from the database or initializes the record for an upsert statement. 2) Loads the new record field values from the request and overwrites the old values with new values. If the request came from a standard User Interface edit page,  runs system validation to check the record for: -Checks whether Required [...]

Avoid recursive trigger in salesforce using static variable

Avoid recursive trigger in salesforce using static variable Avoid recursive trigger in salesforce using static variable Recursion occurs when same code is executed again and again. It can lead to infinite loop and which can result to governor limit sometime. Sometime it can also result in unexpected output. It is very common to have recursion [...]

Trigger for Auto Converting Leads

Trigger for Auto Converting Leads Requirement:- Webform__c is custom text field  on lead . And Whenever new lead is created with Webform__c=='free trail' then lead should be converted autmatically trigger AutoLeadConvert on Lead (after insert) { leadStatus convertStatus =[SELECT MasterLabel FROM LeadStatus WHERE isConverted = true Limit 1];  List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();  for(lead{    [...]

Lead Conversion Trigger Order of Execution

Lead Conversion Trigger Order of Execution On conversion of Lead following is the order of execution of triggers of Account, Opportunity, Contact and Lead: 1) Account Before Trigger 2) Account After Trigger 3) Opportunity Before Trigger 4) Opportunity After Trigger 5) Contact Before Trigger 6) Contact After Trigger 7) Lead Before Trigger 8) Lead After [...]

Roll Up Summary Triggers on LookUp Relationship

Generally we can create rollup summary fields in between Master_Detail relationship. if we want to create roll up summary  fields in between look up relation we need to  write a trigger. Requirement :- Custom field On contact :Salary__C Custom field on Account :Total_salary__C Count the sum of  salaries of contact with same AccountId  and the sum [...]