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));**
}
}
}
}