• James Sanford 6
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I have an apex class that copies data from a custom field on the close case screen  into case comments when the case is closed, however it is creating the comments twice each time.  Can anyone tell me why it is doing this and how I can fix it?  Below is the apex class;

public without sharing class CaseManagement {
    
    //*****************************************************************************
  //Method called on after update of Case
  //****************************************************************************/
  public static void onAfterUpdate(List<Case> lstCase, Map<Id, Case> oldMap){ 
    checkCloseCase(lstCase, oldMap);   
  } 

  //*****************************************************************************
  //Method called to check the Close Case
  //****************************************************************************/
  public static void checkCloseCase(List<Case> lstCase, Map<Id, Case> oldMap) { 
    
    List<CaseComment> comments = new List<CaseComment>();
               
    for(Case caseUpdate : lstCase) {
            
      if( caseUpdate.Case_Close_Notes_To_Customer__c != null && caseUpdate.Case_Close_Notes_To_Customer__c != oldMap.get(caseUpdate.Id).Case_Close_Notes_To_Customer__c) {
        comments.add( new CaseComment(CommentBody = caseUpdate.Case_Close_Notes_To_Customer__c, IsPublished = false, ParentId = caseUpdate.Id));
      }
    }
    insert comments;
  }
}
I have an apex class that copies data from a custom field on the close case screen  into case comments when the case is closed, however it is creating the comments twice each time.  Can anyone tell me why it is doing this and how I can fix it?  Below is the apex class;

public without sharing class CaseManagement {
    
    //*****************************************************************************
  //Method called on after update of Case
  //****************************************************************************/
  public static void onAfterUpdate(List<Case> lstCase, Map<Id, Case> oldMap){ 
    checkCloseCase(lstCase, oldMap);   
  } 

  //*****************************************************************************
  //Method called to check the Close Case
  //****************************************************************************/
  public static void checkCloseCase(List<Case> lstCase, Map<Id, Case> oldMap) { 
    
    List<CaseComment> comments = new List<CaseComment>();
               
    for(Case caseUpdate : lstCase) {
            
      if( caseUpdate.Case_Close_Notes_To_Customer__c != null && caseUpdate.Case_Close_Notes_To_Customer__c != oldMap.get(caseUpdate.Id).Case_Close_Notes_To_Customer__c) {
        comments.add( new CaseComment(CommentBody = caseUpdate.Case_Close_Notes_To_Customer__c, IsPublished = false, ParentId = caseUpdate.Id));
      }
    }
    insert comments;
  }
}