1

This Trigger is on lead (Before Insert, Before Update) and it errors (System.NullPointerException: Attempt to de-reference a null object). I'm getting this error when i'm posting "Web to Lead". However i do have the right ORG iD and Record type id included on the xml file:

<input type=hidden name="oid" value="xxxxxxxxxxxxx">
<input type=hidden name="recordType" id="recordType" value="zzzzzzzzzzzzzzzzzz">
trigger IAAILeadTrigger on Lead (before insert, before update){
//BEGIN: S-444340 T-561808 - Process only IAA Remarketing Recordtype data
    Map<ID,Schema.RecordTypeInfo> rt_Map = Lead.sObjectType.getDescribe().getRecordTypeInfosById();

    //Looks up the branches based on the Lead PostalCode
    if(trigger.isBefore){

        for(Lead ld : trigger.new){            
         if (rt_map.get(ld.recordTypeID).getName().containsIgnoreCase('IAA Remarketing') )

              if( trigger.isInsert || 
                  (trigger.isUpdate && ld.Branch__c == null) ||
                  (trigger.isUpdate && ld.PostalCode != trigger.oldMap.get(ld.id).PostalCode)
              ){
                  leads.add(ld);
              }

         }    
      }        
        //Only check the leads that are in the lead list and NOT the full trigger.new list
        if(!leads.isEmpty()){
            LeadController.GetLeadBranches(leads);  
        }
    }

}
4
  • This saves? Where is leads?
    – EricSSH
    Commented Aug 15, 2017 at 20:43
  • 3
    On a BeforeInsert, there is NO Lead ID. There's also No Oldmap either. What you're doing will only work for a BeforeUpdate.
    – crmprogdev
    Commented Aug 15, 2017 at 20:45
  • Thanks #crmprogdev that answers the question and I have fixed it by adding if statement if(ld.ID != null).
    – Templar
    Commented Aug 15, 2017 at 20:58
  • @crmprogdev, can you convert your response into an answer so templar can accept it?
    – metasync
    Commented Aug 15, 2017 at 21:03

1 Answer 1

3

That's not what crmprogdev was saying, he was saying you don't have access to ld.id or the oldMap in the before insert context. We don't know what GetLeadBranches is doing but if you need the IDs you need to change it to after insert..

By putting if(ld.ID != null){} you're completely negating the before insert part of this trigger, but considering your checking the oldMap It appears that you only care about updates so you can just trash the insert context entirely.

Trigger IAAILeadTrigger on Lead (before update)
{

Map<ID,Schema.RecordTypeInfo> rt_Map = Lead.sObjectType.getDescribe().getRecordTypeInfosById();

List<Lead> leadList = new List<Lead>();
//Looks up the branches based on the Lead PostalCode
for(Lead ld : Trigger.new)
{            
    if (rt_map.containsKey(ld.recordTypeID) && 
        rt_map.get(ld.recordTypeID).getName().containsIgnoreCase('IAA Remarketing') )
    {
      if(Trigger.isInsert || 
          (Trigger.isUpdate && ld.Branch__c == null) ||
          (Trigger.isUpdate && ld.PostalCode != Trigger.oldMap.get(ld.ID).PostalCode)
      )
          {
              leadList.add(ld);
          }
    }    
}        
//Only check the leads that are in the lead list and NOT the full 
if(!leadList.isEmpty()){
    LeadController.GetLeadBranches(leadList);  
  }
}
0

You must log in to answer this question.

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