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
huskerwendyhuskerwendy 

Trigger on Event object on Live Salesforce causing NullPointerException

I pushed a trigger live a couple of weeks ago and it seemed to be working fine. But in the last two days, I've received 11 emails notifying me of an error. I've tested and can't duplicate the error and I'm not sure what's causing it. I think it must be an automated process that's trying to create an event because none of my users have complained about the error. How can I determine what is causing the error and how can I fix it?

 

Here's the error: 

 

Trigger.EventIndustry: line 28, column 38

Apex script unhandled trigger exception by user/organization: 00560000000m32k/00D600000006nRH

EventIndustry: execution of BeforeInsert

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

 

Here's the code:

trigger EventIndustry on Event (before insert, before update) {
	if(trigger.isInsert){
        //create a set of all unique WhatIDs and WhoIDs
        set<id> WhoIDs = new Set<id>();
        Set<id> WhatIds= new Set<id>();
        Set<id> AccountIds= new Set<id>();
        
        for (Event t : trigger.new){
        	if (String.valueOf(t.whoId)!= null){
               	WhoIDs.add(t.WhoID); 
        	}else if (String.valueOf(t.WhatID)!= Null){
        		WhatIds.add(t.WhatID);
         	}else if (String.valueOf(t.AccountID)!= Null){
        		AccountIds.add(t.AccountID);
        	}
        }   
        //create a map for a hash table for the industry
       	Map<id, Lead> who = new Map<id, Lead>([SELECT Industry FROM Lead WHERE ID in :WhoIDs]);     
	   	Map<Id, Account> acct = new Map<Id, Account>([Select Industry From Account Where ID in :AccountIds OR ID in: WhatIDs]);
	   	Map<id, Contact> con = new Map<id, Contact>([SELECT Account.Industry from Contact WHERE ID in :WhoIDs]);
	   	Map<id, Opportunity> opp = new Map<id, Opportunity>([Select Account.Industry From Opportunity Where Id in :WhatIDs]);	
		
		// iterate over the list of records being processed in the trigger and set the industry
		for (Event a : Trigger.new){
			if (!who.isEmpty()){
		    	a.Industry__c = who.get(a.WhoId).Industry;
			} else if (!con.isEmpty()){
				a.Industry__c = con.get(a.WhoId).account.Industry;
			} else if (!opp.isEmpty()){
				a.Industry__c = opp.get(a.WhatId).account.Industry;
			} else if (acct != null && !acct.isEmpty()){
					if(acct.ContainsKey(a.whatID)){
						a.Industry__c = acct.get(a.whatID).Industry;
					}else if(acct.ContainsKey(a.AccountId)){
						a.Industry__c = acct.get(a.AccountId).Industry;
					}
			}
		}
	} else {
		if(trigger.isUpdate){
			 //create a set of all unique ids 
	        set<id> WhoIDs = new Set<id>();
	        Set<id> WhatIds= new Set<id>();
	        Set<id> AccountIds= new Set<id>();
			
			for (Event t : Trigger.new){
				if (Trigger.oldMap.get(t.Id).WhoId != Trigger.newMap.get(t.Id).WhoId){
					WhoIDs.add(t.WhoID); 
				}else if (Trigger.oldMap.get(t.Id).WhatID != Trigger.newMap.get(t.Id).WhatID){
					WhatIds.add(t.WhatID);
				}else if (Trigger.oldMap.get(t.Id).AccountID != Trigger.newMap.get(t.Id).AccountID){
					AccountIDs.add(t.AccountID);
				}
			}
			
			//create a map for a hash table for the industry
       		Map<id, Lead> who = new Map<id, Lead>([SELECT Industry FROM Lead WHERE ID in :WhoIDs]);     
	   		Map<Id, Account> acct = new Map<Id, Account>([Select Industry From Account Where ID in :AccountIds OR ID in: WhatIDs]);
	   		Map<id, Contact> con = new Map<id, Contact>([SELECT Account.Industry from Contact WHERE ID in :WhoIDs]);
	   		Map<id, Opportunity> opp = new Map<id, Opportunity>([Select Account.Industry From Opportunity Where Id in :WhatIDs]);	
			
			for (Event a : Trigger.new){
				if (!who.isEmpty()&& who.ContainsKey(a.WhoId)){
			    	a.Industry__c = who.get(a.WhoId).Industry;
				} else if (!con.isEmpty()){
					a.Industry__c = con.get(a.WhoId).account.Industry;
				} else if (!opp.isEmpty()){
					a.Industry__c = opp.get(a.WhatId).account.Industry;
				} else if (acct != null && !acct.isEmpty()){
					if(acct.ContainsKey(a.whatID)){
						a.Industry__c = acct.get(a.whatID).Industry;
					}else if(acct.ContainsKey(a.AccountId)){
						a.Industry__c = acct.get(a.AccountId).Industry;
					}
				}
			}
		}		
	}
}

 

Thanks,

 

Wendy

homer671homer671

 

I think you have to make sure here that your not looking for whoid when you should be looking for whatid.   On a case of batched you might have contacts and opportunities within the same transaction of events. 

That's probably causing null.  Try to create a list of 2 events.  One with a.whoId set and one with a.WhatID set and insert them together.  It should error out becaus it's only looking if con.isEmpty which it is not.   

 

It might be better to do a if statement something like:

 

else if (!con.isEmpty() && con.get(a.whoId) != null )

 

It ensures that it's doing the right part and the value is not null.   If it is a whatId in a batch it will skip over this line.   



if (!who.isEmpty()){
		    	a.Industry__c = who.get(a.WhoId).Industry;
			} else if (!con.isEmpty()){
				a.Industry__c = con.get(a.WhoId).account.Industry;
			} else if (!opp.isEmpty()){
				a.Industry__c = opp.get(a.WhatId).account.Industry;
			} else if (acct != null && !acct.isEmpty()){
					if(acct.ContainsKey(a.whatID)){
						a.Industry__c = acct.get(a.whatID).Industry;
					}else if(acct.ContainsKey(a.AccountId)){
						a.Industry__c = acct.get(a.AccountId).Industry;
					}
			}
huskerwendyhuskerwendy

Thanks, homer. I'll give it a try.

yashagarwalyashagarwal

if the problem is not solved yet ,  just try the system log. Click on your username on the top , it would display a system log option in the drop down.

 

then try inserting the object , there will be a log recorded. Click on the log and Ctrl +F (exception) , it would show the exact line throwing the exception.

 

Hope that helps.

b-Forceb-Force
trigger EventIndustry on Event (before insert, before update) {
	if(trigger.isInsert){
        //create a set of all unique WhatIDs and WhoIDs
        set<id> WhoIDs = new Set<id>();
        Set<id> WhatIds= new Set<id>();
        Set<id> AccountIds= new Set<id>();
        
        for (Event t : trigger.new){
        	if (t.whoId !=null && String.valueOf(t.whoId)!= null){
               	WhoIDs.add(t.WhoID); 
        	}else if (t.WhatID !=null && String.valueOf(t.WhatID)!= Null){
        		WhatIds.add(t.WhatID);
         	}else if (t.AccountID !=null && String.valueOf(t.AccountID)!= Null){
        		AccountIds.add(t.AccountID);
        	}
        }   
        //create a map for a hash table for the industry
       	Map<id, Lead> who = new Map<id, Lead>([SELECT Industry FROM Lead WHERE ID in :WhoIDs]);     
	   	Map<Id, Account> acct = new Map<Id, Account>([Select Industry From Account Where ID in :AccountIds OR ID in: WhatIDs]);
	   	Map<id, Contact> con = new Map<id, Contact>([SELECT Account.Industry from Contact WHERE ID in :WhoIDs]);
	   	Map<id, Opportunity> opp = new Map<id, Opportunity>([Select Account.Industry From Opportunity Where Id in :WhatIDs]);	
		
		// iterate over the list of records being processed in the trigger and set the industry
		for (Event a : Trigger.new){
			if (!who.isEmpty()){
		    	a.Industry__c = who.get(a.WhoId).Industry;
			} else if (!con.isEmpty()){
				a.Industry__c = con.get(a.WhoId).account.Industry;
			} else if (!opp.isEmpty()){
				a.Industry__c = opp.get(a.WhatId).account.Industry;
			} else if (acct != null && !acct.isEmpty()){
					if(acct.ContainsKey(a.whatID)){
						a.Industry__c = acct.get(a.whatID).Industry;
					}else if(acct.ContainsKey(a.AccountId)){
						a.Industry__c = acct.get(a.AccountId).Industry;
					}
			}
		}
	} else {
		if(trigger.isUpdate){
			 //create a set of all unique ids 
	        set<id> WhoIDs = new Set<id>();
	        Set<id> WhatIds= new Set<id>();
	        Set<id> AccountIds= new Set<id>();
			
			for (Event t : Trigger.new){
				if (Trigger.oldMap.get(t.Id).WhoId != Trigger.newMap.get(t.Id).WhoId){
					WhoIDs.add(t.WhoID); 
				}else if (Trigger.oldMap.get(t.Id).WhatID != Trigger.newMap.get(t.Id).WhatID){
					WhatIds.add(t.WhatID);
				}else if (Trigger.oldMap.get(t.Id).AccountID != Trigger.newMap.get(t.Id).AccountID){
					AccountIDs.add(t.AccountID);
				}
			}
			
			//create a map for a hash table for the industry
       		Map<id, Lead> who = new Map<id, Lead>([SELECT Industry FROM Lead WHERE ID in :WhoIDs]);     
	   		Map<Id, Account> acct = new Map<Id, Account>([Select Industry From Account Where ID in :AccountIds OR ID in: WhatIDs]);
	   		Map<id, Contact> con = new Map<id, Contact>([SELECT Account.Industry from Contact WHERE ID in :WhoIDs]);
	   		Map<id, Opportunity> opp = new Map<id, Opportunity>([Select Account.Industry From Opportunity Where Id in :WhatIDs]);	
			
			for (Event a : Trigger.new){
				if (!who.isEmpty()&& who.ContainsKey(a.WhoId)){
			    	a.Industry__c = who.get(a.WhoId).Industry;
				} else if (!con.isEmpty()){
					a.Industry__c = con.get(a.WhoId).account.Industry;
				} else if (!opp.isEmpty()){
					a.Industry__c = opp.get(a.WhatId).account.Industry;
				} else if (acct != null && !acct.isEmpty()){
					if(acct.ContainsKey(a.whatID)){
						a.Industry__c = acct.get(a.whatID).Industry;
					}else if(acct.ContainsKey(a.AccountId)){
						a.Industry__c = acct.get(a.AccountId).Industry;
					}
				}
			}
		}		
	}
}

 try this code 

 

 

Thanks,

Bala

huskerwendyhuskerwendy

I ended up changing this code

 

} else if (!con.isEmpty()){
				a.Industry__c = con.get(a.WhoId).account.Industry;

 to this 

} else if (!con.isEmpty()){
				if(con.ContainsKey(a.WhoId)){
					a.Industry__c = con.get(a.WhoId).account.Industry;
				}

 and it seems to have fixed the problem for now. At least I haven't received any errors.

 

Thanks for everyone's help.

 

Wendy

Coupa Sales OperationsCoupa Sales Operations
Hi Wendy, 

Just grabbed this code for our instance - its great!  Could you please let me know what you're using for the test class?  I have the basics (create account, contact & event) but I'm not sure what I need to do to get the right test coverage.

Thanks!
Tiffanie