function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
erikrusselerikrussel 

Error:Apex trigger UpdateCountyInfo2 caused an unexpected exception

Error:Apex trigger UpdateCountyInfo2 caused an unexpected exception, contact your administrator: UpdateCountyInfo2: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateCountyInfo2: line 20, column 74

 

I recieved the following error when trying to save a record.  Hoping someone could help/provide some insight on this?  Below is my trigger.

 

trigger UpdateCountyInfo2 on Lead(after insert,after update) {
Set<Id> assIdSet=new Set<Id>();

   for(Lead l:Trigger.new)
   {
       if(l.Postal_Code_LookUp_Field__c != null) {
     
        assIdSet.add(l.Name);
        }
    }
   

       if(assIdSet.size()>0){
      Map<Id,sundog_deprm2__Postal_Code__c> assMap = new Map<Id,sundog_deprm2__Postal_Code__c>([Select Id,Name,sundog_deprm2__County__c from sundog_deprm2__Postal_Code__c where Id IN: assIdSet]);
     
         for(Lead LD:Trigger.new)
         {
           if(LD.Postal_Code_LookUp_Field__c !=null) 
           {
               LD.County__c = assMap.get(LD.Postal_Code_LookUp_Field__c).sundog_deprm2__County__c; 

          }

         else{

                   LD.County__c=null;

                 }
                 }
                 }
                 }
Ritesh AswaneyRitesh Aswaney

You've added names to the assIdSet,

assIdSet.add(l.Name);

but in the SOQL Query, you're comparing Ids with this set.

 

SMGSFDCDEVSMGSFDCDEV

What record where you trying to save?

 

Usually, when you receive the error message: "Attempt to de-reference a null object" it means that the one of your DML statements (after insert, after update) is requiring a value in one of your fields on your object: 

sundog_deprm2__Postal_Code__c.  However the fields for that custom object might not be available on the Lead object.

 

Can you provide more information on the record you are trying to commit to the database?

sundog_deprm2__Postal_Code__c
erikrusselerikrussel

So I have a object called Postal_Code that contains a field sundog_deprm2__County__c.  I want to somehow pass that value to a Lead field I created called County__c.  

 

These objects are not related.  So I created the a field on leads Postal_Code_LookUp_Field__c.  

 

Now my trigger works only the problem is Postal_Code_LookUp_Field__c is a lookup field so I can't create a workflow to pass PostalCode (Zip/Postal Code on Lead) to Postal_Code_LookUp_Field__c.  So if I manually update Postal_Code_LookUp_Field__c with the correct zip the trigger works but that kinda defeats the purpose.

 

Thanks, Hope you can help

Erik