2

I wrote this trigger to change the lead owner to the user on the zip code custom object record whenever the postal code on the lead has the postal code that matches an entry for the custom object zip code , Basically it the trigger checks the value of postal code on the lead record and if it is not null then it changes the lead owner to the owner of the zip code record , Zip code object has a custom user field ,

This code is working perfectly for small amount of records but when I am updating large no of records via dataloader this code give too many soql 101 errors.

I guess it need to be bulkified. could anyone please guide me with the correct problem and fixing that. Below is the code -

Trigger Changeleadowner on Lead (before insert, before update)
{List<Lead> leadsToUpdate = new List<Lead>();
for (Lead lead : Trigger.new)
  if (l.PostalCode != NULL)   
      {// Find the sales rep for the current zipcode
       List<Zip_Codes__c> zip = [select user__c from Zip_codes__c where
 Name = :lead.PostalCode limit 1];     

          // if you found one
          if (zip.size() > 0)x
          {   //assign the lead owner to the zip code owner
              lead.OwnerId = zip[0].User__c;leadsToUpdate.add(lead);

          }
       }
    }
}

Below is the code that i am trying to bulkify

trigger Changeowneronlead on Lead (before insert, before update) {


    Set<id> leadpostalcode = new Set<id>();
    for (lead a : Trigger.new)
    {if (a.PostalCode != NULL)
        leadpostalcode.add(a.postalcode); }  

     Map<string, zip_codes__c> zipcodes = new Map<string, zip_codes__c>([select user__c  from Zip_codes__c Where name IN :leadpostalcode]);  

    // iterate over the list of records being processed in the trigger and
    // set the color before being inserted or updated
    for (Lead a : Trigger.new)
        a.ownerid= zipcodes.get(a.OwnerId).User__c;

}

With the above code i am running into the below error :

unexpected exception, contact your administrator: AddOwnerColor: execution of BeforeInsert caused by: System.StringException: Invalid id: 12345: External entry point

could anyone please guide me with the correct problem and fixing that.

1

Make practice to use collection to avoid queries in for loop. You have query in for loop:- List<Zip_Codes__c> zip = [select user__c from Zip_codes__c where Name = :lead.PostalCode limit 1];

Instead you can create collection and use as:

List<Lead> leadsToUpdate = new List<Lead>();
Set<String> postalCodes = new Set<String>();

for (Lead lead : Trigger.new)
{
   if(l.PostalCode != null)
       postalCodes.add(l.PostalCode);
}

Map<String,Id> postalUserMap = new Map<String,Id>();
for(Zip_Codes__c zip = [SELECT Name, User__c from Zip_codes__c 
                        WHERE Name IN: postalCodes];     
{
       // Create Map to use
       postalUserMap.put(zip.Name, zip.User__c)
}

for (Lead lead : Trigger.new)
{
      if (l.PostalCode != NULL)   
      {
         // Find the sales rep for the current zipcode
         // if you found one
         if (postalCodes.contains(l.PostalCode))
         {    
              //assign the lead owner to the zip code owner
              // get owner from Map
              lead.OwnerId = postalUserMap.get(l.PostalCode);
              leadsToUpdate.add(lead);
         }
}

Above example is for direction only but would work as well but not tested. Same way you can resolve other issues and avoid SOQL query using collections. Use appropriate checks to avoid other errors.

System.StringException is because you are assigning a number/text value to a field which is a LookUp type field.

6
  • Thank you Ashwani for the detailed response , Thank you very much :)
    – Natasha
    Mar 15 '16 at 10:05
  • While testing the code i see Invalid type: postalUserMap , i am not sure what that means . Below is the code i tweaked : for (Lead lead : Trigger.new) { if(l.PostalCode != null) postalCodes.add(l.PostalCode); } Map<String,Id> postalUserMap = new postalUser<String,Id>(); for(Zip_Codes__c zip : [SELECT Name, User__c from Zip_codes__c WHERE Name IN: postalCodes]) { // Create Map to use postalUserMap.put(zip.Name, zip.User__c); } This returns the error Invalid type: postalUser at line 11 column 36
    – Natasha
    Mar 15 '16 at 10:07
  • @Natasha because there is type postalUserMap<String,Id>(); should be Map<String,Id>();. That's all. I would recommend to understand the approach we used, instead of just copying code.
    – Ashwani
    Mar 15 '16 at 10:15
  • This works like a charm , I was able to perform a bulk insert , Thank you ever so much Ashwani , Also i am pretty new to coding , trying to grasp the concepts slowly .
    – Natasha
    Mar 15 '16 at 10:41
  • Much appreciate your help on this one . Thanks a ton !
    – Natasha
    Mar 15 '16 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.