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 I want to update custom case field when add comment .

my requirement is when i will add comment in case feed comment then in case field which i created (latestComment) show latest comment.

here is my code: for Trigger

trigger latestCommentTrigger on Case (before insert, after update) {
    for(Case c : trigger.new) {
        if(c.Comments == Null){
            c.Comments.adderror('enter comment');
        }
        c.Comments = c.Latest_Comment__c;
        
            }
}


i didnot getting output of this code can any one help me out of this question.
Rohit Kumar SainiRohit Kumar Saini
Hello,

As the DML is happening on CaseComment object ( you are inserting a new CaseComment), we need a trigger on CaseComment object. It can be as simple as below ( assuming you don't have any other trigger on CaseComment).
 
trigger CaseCommentTrigger on CaseComment (after insert) {
    List<Case> casesToUpdate = new List<Case>();
    for(CaseComment cc: Trigger.New){
        Case caseToUpdate = new Case(Id=cc.ParentId, Latest_Comment__c=cc.CommentBody);
        casesToUpdate.add(caseToUpdate);
    }
    update casesToUpdate;
}

Please try it and let me know how it goes. Thanks!