Skip to main content
So i have been trying to bulkify my code for the trigger, but i keep falling short . In short is there a way to go through 2 large list and create a map based on if the 2 list have similar id's. This is what i have so far? My goal is to add multiple contacts to thier given account and append them to 1 field in accounts.

 

trigger MPAccountConcat on Account(before update) {

 

   List<Account> myAccount = [select id from account ORDER BY id asc];

 

List<Contact> myContact = [select accountid, name from contact ORDER BY accountid asc];

 

Map<Id, String> mapper = new Map<Id, String>();

 

    String temp = '';

 

  for(Account check: myAccount){  

 

temp = '';

 

     for (Contact j: myContact) {

 

       

 

            if (check.id == j.accountid) {

 

                temp += j.name + '/ ';

 

             

 

               

 

           }

 

           mapper.put(j.accountid, temp);

 

         

 

           }

 

   

 

}

 

try{

 

    for (Account checker: Trigger.new ) {

 

                checker.Main_Points_of_Contact__c = mapper.get(checker.id);

 

          

 

    }

 

   

 

}catch(Exception e){

 

     System.debug('An error happened, as predicted!');

 

    }

 

}
4 risposte
  1. 18 dic 2013, 17:16
    Say for example I want to update the Name [ like this - Contact of <Name of the Account> . For eg: Contact of Salesforce.com or say Contact of Dell.com ] of all the Related Contacts of an Account as and when the Name of an Account is changed. So I will have an After Update Trigger like this:

     

    trigger trgr_UpdateContacts on Account( after Update){

     

     

        List< Account > lstAccounts =

        [

     

            SELECT

     

                Id,

     

                Name,

     

                (SELECT Id,LastName FROM Contacts)

     

            FROM

     

                Account

     

            WHERE

     

                Id IN: trigger. newMap . keySet ()

     

               

     

        ];

       

     

        List< Contact > contactsToUpdate = new List< Contact >();

     

        for(Account acc : lstAccounts){

     

            for(Contact con : acc.Contacts){

     

                contactsToUpdate.add(

     

                    new Contact(

     

                        Id = con.Id,

     

                        LastName = 'Contact of Account - ' + acc.Name

     

                    )

     

                );

     

            }   

     

        }

     

     

        if(contactsToUpdate.size()>0)

     

            UPDATE contactsToUpdate;

     

    }

     

    Now what are the steps take to Bulkify -

     

     

    If you look at the SOQL, it has a  sub-soql or an inner soql that says - (SELECT Id , LastName FROM Contacts). The benefit of writing like this, is that we can get all the related child records and they can be referenced like - acc. Contacts . The Force.com SOQL Engine will intelligently identify the related child records for us we need not run a seperate SOQL to get the Contact records.

     

     

    If you look at the loops, there is an  outer for loop that iterates over the Account records. As and when each Account record is iterated, the related Contact records are referenced via - acc. Contacts . You can then iterate over each Contact record and then update its Name and then add the same to a new List named -  contactsToUpdate .

     

     

    And finally run an UPDATE on that List. #Bulkified. Is your requirement close ?
0/9000