Thursday, November 8, 2018

Bulkified and Non-Bulkified Apex Code concepts in Salesforce.com

Non-Bulkified code:
It can not process large set of data and eventually can hit governor limit because it works on one record.

For example: It can process only one (first) account record from the Trigger.new collection, but if more than one account has initiated this trigger, then those additional records will not be processed.

trigger nonBulkifiedTrigger on Account (before insert, before update) {

   //It can get only first record from the collection


   Account acct = Trigger.new[0];
   List<Contact> contacts = [Select id, firstname, lastname, email 
                                                        From Contact 
                                                        Where accountId = :acct.Id];
   
}

Bulkified Code:
It can process large set of data and escape governor limit because it works on list of records.

For example: It can process all account record from the Trigger.new collection.

trigger bulkifiedTrigger on Account (before insert, before update) {

    //Loop through all records in the Trigger.new collection
    for(Account a: Trigger.new){

    //Concatenate the Name and billingState into the Description field
    a.Description = a.Name + ':' + a.BillingState
   }
}










No comments:

Post a Comment

Validation in flow input text field in sfdc: Maximum 255 characters limit

The input text field validate in sfdc flow behaves opposite of the generic sfdc validation rule.  Here the validation formula is evaluating ...