You need to sign in to do that
Don't have an account?

FIrst Error: Attemp to de-reference a null object
I am trying to send an email in batches when someone adds a comment to a case or someone is added as a case member. The code has simply been mofified for the content of the email. Within the sandbox the emails process correctly but when I deployed to production i got an error. First error: Attemp to de-reference a null object. Any help would be appreciated as I am new to Apex Code.
global class CaseEmailBatch implements Database.Batchable<sObject> { global Database.QueryLocator start(Database.BatchableContext BC) { DateTime runDate = System.Now().addMinutes(-30); return Database.getQueryLocator('SELECT CreatedById,CreatedDate,Id,LastModifiedById,LastModifiedDate,MemberId,ParentId,SystemModstamp,TeamRoleId,TeamTemplateMemberId FROM CaseTeamMember WHERE CreatedDate >: runDate '); } global void execute(Database.BatchableContext BC,List<sObject> scope) { List<Messaging.Singleemailmessage> emails = new List<Messaging.Singleemailmessage>(); Set<ID> caseIds = new Set<ID>(); for(Sobject so: scope){ CaseTeamMember ctm = (CaseTeamMember)so; String caseId = String.valueOf(so.get('ParentId')); caseIds.add(caseId); } List<User> users = new List<User>(); users = [select id, email, name FROM User]; Map<Id, User> uIdUserMap = new Map<Id, User>(); for(User u: users) uIdUserMap.put(u.id, u); List<Case> cases = [select id, CaseNumber, Subject, Description, Account.name, Contact.name, Contact.email, Contact.phone, Origin, CreatedDate, Owner.name, Owner.email FROM Case WHERE id in:caseIds]; Map<Id, Case> caseMap = new Map<Id, Case>(); for(Case c: cases){ caseMap.put(c.id, c); } for(Sobject so: scope){ CaseTeamMember ctm = (CaseTeamMember)so; Case c = caseMap.get(ctm.ParentId); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId(ctm.memberId); mail.setSubject('You have been added as a Case Member :' + c.CaseNumber); String description; if(c.Description.length() > 500){ description = c.Description.substring(0,500); } else{ description = c.Description; } mail.setPlainTextBody('Case Number: ' + c.CaseNumber + '\n' + 'Subject: ' + c.Subject + '\n' + 'Description: ' + c.Description + '\n' + '\n' + 'Account Name: ' + c.Account.Name + '\n' + 'Contact Name: ' + c.Contact.Name + '\n' + 'Contact Email: ' + c.Contact.email + '\n' + 'Contact Phone: ' + c.Contact.phone + '\n' + 'Case Origin: ' + c.Origin + '\n' + 'Date/Time Opened: ' + c.CreatedDate + '\n' + 'Owner Name: ' + c.Owner.Name + '\n' + 'Owner Email: ' + c.Owner.email + '\n' + '\n' + 'Click here to access the case directly: ' + 'https://ssl.salesforce.com/' + c.Id + '\n'); mail.setSaveAsActivity(false); User u = uIdUserMap.get(ctm.CreatedById); mail.setReplyTo(u.email); mail.setSenderDisplayName(u.name); emails.add(mail); } List<CaseComment> cms = new List<CaseComment>(); DateTime runDate = System.Now().addMinutes(-30); cms = [SELECT CommentBody, CreatedById,CreatedDate,Id,IsDeleted,IsPublished,LastModifiedById,LastModifiedDate,ParentId, Parent.CaseNumber, Parent.Description, Parent.Subject, Parent.Account.name, Parent.Contact.name, Parent.Contact.email, Parent.Contact.phone, Parent.Origin, Parent.CreatedDate, Parent.Owner.name, Parent.Owner.email, Parent.Id FROM CaseComment WHERE CreatedDate >: runDate]; Set<ID> parentIds = new Set<ID>(); Map<Id, CaseComment> caseCommentMap = new Map<Id, CaseComment>(); for(CaseComment cm: cms){ caseCommentMap.put(cm.ParentId, cm); parentIds.add(cm.ParentId); } List<CaseTeamMember> commentMembers = new List<CaseTeamMember>(); commentMembers = [SELECT CreatedById,CreatedDate,Id,LastModifiedById,LastModifiedDate,MemberId,ParentId,SystemModstamp,TeamRoleId,TeamTemplateMemberId FROM CaseTeamMember WHERE ParentId in: parentIds]; for(CaseTeamMember ctm: commentMembers){ CaseComment cm = casecommentMap.get(ctm.ParentId); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId(ctm.memberId); mail.setSubject('A new comment has been added to Case :' + cm.Parent.CaseNumber); String description; if(cm.Parent.Description.length() > 500){ description = cm.Parent.Description.substring(0,500); } else{ description = cm.Parent.Description; } mail.setPlainTextBody('Case Number: ' + cm.Parent.CaseNumber + '\n' + 'Subject: ' + cm.Parent.Subject + '\n' + 'Description: ' + cm.Parent.Description + '\n' + '\n' + 'Last Comment: ' + cm.CommentBody + '\n' + '\n' + 'Account Name: ' + cm.Parent.Account.Name + '\n' + 'Contact Name: ' + cm.Parent.Contact.Name + '\n' + 'Contact Email: ' + cm.Parent.Contact.email + '\n' + 'Contact Phone: ' + cm.Parent.Contact.phone + '\n' + 'Case Origin: ' + cm.Parent.Origin + '\n' + 'Date/Time Opened: ' + cm.Parent.CreatedDate + '\n' + 'Owner Name: ' + cm.Parent.Owner.Name + '\n' + 'Owner Email: ' + cm.Parent.Owner.email + '\n' + '\n' + 'Click here to access the case directly: ' + 'https://ssl.salesforce.com/' + cm.Parent.Id); mail.setSaveAsActivity(false); User u = uIdUserMap.get(cm.CreatedById); mail.setReplyTo(u.email); mail.setSenderDisplayName(u.name); emails.add(mail); } Messaging.sendEmail(emails); } global void finish(Database.BatchableContext BC) { } }
The error means you are refering to a List or Collection which did not have values in it . write debug logs after each list and check in debug log files weather all the lists are retrieving values or not.
you didn't highlight the lines where you got this error.
The error showed up on the apex jobs monitoring area. Nothing was rejected in the code itself and the code was working properly in the sandbox.
On the Apex Jobs screen it shows the following:
Submitted Date Job Type Status Status Detail
4/23/2013 2:00PM Batch Apex Completed First error: Attempt to de-reference a null object
Total Batches Batches Processed Failures Apex Class
1 1 1 CaseEmailBatch
I wonder if some of the fields you're querying are null, and thus the methods trying to do their operations are returning the error.
For example, the first candidate I saw was the following line:
If there is no Parent of the CaseTeamMember record, then there's nothing to convert to a string... therefore a null exception would occur. Other lines in the class could be the culprit as well. I'd do some queries of your data to see if any of these fields are blank, and if so you'll need to set up a conditional before you apply the methods.
Debug Logs show the following...
This would put you at this line.
I removed the description string and the batch processed correctly.