-2

Error on creating a lead in salesforce:

Apex trigger LeadConvert caused an unexpected exception, contact your administrator: LeadConvert: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: ()

Trigger LeadConvert:

trigger LeadConvert on Lead (before insert,after insert,before update, after update){
Set<String> leadSourceSet = new Set<String>{'ABC','DEF','SFDC','SFDC-IN|XYZ'};
Class_Lead_TriggerFunction cls = new Class_Lead_TriggerFunction();
if(trigger.isInsert){
    if(trigger.isBefore){
        cls.beforeInsert(trigger.new);
    }
    if(trigger.isAfter){
        cls.afterInsert(trigger.new,trigger.oldMap);
    }
}
}

Class_Lead_TriggerFunction:

public class Class_Lead_TriggerFunction{
  public static LeadStatus convertStatus = new LeadStatus();
  public static Set<String> leadSourceSet = new Set<String>{'ABC','DEF','SFDC','SFDC-IN|XYZ'};

  public Class_Lead_TriggerFunction(){
    convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  }  


    public void AfterInsert(list<Lead> newList,map<Id,Lead> oldMap){
      afterLeadConvert(newList,oldMap);
    }     


    public static void afterLeadConvert(list<Lead> newList,map<Id,Lead> oldMap){
      map<String,Id> accountNameIdMap = new map<string,Id>();
      set<string> leadCompanySet = new set<string>();
    list<Lead> leadListToConvert = new list<Lead>();

      for(Lead newLead : newList){
       if((trigger.isInsert || (trigger.isUpdate && oldMap.get(newLead.Id).LeadSource != newLead.LeadSource)) && newLead.Is_Duplicate__c == false && ((!leadSourceSet.contains(newLead.LeadSource) && !(newLead.LeadSource).startsWith('SFDC')) || newLead.LeadSource == null)){
        leadListToConvert.add(newLead);
        if(lead.Company != null){
          leadCompanySet.add(newLead.Company);
        }
       }  
    }
    system.debug('leadCompanySet*************************************'+leadCompanySet);
    if(leadCompanySet.size() > 0){
      for(Account account : [select Id,Name from Account where Name in : leadCompanySet]){
        accountNameIdMap.put(account.Name,account.Id);

      }
    }
    system.debug('accountNameIdMap*************************************'+accountNameIdMap);
    for(Lead newLead : leadListToConvert){
      Database.LeadConvert lc = new database.LeadConvert(); 
      lc.setLeadId(newLead.Id);
      if(accountNameIdMap.get(newLead.company) != null){
        lc.setAccountId(accountNameIdMap.get(newLead.company));
      }
         lc.setDoNotCreateOpportunity(true);         
      lc.setConvertedStatus(convertStatus.MasterLabel);
      Database.LeadConvertResult lcr = Database.convertLead(lc); 
      System.assert(lcr.isSuccess());
    }
 }   

Please suggest, issue in Prod.

Thanks

3
  • 1
    You will need to provide where do you see this error in your code.
    – Jayant Das
    May 3, 2019 at 13:35
  • when trigger calls: cls.afterInsert(trigger.new,trigger.oldMap); May 3, 2019 at 13:36
  • if((trigger.isInsert || (trigger.isUpdate && oldMap.get(newLead.Id).LeadSource != newLead.LeadSource)) && newLead.Is_Duplicate__c == false && ((!leadSourceSet.contains(newLead.LeadSource) && !(newLead.LeadSource).startsWith('SFDC')) || newLead.LeadSource == null)) throws error May 3, 2019 at 13:37

2 Answers 2

0

You may have couple of problems here, but at the first instance these look very obvious.

  1. Trigger.OldMap in insert triggers are not available.

  2. Then in your method it seems you are having issue where you are checking this condition where oldMap.get is potentially returning null

    if((trigger.isInsert || 
    (trigger.isUpdate && oldMap.get(newLead.Id).LeadSource != newLead.LeadSource)) 
      && newLead.Is_Duplicate__c == false 
      && ((!leadSourceSet.contains(newLead.LeadSource) 
      && !(newLead.LeadSource).startsWith('SFDC')) || newLead.LeadSource == null)){
    
  3. And where you are using:

    (newLead.LeadSource).startsWith('SFDC')
    

    If at all your newLead.LeadSource returns null, you will end up having a Null Pointer Exception. You will need to modify your condition here. In such scenarios, it's always advisable to first fetch the value and the compare it with the string.

    Your modified code can look like as below, where you can check for null before performing further action on the value itself.

    if( .... && !(newLead.LeadSource != null && newLeadSource.startsWith('SFDC'))
    
2
  • Well, it is not showing errors anymore, but the condition is getting true and as line 98 says, it is converted all the Leads to contacts. May 3, 2019 at 15:00
  • I don't quite follow. But if your issue is resolved on errors, that would mean that your question is now answered.
    – Jayant Das
    May 3, 2019 at 15:05
-1

When you are checking this- if(accountNameIdMap.get(newLead.company) != null) Can you replace above if condition with the below condition like and try.

if(accountNameIdMap.containsKey(newLead.company) {
      lc.setAccountId(accountNameIdMap.get(newLead.company));  
}

If the map doesn't contains newLead.company and you are trying to get the value for key-newLead.company , if may throw an error.

2
  • This answer is mostly wrong. Both map.containsKey(<key>) and map.get(<key>) != null are valid ways to check if a map contains a particular key. For cases like this where you aren't sure about how a piece of code works, it really helps if you look at documentation and/or work something out in anonymous apex to confirm your claims before posting an answer.
    – Derek F
    May 7, 2019 at 22:40
  • Thank you @DerekF because the errors still persist. May 8, 2019 at 7:40

You must log in to answer this question.

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