You need to sign in to do that
Don't have an account?
Adil_SFDC
Trigger on Lead
Hi I have the following trigger to write
Write a trigger on the Lead object that populates a custom field on Lead, called Lead.Territory__c, with the ZipTerr__c.Territory__c that matches Lead.PostalCode to ZipTerr__c.Zip__c.
Below is the ZipTerr object (table)
ZipTerr__c
Zip__c | Territory__c |
94305 | 201 |
94261 | 201 |
… | … |
75093 | 101 |
75248 | 101 |
… | …
|
Any help please
Thanks
when i am trying to execute the same
I get the following error:
Method does not exist or incorrect signature: [MAP<String,ZipTerr__c>].put(String) at line 5 column 5
Thanks in advance
Hi
this is has wrong
codes.put(record.postalcode);
codes is map , map has key & value pair but your passing only one string.
changes above line to like this
codes.put(record.postalcode,record);
Adil,
Try below ,
As codes is a map<String,ZipTerr__c> ,it will throw an error if you add lead to it,hence I created a new map
trigger updateTerritory on Lead(before insert, before update) {
Map<String,ZipTerr__c> codes = new map<string,zipterr__c>();
Map<String,Lead> leadcodes = new map<string,Lead>();
for(lead record:trigger.new) {
leadcodes.put(record.postalcode,record);
}
for(zipterr__c record:[select id,zip__c,territory__c from zipterr__c where zip__c in :leadcodes.keyset()]) {
codes.put(record.zip__c,record);
}
for(lead record:trigger.new) {
if(codes.containskey(record.postalcode)) {
record.Territory__c = codes.get(record.postalcode).Territory__c;
}
}
}
Here is what I came up with
trigger updateTerritory on Lead(before insert, before update)
{
Map<String,String> zipTerrMap = new Map<String,String>();
Set<String> zipcodes = new Set<String>();
//Adding Lead Postal codes to Zipcodes
for(Lead record : trigger.new)
{
zipcodes.add(record.postalCode);
}
//Adding Zip and Territory from ZipTerr Object to the Map
for( ZipTerr__c zip : [select zip__c,Territory__c from ZipTerr__c where zip__c in : zipcodes])
{
zipTerrMap.put(zip.zip__c,zip.Territory__c);
}
// Updating the Territory field
for(Lead le : trigger.new)
{
if(zipTerrMap.containsKey(le.postalCode))
{
le.Territory__c=zipTerrMap.get(le.postalCode);
}
}
}