1

I'm trying to implement lead assignment based on zip codes, and I'm running into an error with the code. I set up a Zip Code custom object, Zip_Code__c, where the name is the zipcode. Both the custom object and the Lead object have a custom text field, CA_Territory__c (this is for California zip codes only). When the zip code on a lead is entered or edited, I want to pull the CA Territory value from the custom object into the lead field. This is my code:

trigger CATerritory on Lead (before insert, before update) {  
//get the first five digits of the PostalCode from the lead  
set<string> left5zips = new set<string>();  
for (lead l: trigger.New) if (l.postalcode !=null) left5zips.add(l.postalcode.substring(0,5));

//if no zip listed on the lead  
if(left5zips.isEmpty()) lead.ca_territory__c='None';  

//query the zip_code object to get the zipcode (Name) and zone from the zip code object  
map<string,string> zmap=new map<string,string>();  
  for(Zip_Code__c z :
     [Select name, ca_territory__c from Zip_Code__c WHERE name IN :left5zips])
zmap.put (z.name, z.ca_territory__c);

for(lead l:trigger.new){  
if(zmap.containskey(l.left5zips))  
     l.ca_territory__c=zmap.get(l.left5zips);  
}  
}

The error I am getting is 'Compile Error: Expression cannot be assigned at line -1 column -1'. This is my first attempt at a trigger, and it is based on other people's code, so I don't really know how to troubleshoot. Any ideas? Thanks!

3 Answers 3

3

You have a few errors in your code that will stop it from compiling:

  • lead.ca_territory__c='None' won't compile because there is no variable called lead
  • l.left5zips won't compile because there is no field called left5zips on Lead

The code below should work but I wrote it from a text editor so not sure if it will compile. If you are looking for help in debugging your code then have a look at: How do I start to debug my own Apex code?

trigger CATerritory 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.substring(0,5));
        }
    }

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

    for (lead l: trigger.new) {
        if(l.postalcode != null) {
            String territory = territoryMap.get(l.postalcode.substring(0,5));
            if (territory != null) {
                l.ca_territory__c = territory;
            }

        }
        if (l.ca_territory__c == null ) {
            l.ca_territory__c = 'None';
        }
    }
}
1
  • Thank you very much for your help. There was a semicolon missing at the end of the last if statement, but once I entered that it worked. Commented Sep 25, 2014 at 18:22
1

It looks like you have a bit too much shorthand in your code. In your second and third for loops, you do not have opening and closing curly brackets. Try adding those and see if the problem persists.

EDIT: Also, you should add curly brackets around your IF statements.

0

Your code does have some issues, but I see an issue at the end of your trigger:

for(lead l:trigger.new){  
if(zmap.containskey(l.left5zips))  
     l.ca_territory__c=zmap.get(l.left5zips);  
} 

l.left5zips doesn't exist. left5zips the set exists, but not as a member of the lead object. I think you may be looking for

zmap.containskey(l.ca_territory__c)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .