0

I keep getting the System.NullPointerException: Attempt to de-reference a null object on my trigger at line 21. I've attempted to add != null to that line and I get an error too. Please see the code below.

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)){

        l.DMA__c = validBillingPostalCode.get(l.BillingPostalCode).DMA_Value__c;
    }
    else if(trigger.oldMap.get(l.Id).BillingPostalCode != NULL || l.BillingPostalCode && !validBillingPostalCode.containsKey(l.BillingPostalCode)){
        //blank value if new PostalCode does not match a record in the Map
        l.DMA__c = Null;
    }
  }
 }
}
  • Line 21 is the line starting with "else if(trigger.oldMap.get(l.ID).BillingPostalCode" near the bottom – Blair Kristin Mar 12 '15 at 21:36
  • 1
    one issue could be your map creation doesn't occur unless BillingPostalCodes.size() > 0 - if 0, then map is null and second condition fails on line 21; also, use of variable l for accounts is not best practice as it makes one think of leads – cropredy Mar 12 '15 at 21:44
  • @crop1645 could I use >= to say that it is greater than or equal to 0? – Blair Kristin Mar 12 '15 at 21:52
1

Blair

This seems like it could be much simpler. If the incoming billingPostalCode (bpc) is not present in the custom object that maps bpc to DMAs, then you want the value of account.dma__c to be null; if it is present in the custom object, you want the value to be the corresponding DMA. Testing for whether the bpc changed on an update doesn't seem relevant as you just replace the "dma" with itself and cause no harm.

trigger UpdateDMAAccount on Account (before insert, before update) {
   // 1 - get unique set of Postal Codes from leads to be inserted or updated
 Set<String> bpcSet = new Set<String>();
 for(Account l : trigger.new)
   if(l.BillingPostalCode != null)
     bpcSet.add(l.BillingPostalCode);

  // 2- create Map of custom DMA records with Name (PostalCode) as key   
  Map<String,DMA_Value__c> bpcToDmaMap = new Map<String,DMA_Value__c>();
  for(DMA_Value__c dv : [SELECT Id, Name, DMA_Value__c FROM DMA_Value__c
                           WHERE Name IN :bpcSet])
    bpcToDmaMap.put(dv.Name,dv);

  // set account's dma based on billingpostalCode - if not valid, dma is null
  for(Account l : trigger.new)
   l.dma__c =  bpcToDmaMap.get(l.billingPostalCode).dma_value__c // not present, returns null

}

I omitted issues where the incoming Account billingPostalCode is in lower case but the custom object key of DMA_Value__c is in upper case (think UK, Canada postal codes). Apex Map string keys are case sensitive. "Exercise for the reader"

  • it keeps giving me the following error when I use the code above: "Error: Compile Error: Illegal assignment from DMA_Value__c to String at line 16 column 4" . Line 16 is the last line of code. – Blair Kristin Mar 13 '15 at 14:10
  • fixed; see revised line 16 – cropredy Mar 13 '15 at 20:41
0

Well your using .oldMap and it's an insert trigger, there is never going to be an id.. I'm not sure what you use case is but you can put in If(Trigger.isUpdate){dostuff}, you might have to break out that logic at 21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.