1

I have a list of 41K zip codes that are all are connected to a DMA. The business would like to enter in a zip code and then based on that value automatically return the DMA value in the Market2__c field. This could be done with a formula but it would be so long, that I think it would be unmanageable. I could do a dependent picklist but then I would have to change the format of the zip code field and would have to make a separate field for Market2__c because it is a formula field and the data type can't be changed. There aren't enough workflows to turn this into a series of workflows. Even if that amount were available I wouldn't want to do it that way. I was going to try a custom object with a trigger. Here is what my trigger looks like so far. Any suggestions?

trigger InsertDMA on Lead (before insert, before update) {
    for(Lead lead : Trigger.new)
    {
        string DMA = DMA_Value__c.getInstance(lead.PostalCode).DMA_Value__c;
        lead.Market2__c = DMA ;
    }
}

1 Answer 1

1

I have done this sort of functionality before and that is the approach we used. However, I strongly recommend you move all code out of your trigger. I typically employ a handler pattern. This pattern has worked really well for me and it makes testing so much easier.

NOTE: You are going to run into errors if you assume getInstance will return a result. Add a null check!

DMA_Value__c setting = DMA_Value__c.getInstance(lead.PostalCode);
String dmaValue = (setting == null) ? null : setting.DMA_Value__c;

You must log in to answer this question.

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