You need to sign in to do that
Don't have an account?
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
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.
Thanks, homer. I'll give it a try.
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.
try this code
Thanks,
Bala
I ended up changing this code
to this
and it seems to have fixed the problem for now. At least I haven't received any errors.
Thanks for everyone's help.
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