+ Start a Discussion
KCLKCL 

Approval comments

I have a custom object to store Comments(mattercomments__c)

The only issue is we have 2 approvers for one Matter__c record. when the first approver writes a comment , the comment does not go into the 
Approver_Comment__c (long text ) field in the custom Matter__c object. When the second or last approver writes a comment, it goes into the Approver_Comment__c (long text ) field in the custom Matter__c object.  

This there a way to modify the code below where when both approver writes a comment, it goes into the Approver_Comment__c (long text ) field in the custom Matter__c object? 
List<Matter__c> matList =  [Select id, Approver_Comments__c,
                                                   (Select Id, 
                                                         IsPending, 
                                                         ProcessInstanceId, 
                                                         TargetObjectId, 
                                                         StepStatus, 
                                                         OriginalActorId, 
                                                         ActorId, 
                                                         RemindersSent, 
                                                         Comments, 
                                                         IsDeleted, 
                                                         CreatedDate, 
                                                         CreatedById, 
                                                         SystemModstamp 
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                                    From Matter__c
                                                WHERE Id IN : Trigger.new];
                                               

        if(matList.size() > 0){
           List<mattercomments__c  > notes_new = new List<mattercomments__c  >();
            for(Matter__c mat : matList){
                for(Matter__c mat1 : newlist) {
               
                //check copy comment is true
                if(mat.id == mat1.id) {
                     if (mat.ProcessSteps.size() > 0) {
                        mat1.Approver_Comments__c  = mat.ProcessSteps[0].Comments;
                     }

                }
                 
                if(mat1.Approver_Comments__c   != mat.Approver_Comments__c   && !mat.ProcessSteps.isEmpty() && (mat.ProcessSteps[0].StepStatus == 'Approved' || mat.ProcessSteps[0].StepStatus == 'Rejected')){
                    notes_new.add(new mattercomments__c  (Matterr__c__c= mat.id, Steps__c =mat.ProcessSteps[0].Comments, Type__c = mat.ProcessSteps[0].StepStatus+' Comments'));     
                    
                 }
                 
                    }
               }
                if(!notes_new.isEmpty()) {            
                        insert notes_new; 

                        } 
             }

 
David Zhu 🔥David Zhu 🔥
You can use For Update Clause to lock the record in your trigger, the lock continues until the transaction is complete. That would prevent the second approver's comments overriding the first approver's comments. 
List<Matter__c> matList =  [Select id, Approver_Comments__c,
                                                   (Select Id, 
                                                         IsPending, 
                                                         ProcessInstanceId, 
                                                         TargetObjectId, 
                                                         StepStatus, 
                                                         OriginalActorId, 
                                                         ActorId, 
                                                         RemindersSent, 
                                                         Comments, 
                                                         IsDeleted, 
                                                         CreatedDate, 
                                                         CreatedById, 
                                                         SystemModstamp 
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                                    From Matter__c
                                                WHERE Id IN : Trigger.new FOR UPDATE];

 
KCLKCL
I am usign apex class 
David Zhu 🔥David Zhu 🔥
It is the same. the key is adding FOR Update at the end of soql.

List<Matter__c> matList =  [Select id, Approver_Comments__c, ...... where ID in :Trigger.new FOR UPDATE];
KCLKCL
still it is stroing last approver comment, we need to store the both approver comments seperated by approver name or comma like
KCLKCL
I am going with above code but when I try to mass update the records I am getting "Apx CPU limit error"