1

Error: There was an error creating Lead 1. Salesforce error:

dlrs_LeadTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Class.Helper.populateparnerId: line 23, column 1 Trigger.dlrs_LeadTrigger: line 9, column 1

So I am working on two object:

-Leads

-Partner object(Custom)

So I have a before insert Trigger on Leads, so when a new Lead is created and Partner Number Field is not blank it has to lookup for Partner_Number_Copy__c field on the Partner Object and return its Partner ID

Following Code works fine if I manually create a lead and provide a Partner Number it is populating a Partner__c field which is a lookup field to Partner object.

But when Leads are created through a Web Form I am receiving the above error.

FYI: Line 26 in the code is l.Partner__c=mapIdByPartner.get(l.Partner_Number__c).id;

`

public class Helper{      

         public static void populateparnerId(list<Lead> lstlead){

             set<decimal> setpartnerIds=new set<decimal>();

              for(Lead l:lstlead){
                 if(l.Partner_Number__c!=null){
                      setpartnerIds.add(l.Partner_Number__c);
                    }
             }

       //Query partner records

    map<decimal,Partner__c>  mapIdByPartner=new map<decimal,Partner__c>();

    for(Partner__c p:[Select Id,Partner_Number_Copy__c from Partner__c where Partner_Number_Copy__c  in:setpartnerIds]){
       mapIdByPartner.put(p.Partner_Number_Copy__c,p);
    }

     for(lead l:lstlead){
           if(l.Partner_Number__c!=null){
             l.Partner__c=mapIdByPartner.get(l.Partner_Number__c).id;
          }
      }
  }
}

`

Trigger used (also Line 9 from the error message),

if(trigger.isInsert && trigger.isBefore){ Helper.populateparnerId(trigger.new); }

  • In your test through a Web Form, were you setting a Partner Number? You may be getting that error if none of the leads in your batch have Partner Numbers. You should wrap everything from your mapIdByPartner and beyond in an if(setpartnerIds.size()>0){ //your code here } to avoid querying unnecessarily. Also I would confirm that all of your data types for Partner_Number__c and Partner_Number_Copy__c match. When in doubt, add a bunch of debug statements to see what the returned values are. – Todd Sprinkel Jul 24 '15 at 18:34
  • @ToddSprinkel: I used the same Partner number to test in the Web form and manual creation of lead and Manual Testing worked but Web form--No. I even checked if the form is populating the value right by mapping it to a different field and it did populate the value and no error. – Sanjay Jul 24 '15 at 19:05
1

Consider the following situation:

A Lead is coming with Partner_Number__c = '123' ('123' != null). If you do not have Partner__c record with such Partner_Number_Copy__c then mapIdByPartner.get(l.Partner_Number__c) will return null and it obviously has no Id. So you might want to modify your code as follows :

if(l.Partner_Number__c != null && mapIdByPartner.get(l.Partner_Number__c) != null){ l.Partner__c = mapIdByPartner.get(l.Partner_Number__c).id; }

This way you`ll be ready for situations when there will be no records with incoming Partner_Number__c in your system.

  • I used the same Partner number to test in the Web form and manual creation of lead and Manual Testing worked but Web form--No. I even checked if the form is populating the value right by mapping it to a different field and it did populate the value and no error. – Sanjay Jul 24 '15 at 19:05
  • Can you add the following line of code System.debug('*** Partner number: '+l.Partner_Number__c) just before that line where you are getting this error. Then set up debug logs for your users and look what value is coming there? – Oles Malkov Jul 24 '15 at 19:11
  • Sure, will update you on this.. I'm currently waiting on login access to the Webform. – Sanjay Jul 24 '15 at 19:44
  • No field can contain only a single empty space. It will be rendered as null upon saving. – sfdcfox Jul 24 '15 at 19:52
  • Thanks for pointing that, I have updated my answer, anyway that was just for example of a value that does not exist in the system. – Oles Malkov Jul 24 '15 at 20:09
0

Hello Sanjay you are putting "p.Partner_Number_Copy__c" as key in "mapIdByPartner" map and at the line no 23 you are trying to get value using key"l.Partner_Number__c". so i think it is not returning any value...

so make sure that in this line

l.Partner__c=mapIdByPartner.get(l.Partner_Number__c).id;

l.Partner_Number__c is present or not in mapIdByPartner collection as a key

  • It works fine when I manually Test it by creating a lead manually. – Sanjay Jul 24 '15 at 19:06
0

You should change line 9 from

l.Partner__c=mapIdByPartner.get(l.Partner_Number__c).id;

to

if(mapIdByPartner.containsKey(l.Partner_Number__c))
{
  l.Partner__c=mapIdByPartner.get(l.Partner_Number__c).id;
}

Most probabily , map is null and you are trying to access id.

  • So I used the same Partner Number value in the web form which is causing the error and when manually creating a Lead with the same Partner Number its not. It should find a match isn't it ? Partner Number used while testing both through Web form and Manually is same value. – Sanjay Jul 24 '15 at 18:44
  • You may need to check if you are getting this field value from webform or not ? WebtoLead form needs to have that value. – Jitendra Zaa Jul 24 '15 at 18:46
  • I checked populating the value to a different field and it did -clean. So it is populating the right value. Not sure why its not pulling the value. – Sanjay Jul 24 '15 at 18:50
  • @JitendraZaa The map will never be null. There's no need to check for something that will never be. – sfdcfox Jul 24 '15 at 19:54
  • @sfdcfox: Any suggestions on why it is not populating the Partner ID(lookup) ? I did fix the error message by checking for the NULL value. So now it creates a lead and also the Partner Number field is populating the Partner Number, and I made sure the Partner Number exists on the Partner Object so it should populate it respective Partner ID, while testing manually without using forms It works. But not using the forms – Sanjay Jul 27 '15 at 17:07

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.