function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jerry ClifftJerry Clifft 

Help with trigger on lead that adds data from custom object

So, here is my situation.

 

Standard object = Lead

Standard object = PostalCode

Standard Object Custom field = Sales Territory

 

Custom Object = Zipcode

Custom Object Field = ZipCode

Custom Object Field = Sales Territory

 

I am trying to write a trigger on the lead (before insert, before update) that takes the PostalCode on the lead, lookups the same Postalcode in my ZipCode custom object and inserts the value of Sales Territory into the lead.


How can I do this?

EguiEgui

Hi,

 

If I follow your path :

 

- Start from a Lead where to get the ZipCode

- Query the ZipCode into the PostalCode object

- And there, what is link with the Sales Territory object ?

Jerry ClifftJerry Clifft

Sounds good. I attempted to do this:

 

trigger LeadfindTLR on Lead (After Insert, After Update) {
 Set<Id> ZipCodeIds = new Set<Id>(); 
 Set<Id> LeadIds = new Set<Id>();

// On Lead take the zipcode and find that zipcode in the custom object Zipcodes then update the lead field "Territory" with data from the field Queue_ID custom object Zipcode.

       for (Lead L: Trigger.New){
         if (L.Name != null && L.PostalCode != null )
         LeadIds.add(L.Id);
         ZipCodeIds.add(L.PostalCode);
         }

         Lead L2 = [select id, Status from Lead where ID in :LeadIds];
         if(L2.Status != 'Closed'){
         Zipcodes__c z = [select Territory__c  from Zipcodes__c where Name in :ZipCodeIds];
           L2.Territoy__c = z.Territory__c;
         }
         Update L2;
        }

 

 

Now, SF let's me save the trigger, but when I create a new lead (postalcode = 78664), I receive the following error:

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger LeadfindTLR caused an unexpected exception, contact your administrator: LeadfindTLR: execution of AfterInsert caused by: System.StringException: Invalid id: 78664: External entry point

 

 I seem to have something out of whack....

 

EguiEgui
Pay attention not to perform Select ou update queries in a loop or you'll
run into governor limits (BULK).



Do this outside of the loop.



Regarding your error, seems like you're mistaking lead id and lead zip code.
Jerry ClifftJerry Clifft

I am not sure how to do that. Also I think I am doing something wrong with part of that trigger, as I get the error below:

 

Apex trigger LeadfindTLR caused an unexpected exception, contact your administrator: LeadfindTLR: execution of AfterInsert caused by: System.StringException: Invalid id: 78664: External entry point.

 

Not sure where to go from, any/all help is appreciated.

sushant sussushant sus
First create
map<string,string>maps=new map<string,string>();
list<lead>leadlist=newlist<lead>();

replace this line by
Lead L2 = [select id, Status from Lead where ID in :LeadIds];
if(L2.Status != 'Closed'){
Zipcodes__c z = [select Territory__c from Zipcodes__c where Name in :ZipCodeIds];
L2.Territoy__c = z.Territory__c;
}
Update L2;
}
by
Zipcodes__c z = [select Territory__c from Zipcodes__c where Name in :ZipCodeIds];
maps.put(postalcode,territory);
for(lead l11:trigger.new)
{
l11.territoy__c=maps.get(l11.postalcode)
leadlist.add(l11);
}
update leadlist;
Jerry ClifftJerry Clifft

Thanks for teh reply, I put this together best I could: here is what I got...

 

trigger LeadfindTLR2 on Lead (After Insert, After Update) {

    map<string,string>maps=new map<string,string>();
    list<lead>leadlist=new list<lead>();
    Set<Id> ZipCodeIds = new Set<Id>();
    Set<Id> LeadIds = new Set<Id>();

// On Lead take the zipcode and find that zipcode in the custom object Zipcodes then update the lead field "Territory" with data from the field Queue_ID custom object Zipcode.

       for (Lead L: Trigger.New){
         if (L.Name != null && L.PostalCode != null )
         LeadIds.add(L.Id);
         ZipCodeIds.add(L.PostalCode);
         }

    Zipcodes__c z = [select Name from Zipcodes__c where Name in :ZipCodeIds];
        maps.put(postalcode,Name);
            for(lead l11:trigger.new)
    
    {
        l11.territoy__c=maps.get(l11.postalcode);
        leadlist.add(l11);
    }
        update leadlist;
    }

 

I now receive the following error when I try to save:

Error: Compile Error: Variable does not exist: postalcode at line 17 column 18