0

I can't figure out what i am missing here - I have this code im using to change lead owner to existing account owner if email domain matches a domain field on account. It works fine - however if a lead comes in without an email im getting an error.

Apex trigger leadOwnerAssignEmailTrigger caused an unexpected exception, contact your administrator: leadOwnerAssignEmailTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.leadOwnerAssignEmailHandler.updateLeadOwner: line 21, column 1

Line 21 is marked with ** in the code below.

public class leadOwnerAssignEmailHandler {
    public static void updateLeadOwner(List<lead> domainList) {

      set<String> domainSet = new set<string>();

     For (lead l:domainList){
      if(l.email !=null && l.email !='')
       domainSet.add(l.Email.split('@').get(1));
     }

      List<Account> accDomainList = [Select Id, OwnerId, domain__c FROM Account 
      WHERE domain__c IN: domainSet];


       Map<String, Id> accountsByDomainMap = new Map<String, Id>();
        if(accDomainList!= null && accDomainList.size()>0) {
            for(Account acc : accDomainList) {
                accountsByDomainMap.put(acc.domain__c,acc.OwnerID);
            }
          }
         for(lead l : domainList) {
            **if(accountsByDomainMap != null && 
        accountsByDomainMap.containskey(l.email.split('@').get(1))) {
                l.ownerID = accountsByDomainMap.get(l.email.split('@').get(1));**
          }
          }
        }
   }
2

Issue is here l.email.split('@').get(1) statement. Seems your email is null here.

Add one more condition to check email is not null. This will solve your issue.

You are just trying to split email with @. If there is not @ and how you will get the First string using get(1)

In this case you will get

System.ListException: List index out of bounds: 1 error

This is the cause I can see so far.

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.