1

I've been trying to mass update existing records to pull and update the Prospect Referral Store. However when I run the data import wizard I get the error

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY:HomeDepotStoreProspect: execution of BeforeUpdate

caused by: System.NullPointerException: Attempt to de-reference a null object

Here is my Trigger Handler Class Code

public class HomeDepotStoreHandler extends TriggerHandler {   
public HomeDepotStoreHandler() {}

public override void beforeUpdate() {
i360__Prospect__c[] prospects = (i360__Prospect__c[])Trigger.new;

Set<String> referralStoreNames = new Set<String>();
for (i360__Prospect__c prospect : prospects){ 
    if (prospect.Referral_Store__c != null){
      referralStoreNames.add(prospect.Referral_Store__c.substringBefore(' '));
    }
}
  
i360__Staff__c[] staffMembers = [SELECT Id, Name 
                             FROM i360__Staff__c 
                             WHERE Name IN :referralStoreNames]; 

Map<String,i360__Staff__c> staffMap = new Map<String,i360__Staff__c>();
for (i360__Staff__c staffMember : staffMembers){
staffMap.put(staffMember.Name,staffMember);
System.debug(LoggingLevel.DEBUG, 'Staff Name:' + staffMember.Name);
}   
for (i360__Prospect__c prospect : prospects){ 
//enter loop if one value is empty, but the store person value is not.
if (prospect.HD_Referral_Store_Our_System__c == null && prospect.Referral_Store__c != null) {
    prospect.HD_Referral_Store_Our_System__c = staffMap.get(prospect.Referral_Store__c.substringBefore(' ')).Id;
}
    
}
}
}

I know it's most likely an issue with the bulkifying but was curious if anyone else was able to spot anything that looked like an issue.

Thanks in advanced.

2
  • can you post the line of error Commented Aug 29, 2017 at 5:23
  • @SantanuBoral Sadly it doesn't return a line, it's an error that's coming off of the data import wizard on the production site. Commented Aug 29, 2017 at 5:31

1 Answer 1

2

Basically, following line will throw NullPointerException

prospect.HD_Referral_Store_Our_System__c = staffMap.get(prospect.Referral_Store__c.substringBefore(' ')).Id;

As staffMap may not contain that key.

To avoid that add this checking

for (i360__Prospect__c prospect : prospects){ 
        //enter loop if one value is empty, but the store person value is not.
            if (prospect.HD_Referral_Store_Our_System__c == null && prospect.Referral_Store__c != null 
            && staffMap.containsKey(prospect.Referral_Store__c.substringBefore(' '))) 
            {
                prospect.HD_Referral_Store_Our_System__c = staffMap.get(prospect.Referral_Store__c.substringBefore(' ')).Id;
            }

        }
1
  • Ah, thanks so much! Didn't realize I had missed that, that completely fixed the issue. Commented Aug 29, 2017 at 5:45

You must log in to answer this question.

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