UrbanPro
true

Learn Salesforce Developer from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Trigger Best Practices

R
Rajiv Kumar Singh
27/04/2017 1 0

Triggers - Best Practices in Salesforce

 
1. Future methods, SOQL and DML:

    Avoid writing Future methods, SOQL and DML inside the "For" loop.

2. Bulkify the trigger:

    Start developing the logic for the bulk of records getting inserted or updated or deleted. The trigger will be invoked when we insert bulk of records from any data loading tools or through Web services. So, we should not concentrate on 1 record, we have to concentrate on the bulk of records.

3. Larger sets of records:

    Use SOQL in For loop, to avoid 50001 limit error.

Account[] accts = [SELECT id FROM account];

Exception will be thrown, if there are more than 50000 records.

for (List<Account> acct : [SELECT id, name FROM account WHERE name LIKE 'Test']) {

    // Your logic here

    update acct;
}

The Force.com platform chunk your large query results into batches of 200 records by using this syntax where the SOQL query is in the for loop definition, and then handle the individual datasets in the for loop logic.

4. Make use of the Limits Apex Methods to check whether we are nearing Governor Limits.

Number of SOQL Queries allowed in this Apex code context - Limits.getLimitQueries()

Number of records that can be queried  in this Apex code context - Limits.getLimitDmlRows()

Number of DML statements allowed in this Apex code context - Limits.getLimitDmlStatements()

Number of CPU usage time (in ms) allowed in this Apex code context - Limits.getLimitCpuTime()

5. Never hardcode SFDC record ids.

Cheers!!!
1 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

When To Use Before Triggers And When To Use After Triggers?
1. Before Triggers: To perform the validations we should use before triggers. If you are updating any field on the same object on which you are writing the trigger and no need to explicitly include the...

Salesforce Job based training
Hi All, Who are genuinely searching for a Salesforce job-based training? , That Job-based training will be provided here. 1.We will provide Salesforce Best Realtime Training 2.We will provide...

Assigning One permission to multiple users automatically without any manual work.
Hi Friends,I recently came across below new things I did in my project. Hope it helps you too.Description :How to Assign one permission set to multiple Users ? For example: If I want to assign ONE permission...

What is SFDC Architecture.
SFDC is built based on the MVC Pattern.Model - Which is handle the Database Objects View - Which will take care of the UI pages. Controllers- The business logic Implementation will be done here.

Following Custom Objects Are Available In The Organization: Interview Question For Salesforce
ChildObject__c ParentObject__c Click to see the full question. Relationship between ChildObject__c and ParentObject__c objects is Lookup relationship. In case of lookup relationship if we delete the...
X

Looking for Salesforce Developer Classes?

The best tutors for Salesforce Developer Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Salesforce Developer with the Best Tutors

The best Tutors for Salesforce Developer Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more