Wednesday, 13 February 2013

Example for Bulk supported Trigger



  To write bulk safe triggers we need to understand and utilize sets and maps. Sets are used to isolate distinct records, while maps are name-value pairs that hold the query results retrievable by record id. for more details refer the Apex developer guide.

Example:
======

 trigger ChangeColor on Account (before insert, before update)
{
      // create a set of all the unique ownerIds
      Set<id> ownerIds = new Set<id>( );
      for (Account a : Trigger.new)
      {
           ownerIds.add(a.OwnerId);
      }
   
      // query for all the User records for the unique userIds
      Map<id, User> owners = new Map<id, User>([Select Favorite_Color__c from User Where Id in          
                                                      :ownerIds]);
     
      // iterate over the list of records being processed
      for (Account a : Trigger.new)
     {
           a.Owner_Favorite_Color__c = owners.get(a.OwnerId).Favorite_Color__c;
     }
}

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Salesforce is a cloud based CRM software. Today's most of the IT industry use this software for customer relationship management. To get more details about salesforce please refer this site.

    Regards..

    Salesforce Training

    ReplyDelete
  3. Excellent post!!! In this competitive market, customer relationship management plays a significant role in determining a business success. That too, cloud based CRM product offer more flexibility to business owners to main strong relationship with the consumers. Amazon Web Services Training in Chennai | AWS Training

    ReplyDelete
  4. Nice blog on Hadoop Technology!!! I agree with your points, Big Data Hadoop technology is ruling the whole organization to handle data in efficient manner. HDFS and Map reduce helps the developer in different ways to manage data.

    Regards:

    Hadoop Training in Chennai |
    Big Data Training in Chennai

    ReplyDelete
  5. Testing an application is become essential for any product to get an effective

    result. Your post help you to gain more info on Selenium Testing

    Selenium Training in chennai |
    Selenium Courses in Chennai

    ReplyDelete