• BobbyQ
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies
Hello, I am getting the following error message. This is my first Apex Trigger and would really appreciate some help. The trigger looks up the lead postalcode in a custom object of zip codes and territory information and populates a lookup field on the lead with the record id of the corresponding zip code in the custom object.
System.LimitException: Too many SOQL queries: 101

Are there any changes I can make to elimate this error? Here is the code:
1trigger LeadAssignmentTrigger on Lead (before insert, before update)
2{
3    List<Lead> leadsToUpdate = new List<Lead>();3
4
5    for (Lead lead : Trigger.new)
6    {    
7      if (lead.PostalCode != NULL)
8      {
9          // Find the sales rep for the current zip code
10          List<Territory_Zip_Codes__c> zip = [select id from Territory_Zip_Codes__c
11                             where Name = :lead.PostalCode limit 1];     
12               
13          // if you found one
14          if (zip.size() > 0)
15          {   
16              //assign the lead owner to the zip code owner
17              lead.Territory_Zip_Code__c = zip[0].id;
18         
19              leadsToUpdate.add(lead);
20         
21          }
22       }
23    }
24}
  • December 22, 2020
  • Like
  • 0
Hello, trying to piece together my first trigger and could use some help? I have a feeling it something simple I'm overlooking.

I setup a custom object 'Territory_Zip_Codes__c', where the name is 'Zip Code'. The Lead has a lookup field 'Territory_Zip_Code__c'. When 'postalcode' is entered or edited on the Lead I want to update the 'Territory_Zip_Code__c' lookup field on the lead. When I sub in a random text field on the Lead in place of 'Territory_Zip_Code__c', I can see it get updated with the proper ID from the custom object. But, when I point it back to the lookup field it gives this error when updating the record:
 
Territory: execution of BeforeUpdate caused by: System.StringException: Invalid id: ()
trigger Territory on Lead (before insert, before update) { 
    set<string> left5zips = new set<string>(); 
    for (lead l: trigger.New) {
        if (l.postalcode != null) {
            left5zips.add(l.postalcode);
        }else {
            l.Territory_Zip_Code__c='None';   
        }
    }

    //query the zip_code object to get the zipcode (Name) and ID from the zip code object 
    map<string,string> zMap=new map<string,string>(); 
    for(Territory_Zip_Codes__c z : [Select name, ID from Territory_Zip_Codes__c WHERE name IN :left5zips]) {
        zMap.put (z.name, z.ID);
    }

    for(lead l:trigger.new){ 
        if(l.postalcode != null){ 
            if(zmap.containskey(l.postalcode)){
               l.Territory_Zipcode__c= '';
                l.Territory_Zipcode__c= zmap.get(l.postalcode);
                if(l.Territory_Zipcode__c!= null && l.Territory_Zipcode__c!= ''){
                    l.Territory_Zipcode__c= l.Territory_Zipcode__c; 
                }
            }           
        }
    } 
}

 
  • December 22, 2020
  • Like
  • 0
Hello, I am getting the following error message. This is my first Apex Trigger and would really appreciate some help. The trigger looks up the lead postalcode in a custom object of zip codes and territory information and populates a lookup field on the lead with the record id of the corresponding zip code in the custom object.
System.LimitException: Too many SOQL queries: 101

Are there any changes I can make to elimate this error? Here is the code:
1trigger LeadAssignmentTrigger on Lead (before insert, before update)
2{
3    List<Lead> leadsToUpdate = new List<Lead>();3
4
5    for (Lead lead : Trigger.new)
6    {    
7      if (lead.PostalCode != NULL)
8      {
9          // Find the sales rep for the current zip code
10          List<Territory_Zip_Codes__c> zip = [select id from Territory_Zip_Codes__c
11                             where Name = :lead.PostalCode limit 1];     
12               
13          // if you found one
14          if (zip.size() > 0)
15          {   
16              //assign the lead owner to the zip code owner
17              lead.Territory_Zip_Code__c = zip[0].id;
18         
19              leadsToUpdate.add(lead);
20         
21          }
22       }
23    }
24}
  • December 22, 2020
  • Like
  • 0
Hello, trying to piece together my first trigger and could use some help? I have a feeling it something simple I'm overlooking.

I setup a custom object 'Territory_Zip_Codes__c', where the name is 'Zip Code'. The Lead has a lookup field 'Territory_Zip_Code__c'. When 'postalcode' is entered or edited on the Lead I want to update the 'Territory_Zip_Code__c' lookup field on the lead. When I sub in a random text field on the Lead in place of 'Territory_Zip_Code__c', I can see it get updated with the proper ID from the custom object. But, when I point it back to the lookup field it gives this error when updating the record:
 
Territory: execution of BeforeUpdate caused by: System.StringException: Invalid id: ()
trigger Territory on Lead (before insert, before update) { 
    set<string> left5zips = new set<string>(); 
    for (lead l: trigger.New) {
        if (l.postalcode != null) {
            left5zips.add(l.postalcode);
        }else {
            l.Territory_Zip_Code__c='None';   
        }
    }

    //query the zip_code object to get the zipcode (Name) and ID from the zip code object 
    map<string,string> zMap=new map<string,string>(); 
    for(Territory_Zip_Codes__c z : [Select name, ID from Territory_Zip_Codes__c WHERE name IN :left5zips]) {
        zMap.put (z.name, z.ID);
    }

    for(lead l:trigger.new){ 
        if(l.postalcode != null){ 
            if(zmap.containskey(l.postalcode)){
               l.Territory_Zipcode__c= '';
                l.Territory_Zipcode__c= zmap.get(l.postalcode);
                if(l.Territory_Zipcode__c!= null && l.Territory_Zipcode__c!= ''){
                    l.Territory_Zipcode__c= l.Territory_Zipcode__c; 
                }
            }           
        }
    } 
}

 
  • December 22, 2020
  • Like
  • 0