I have a trigger that is supposed to work like this: When there is a value in the PostalCode field whether new or updated the trigger looks at the custom object "Name" field which is a list of zipcodes. If the zipcode matches one of the names the DMA_Value__c field on the same row is returned in the Market2__c field on the Lead object. I keep getting errors though. I assume it is because of the set or the map. The custom object is also called DMA_Values__c. The code is below.
{
Set <PostalCode> PostalCode = new Set<PostalCode>();
for(Lead l : trigger.new)
{
if(l.PostalCode != null)
{
PostalCode.add(l.PostalCode);
}
}
Map <PostalCode> validPostalCode = new Map<validPostalCode>();
if(PostalCode.size() > 0)
{
for(DMA_Value__c obj : [SELECT Id, Name, DMA_Value__c FROM DMA_Value__c WHERE Name IN :PostalCode])
{
validPostalCode.put(obj.Name, obj);
}
}
for(Lead l : trigger.new)
{
if( (Trigger.isInsert || Trigger.isUpdate && Trigger.oldMap.get(l.Id).PostalCode != l.PostalCode ) && validPostalCode.containsKey(l.PostalCode))
{
System.debug(l.PostalCode + ' matched ' + validPostalCode.get(l.PostalCode));
l.Market_3__c = validPostalCode.get(l.PostalCode).DMA_Value__c;
}
}
}