function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jishan royjishan roy 

Trigger for latest comment update then update the case field which is a custom field (Latest Comment).

here is my code:

for(case c : lstCase){
            if(c.Latest_Comment__c==Null||c.Latest_Comment__c!=Null){
                c.Latest_Comment__c=c.Comments;
                
            }
        }


But this will work only when create a new case when update comment in that case it will not getting output.
If anybody know then please help me out of this problem
Thanks in Advance.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jishan,

Have you mention the before update event in the trigger?

If not, keep the before update in the trigger and try.

Thanks!!
jishan royjishan roy
Hii Ankaiah,

Here is my handler class:
public class CaseTriggerHandler {
public static void updateComments(list<case> lstCase){
     
        for(case c : lstCase){
            if(c.Latest_Comment__c==Null||c.Latest_Comment__c!=Null){
                c.Latest_Comment__c=c.Comments;
                
            }
        }
       
        
    }
}

This is trigger:

trigger latestCommentTrigger on Case (after update,after insert,before insert,before update) {
   
    if(trigger.isbefore&&(trigger.isupdate||trigger.isinsert)){
        CaseTriggerHandler.updateComments(trigger.new);
    }

}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Jishan,

You need to write a trigger on casecomment object.

try with below code.
trigger commentsupdate1 on CaseComment (after insert,after update) {
    
    List<case> updatecasecomments = new list<case>();
    for(CaseComment cc:trigger.new){
        system.debug('testinggg'+cc);
        if(cc.CommentBody != null)   {
           system.debug('testinggg=='+cc.CommentBody); 
            case cs = new case();
            cs.id = cc.parentid;
            cs.Latest_Comment__c = cc.CommentBody;
            updatecasecomments.add(cs);
            
        } 
        
        update updatecasecomments;
        system.debug('updatecasecomments=='+updatecasecomments); 
                    
            }

}
If this helps, Please mark it as best answer.

Thanks!!