+ Start a Discussion
SF_Admin96SF_Admin96 

APEX Code Help: Multiple entries in Case Comment when clicking Edit and Save.

Hi All,

I currently implemented a code (which I copied somewhere) in my Production which copies the email body and insert it to Case Comment. However, I am getting multiple entries when I edit and Save the record. 

Ex. When the email comes in, it copy the email body and the code insert it to case comment. When I click the Edit button and Save WITHOUT changing any information, it creates a duplicate on the case comment. 

How can I correct this behavior that it should only create case comment when a new email is sent or received?

trigger commentMove on Case (after update) {
  Case myCase = trigger.new[0];
  if (myCase.New_Case_Comment__c!= null) {
    String caseId= myCase.ID;
    CaseComment cc = new CaseComment(CommentBody=myCase.New_Case_Comment__c,parentID=caseId);
    insert cc;
  }
}

Any assistance is greatly appreciated.
Avidev9Avidev9
You can just compare it with trigger.old(which stores the old version of the object edited) for any change 
Something like

trigger commentMove on Case (after update) {
  Case myCase = trigger.new[0];
  if (myCase.New_Case_Comment__c!= null &&  trigger.old[0].New_Case_Comment__c !=myCase.New_Case_Comment__c) {
    String caseId= myCase.ID;
    CaseComment cc = new CaseComment(CommentBody=myCase.New_Case_Comment__c,parentID=caseId);
    insert cc;
  }
}

Please note this trigger is not bulk safe, since you are just picking up only first record from trigger.new
SF_Admin96SF_Admin96
Thanks Avidev,

I will definitely try this one out. I have one question though, what are the cons if the trigger is not bulk safe?
Avidev9Avidev9
If your trigger is not bulk safe it wont work when multiple records get updated in same transaction, by trigger / dataloader or some VF page
SF_Admin96SF_Admin96
Thanks again Avidev. :)
James LoghryJames Loghry
Avidev beat me to it, but your trigger will insert a Case Comment for a Case any time a Case, populated with New_Case_Comment__c is updated.  So if your case is updated every day for instance, you'll have several redundant Case Comment records for that single case.  Also as Avidev mentioned, your trigger only handles a single record, which is a worst practice.  Below is an example, based on Avidev9's code above on how you could both bulkify your trigger and check for redundant case comments.  Note, that I added afterInsert logic as well in case you need to handle that scenario.

trigger commentMove on Case (afterInsert,after update) {
    List<CaseComment> caseComments = new List<CaseComment>();
    for(Case c : Trigger.new){
        if ((Trigger.oldMap == null || Trigger.oldMap.get(c.Id).New_Case_Comment__c != c.New_Case_Comment__c) && c.New_Case_Comment__c != null) {
        caseComments.add(
            new CaseComment(
                CommentBody=myCase.New_Case_Comment__c
                ,parentID=c.Id
            )
        );
    }
    insert caseComments;
}