0

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; 
    }
} 
}

2 Answers 2

2

If I understand your question correctly you want to look up the postal code from the lead, query your custom object, to find matching market values, and update the lead with that value. In that case, you are pretty close on the logic. Your issue is that you're trying to define your Set and Map using the fields, when you need to define what datatype the Set/Map will be using.

Your code should look something like this:

trigger UpdateMarket on Lead (before insert, before update) {
    //get set of Postal Codes from leads to be inserted or updated
    Set<String> postalCodes = new Set<String>();
    for(Lead l : trigger.new){
        if(l.PostalCode != null){
            postalCodes.add(l.PostalCode);
        }
    }
    if(postalCodes.size() > 0){
        //create Map of custom object records with Name (PostalCode) as key
        Map<String,DMA_Values__c> validPostalCode = new Map<String,DMA_Values__c>();
        for(DMA_Values__c obj : [SELECT Id, Name, DMA_Value__c FROM DMA_Values__c WHERE Name IN :postalCodes]){
            validPostalCode.put(obj.Name,obj);
        }
        for(Lead l : trigger.new){
            if((Trigger.isInsert || trigger.oldMap.get(l.Id).PostalCode != l.PostalCode) && validPostalCode.containsKey(l.PostalCode)){
                //Set lead field value from map (not sure what it is called)
                //Market2__c or Market_3__c
                l.Market2__c = validPostalCode.get(l.PostalCode).DMA_Value__c;
            }
        }
    }
}

Are you at all concerned about blanking out the Market2__c field on the lead if the new value of PostalCode is not found in the validPostalCode map? This will only set the value if the new value is retrieved.

EDIT:

trigger UpdateMarket on Lead (before insert, before update) {
    //get set of Postal Codes from leads to be inserted or updated
    Set<String> postalCodes = new Set<String>();
    for(Lead l : trigger.new){
        if(l.PostalCode != null){
            postalCodes.add(l.PostalCode);
        }
    }
    if(postalCodes.size() > 0){
        //create Map of custom object records with Name (PostalCode) as key
        Map<String,DMA_Values__c> validPostalCode = new Map<String,DMA_Values__c>();
        for(DMA_Values__c obj : [SELECT Id, Name, DMA_Value__c FROM DMA_Values__c WHERE Name IN :postalCodes]){
            validPostalCode.put(obj.Name,obj);
        }
        for(Lead l : trigger.new){
            if((Trigger.isInsert || trigger.oldMap.get(l.Id).PostalCode != l.PostalCode) && validPostalCode.containsKey(l.PostalCode)){
                //not sure what this field is called
                //Market2__c or Market_3__c
                l.Market2__c = validPostalCode.get(l.PostalCode).DMA_Value__c;
            }
            else if(trigger.oldMap.get(l.Id).PostalCode != l.PostalCode && !validPostalCode.containsKey(l.PostalCode)){
                //blank value if new PostalCode does not match a record in the Map
                l.Market2__c = null;
            }
        }
    }
}
2
  • I didn't even think about that. Yes it does need to clear if the Postal code is not in the custom object Name field. Commented Mar 6, 2015 at 19:52
  • updated code added. Commented Mar 6, 2015 at 19:59
0

I'm sorry, I'm quite new here, and new to SalesForce - I'd rather put this in a "requesting clarification" comment than an answer, but I don't yet have the points to do that. (Seems backwards...)

But...I just wanted to chime in in case you don't get a better answer. (Please simply ignore me if you do!)

Note: Reading your code, I can't quite figure out what your custom object is called, so I'm just going to refer to it as "YourCustomObject."

Could you get a trigger working that populates a Lookup(YourCustomObject)-typed field on Lead with the YourCustomObject whose YourCustomObject.Name is an exact string match to Lead.PostalCode (leaving it null if there isn't an exact string match)?

I'm not yet good enough at triggers to simply write one like that (although I think my design approach would be to look for the value to fill the field in with by passing Lead.PostalCode to an SOQL query against YourCustomObject with a a WHERE {variable holding Lead.PostalCode}=Name, kind of like happens with passing data into a SOQL statement in this code).

If you are that good at triggers, then once you've populated this Lookup(YourCustomObject) field on Lead, you could add a Formula Field to Lead to grab the appropriate DMAValue.

You'd still have to be good enough to write the trigger in the first place, but at least adding this layer of abstraction would take one problem out of the algorithm you need to encode. And it sounds like perhaps it could remove the part of the algorithm that is giving you errors.

(If I'm no help at all, please just ignore me, or comment asking me to take my answer down! I'd rather be overlooked than downvoted.) :-)

3
  • Thank you for your help, the reason why I"m not using a formula is that there are over 41K records which would be hard to maintain in a formula. It would be a very long formula. Commented Mar 6, 2015 at 17:54
  • @ToddSprinkel That worked perfectly! Thank you so much!!!! Commented Mar 6, 2015 at 20:20
  • Oh well. Glad Todd was able to help, and thanks for the feedback, @BlairKristin! (I just realized that I may have misunderstood the structure of your custom object - I was thinking that "Name" was a unique record ID to its rows and that you simply needed a verbatim copy of a single "DMAValue" from a given row of the custom object.)
    – k..
    Commented Mar 6, 2015 at 22:44

You must log in to answer this question.

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