• Hightower
  • NEWBIE
  • 10 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies

I received a report of something weird happening on cases yesterday where the Description has been converted to special characters. Modifications were not made to the description they only added comments to the case. I have not a clue what's going on so any help would be great.

 

For about the last 5 work days or so, there have been no leads to pull over from, that’s very unusual.  I’ve verified with the sales team that they are sending them.  Please verify that this tool is working correctly.  Thanks.

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)
  {
    
  }
  
}

 

I received a report of something weird happening on cases yesterday where the Description has been converted to special characters. Modifications were not made to the description they only added comments to the case. I have not a clue what's going on so any help would be great.

 

For about the last 5 work days or so, there have been no leads to pull over from, that’s very unusual.  I’ve verified with the sales team that they are sending them.  Please verify that this tool is working correctly.  Thanks.

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)
  {
    
  }
  
}