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
Vidhyasagar RanganathanVidhyasagar Ranganathan 

Append values using trigger

Hi All,

Below is the trigger I got in one of the forums that I modified and works fine. What the trigger does is copies the final approval comment in the approval process and populates in a custom field. Now the requirement is to have comments from all the approval steps to be populated in that custom field including the Approver Name and the Step. 

Thank you

trigger TriggerApprover on zqu__Quote__c (before update) {
    
       if(trigger.isUpdate){
             List<zqu__Quote__c> QuoList =  [Select id,
                                                   (Select  
                                                         ActorId, 
                                                         Comments                                                      
                                                    FROM ProcessSteps
                                                ORDER BY CreatedDate DESC) 
                                                    From zqu__Quote__c
                                                WHERE Id IN : Trigger.new];

             if(QuoList.size() > 0){

               for(zqu__Quote__c Quo : QuoList){
              
                for(zqu__Quote__c Quo1 : Trigger.new) {
                  
                         if(Quo.id == Quo1.id && Quo1.copy_comment__c) {
 
                           if (Quo.ProcessSteps.size() > 0) {
                            
                         Quo1.Approver_Comment__c = Quo.ProcessSteps[0].Comments;
                         Quo1.copy_comment__c = false;
                
                           }

                        }
                 
                    }
               }
             }   
        }  
    }
Best Answer chosen by Vidhyasagar Ranganathan
sowmya Inturi 9sowmya Inturi 9
Hi,
Please modify your code accordingly,
This trigger works fine for me.
trigger AddCommentsToAccount on Account (before update) {
    Set<Id> accIds = new Set<Id>();
    Set<id> piIds = new Set<Id>();
    List<Account> accList= new List<Account>();
    List<ProcessInstance> piList = new List<ProcessInstance>();
    Map<Id,ProcessInstance> piMap= new Map<Id,ProcessInstance>();
    for(Account a: Trigger.new){
        accIds.add(a.id);
    }
    System.debug('accIds'+accIds);
    piList =[select id, 
             status, 
             targetObjectId, 
             createdDate,
             (select id, stepStatus, comments, createdDate from Steps)
             from ProcessInstance where targetObjectId in: accIds order by createddate asc];
    if(piList.size()>0){
        for(ProcessInstance pi:piList)
        {
            piIds.add(pi.id);
            if(pi!=null)
                piMap.put(pi.targetObjectId,pi);
        }
    }
    List<ProcessInstanceStep> instancesSteps = [select Comments,ProcessInstanceId from ProcessInstanceStep where ProcessInstanceId in :piIds];
    String str;
    Map<Id,String> ProcessStepMap = new Map<Id,String>(); 
    for(ProcessInstanceStep pis:instancesSteps){
        system.debug(pis.Comments);
        if(pis.Comments!=null){
            if(str!=null)
                str=str+ ' , '+pis.Comments;
            else
                str=pis.Comments;
        }
        ProcessStepMap.put (pis.ProcessInstanceId, str); 
    }
    
    System.debug('piMap'+piMap);
    System.debug('ProcessStepMap'+ProcessStepMap);
    for(Account a:trigger.new){
        system.debug('piMap.get(a.Id).id'+piMap.get(a.Id).id);
        System.debug('ProcessStepMap.get(piMap.get(a.Id).id)'+ProcessStepMap.get(piMap.get(a.Id).id));
        
        if(trigger.oldMap.get(a.id).Type!=trigger.newMap.get(a.id).Type){
            //  if(trigger.newMap.get(a.id).type.equals('Customer') && a.NumberOfEmployees>500)
            a.Approval_Comments__c=ProcessStepMap.get(piMap.get(a.Id).id);
        }  
       
    }
   
}


Thanks,
Sowmya.

All Answers

sowmya Inturi 9sowmya Inturi 9
Hi,
Please modify your code accordingly,
This trigger works fine for me.
trigger AddCommentsToAccount on Account (before update) {
    Set<Id> accIds = new Set<Id>();
    Set<id> piIds = new Set<Id>();
    List<Account> accList= new List<Account>();
    List<ProcessInstance> piList = new List<ProcessInstance>();
    Map<Id,ProcessInstance> piMap= new Map<Id,ProcessInstance>();
    for(Account a: Trigger.new){
        accIds.add(a.id);
    }
    System.debug('accIds'+accIds);
    piList =[select id, 
             status, 
             targetObjectId, 
             createdDate,
             (select id, stepStatus, comments, createdDate from Steps)
             from ProcessInstance where targetObjectId in: accIds order by createddate asc];
    if(piList.size()>0){
        for(ProcessInstance pi:piList)
        {
            piIds.add(pi.id);
            if(pi!=null)
                piMap.put(pi.targetObjectId,pi);
        }
    }
    List<ProcessInstanceStep> instancesSteps = [select Comments,ProcessInstanceId from ProcessInstanceStep where ProcessInstanceId in :piIds];
    String str;
    Map<Id,String> ProcessStepMap = new Map<Id,String>(); 
    for(ProcessInstanceStep pis:instancesSteps){
        system.debug(pis.Comments);
        if(pis.Comments!=null){
            if(str!=null)
                str=str+ ' , '+pis.Comments;
            else
                str=pis.Comments;
        }
        ProcessStepMap.put (pis.ProcessInstanceId, str); 
    }
    
    System.debug('piMap'+piMap);
    System.debug('ProcessStepMap'+ProcessStepMap);
    for(Account a:trigger.new){
        system.debug('piMap.get(a.Id).id'+piMap.get(a.Id).id);
        System.debug('ProcessStepMap.get(piMap.get(a.Id).id)'+ProcessStepMap.get(piMap.get(a.Id).id));
        
        if(trigger.oldMap.get(a.id).Type!=trigger.newMap.get(a.id).Type){
            //  if(trigger.newMap.get(a.id).type.equals('Customer') && a.NumberOfEmployees>500)
            a.Approval_Comments__c=ProcessStepMap.get(piMap.get(a.Id).id);
        }  
       
    }
   
}


Thanks,
Sowmya.
This was selected as the best answer
Vidhyasagar RanganathanVidhyasagar Ranganathan
Thank you Sowmya, it works very well!