You need to sign in to do that
Don't have an account?
Anand Avinash Dosapati
Attempt to de-reference a null object: Trigger.Email2CaseSharing
Hello Gurus,
I faced the following exception when I am closing a case. The behaviour with the rest of the cases is normal. Something is missing with this, which I am not able to identify. FYR, I providing the code and the exception. Please help me understand.
Exception/Error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Email2CaseSharing caused an unexpected exception, contact your administrator: Email2CaseSharing: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Email2CaseSharing: line 30, column 1
Code:
I faced the following exception when I am closing a case. The behaviour with the rest of the cases is normal. Something is missing with this, which I am not able to identify. FYR, I providing the code and the exception. Please help me understand.
Exception/Error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Email2CaseSharing caused an unexpected exception, contact your administrator: Email2CaseSharing: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Email2CaseSharing: line 30, column 1
Code:
trigger Email2CaseSharing on Case (before insert, before update, after insert, after update) { Set<Id> contactIds = new Set<Id>(); for(Case c : Trigger.new) { if(c.Origin != null && c.Origin == 'Email' && c.ContactId != null) { contactIds.add(c.ContactId); } } if(!contactIds.isEmpty()) { Map<String, String> contactEmails = new Map<String, String>(); Map<String, User> emails = new Map<String, User>(); for(Contact c : [SELECT Id, Email FROM Contact WHERE Id IN :contactIds]) { contactEmails.put(c.Id, c.Email); emails.put(c.Email, null); } for(User u : [SELECT Id, Email FROM User WHERE Email IN :emails.keySet()]) { emails.put(u.Email, u); } List<CaseShare> shares = new List<CaseShare>(); for(Case c : Trigger.new) { if(c.Origin != null && c.Origin == 'Email' && c.ContactId != null) { Id uid = emails.get(contactEmails.get(c.ContactId)).Id; if(Trigger.isBefore) { c.Submitter__c = uid; } else { CaseShare cs = new CaseShare( CaseId = c.Id, UserOrGroupId = uid, CaseAccessLevel = 'Read' ); shares.add(cs); } } } try { if(!shares.isEmpty()) { insert shares; } } catch(Exception e) { System.debug(e.getStacktraceString()); } } }
Id uid = emails.get(contactEmails.get(c.ContactId)).Id; check this line. this is null. becuase of this line emails.put(c.Email, null);
But on the case, the email of contact and contact Id are not null.
Hi Anand,
You can track each list by using the debug log and use the null check before any operation on list.