0

The code below keeps throwing a dereference null object error. I've tried making changes to make sure the item isn't empty but I am still getting this error. It is driving me insane. This code is supposed to check if the value in the BillingPostalCode field matches a value in a field called "Name" in a custom object called DMA_Value__c, if it is it should return the value from the DMA_Value__c field in the custom object to a DMA field on the account.I have looked at the other questions that are said to be duplicates, but those do not give me the answer I need. I've tried putting the suggestions into the code to catch null values. Now that I have done that I'm confused as to how it could still have this error. Maybe a better explanation of what this error message means would help.

FIRST ATTEMPT

trigger UpdateDMAAccount on Account (before insert, before update) {
//get set of Postal Codes from leads to be inserted or updated
Set<String> BillingpostalCodes = new Set<String>();
for(Account l : trigger.new){
if(l.BillingPostalCode != null){
    BillingpostalCodes.add(l.BillingPostalCode);
 }
}
if(BillingpostalCodes.size() >= 0){
//create Map of custom object records with Name (PostalCode) as key
Map<String,DMA_Value__c> validBillingPostalCode = new Map<String,DMA_Value__c>();
for(DMA_Value__c obj : [SELECT Id, Name, DMA_Value__c FROM DMA_Value__c WHERE Name IN :BillingpostalCodes]){
    validBillingPostalCode.put(obj.Name,obj);
}
for(Account l : trigger.new){
    if((Trigger.isInsert || trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode) && validBillingPostalCode.containsKey(l.BillingPostalCode)){
        //not sure what this field is called
        //Market2__c or Market_3__c
        l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
    }
    else if(trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)){
        //blank value if new PostalCode does not match a record in the Map
        l.DMA__c = NULL;
    }
  }
 }
}

SECOND ATTEMPT

trigger UpdateDMAAccount on Account (before insert, before update) {
//get set of Postal Codes from leads to be inserted or updated
Set<String> BillingpostalCodes = new Set<String>();
for(Account l : trigger.new){
if(l.BillingPostalCode != null){
    BillingpostalCodes.add(l.BillingPostalCode);
 }
}
if(BillingpostalCodes.size() >= 0){
//create Map of custom object records with Name (PostalCode) as key
Map<String,DMA_Value__c> validBillingPostalCode = new Map<String,DMA_Value__c>();
for(DMA_Value__c obj : [SELECT Id, Name, DMA_Value__c FROM DMA_Value__c WHERE Name IN :BillingpostalCodes]){
    validBillingPostalCode.put(obj.Name,obj);
}
for(Account l : trigger.new){
    if((l.BillingPostalCode != NULL && trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode) && validBillingPostalCode.containsKey(l.BillingPostalCode)){
        //not sure what this field is called
        //Market2__c or Market_3__c
        l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
    }
    else if(l.BillingPostalCode != NULL && Trigger.isInsert && !validBillingPostalCode.containsKey(l.BillingPostalCode)){
        //blank value if new PostalCode does not match a record in the Map
        l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;         
    }
    else if(trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)) {
        //blank value if new PostalCode does not match a record in the Map
        l.DMA__c = NULL;
    }
  }
 }
3
  • 1
    there is no trigger.oldmap for insert, i don't see a guard against that scenario.
    – Phil B
    Commented Mar 13, 2015 at 20:06
  • @PhilB I removed the old map in the second attempt from the insert. Commented Mar 13, 2015 at 20:21
  • 1
    what happens when its an insert and it hits that first IF statement and tries to get the id from the oldmap?
    – Phil B
    Commented Mar 13, 2015 at 20:25

1 Answer 1

0

Your code must be like this. In addition you must figure out all scenarios where you can get NULL value.

I have added some major condition checks of NULL. Also, you should keep the difference between Insert and Update event.

for(Account l : trigger.new)
{
    if(Trigger.isInsert)
    {
        if(l.BillingPostalCode != NULL && !validBillingPostalCode.containsKey(l.BillingPostalCode))
        {
            //blank value if new PostalCode does not match a record in the Map
            l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;         
        }
    }
    else if(Trigger.isUpdate)
    {
        if((l.BillingPostalCode != NULL && trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode) && validBillingPostalCode.containsKey(l.BillingPostalCode))
        {
            //not sure what this field is called
            //Market2__c or Market_3__c
            l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
        }
        else if(trigger.oldMap.get(l.Id).BillingPostalCode != l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)) 
        {
            //blank value if new PostalCode does not match a record in the Map
            l.DMA__c = NULL;
        }
    }
}

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