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
StaciStaci 

update case comment and case in trigger

I'm looking to create a case comment and update some of the case fields in a trigger.  This is what I have so far, but I get an error saying the record is read-only ( These lines c.Status = 'Pending'; c.Priority = 'Low'; c.CW_Type__c = 'Task';)
 
trigger CW_DSS_CaseTrigger on Case (after update) {
  if(CWcheckRecursive.runOnce())
    {
    
   List<Case> cwCasesUpdated = new List<Case>();
   List<caseComment> caseCommentList = new List<caseComment>();
    for (Case c: Trigger.new)
    {
        
        caseComment cc = new caseComment();
        if(c.Macros__c == 'Application Support information'){
                cc.parentid = c.ID;
                cc.commentbody = 'For application (DSSi and Relay Server) support and troubleshooting requests, please provide the following information.'+'\n' +
                                    'Minimum DSS Ticket Information requirements:'+'\n' +
                                    'Site Name:' +'\n' +
                                    'Site Contact: (For possible Site IT related issues)' +'\n' +
                                    'DSSi Software Version:' +'\n' +
                                    'Problem/Inquiry:' +'\n' +
                                    'Troubleshooting Steps Taken:' +'\n' +
                                    'This information is required with all DSS support requests. Failure to provide this requested information may delay your request.';
                cc.ispublished = true;
                cwCasesUpdated.add(c); 
                casecommentlist.add(cc);
               
        }
        if(c.Macros__c == 'Blackbox - Out of Scope'){
                cc.parentid = c.ID;
                cc.commentbody = 'Blackbox data requests can only be processed for incidents that occur involving driver fatigue, personal or property damage' +'\n' +
                'and injury related to fatigue or distraction and operator tampering.  The DSS is not to be used for disciplinary actions for mine safety violations.'+'\n' +
                'This eliminates sites wanting blackbox video for mine safety infractions, evidence of medical related injuries or diagnostic reasons.' +'\n' +
                 'This is to only be used if necessary to add evidence, clarify or discovery information about an incident or accident involving fatigue and/or distraction.';
                cc.ispublished = true;
                c.Status = 'Pending';
                c.Priority = 'Low';
                c.CW_Type__c = 'Task';
                casecommentlist.add(cc);
               
        }
       
    }
            insert caseCommentList;
            
          
    }
}

 
RituSharmaRituSharma
Your trigger is written for after update event and it does not allow to update the current record. So split your logic into before update and after update. In before update, update case fields and in after update, create case comments.
AbhishekAbhishek (Salesforce Developers) 
You can try the code as mentioned in the below developer discussions,

https://trailblazers.salesforce.com/answers?id=9063A000000iluwQAA

https://developer.salesforce.com/forums/?id=906F00000008yMqIAI

I hope it helps.