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
Victor19Victor19 

Copy Information from Approval History into custom fields

Hi,

 

I am trying to copy the information (Approver, Comments etc) from the Standard Approval Process Related list into custom fields. My debug statements show values but fields are not getting populated with the values. I would really appreciate if someone could suggest a fix to my code.

 

trigger Test_Approval on Test_Obj_1__c (after insert, after update) {
    Set <id> ids = new Set <id>();
        for(Test_Obj_1__c tes1 : Trigger.new){
         ids.add(tes1.id);
System.debug('zz ids: ' + ids);
    }
    
 
    List<Test_Obj_1__c> testlist = [SELECT e.Id, e.Test_Approver__c, e.Comments__c, (Select Id, IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp From ProcessSteps)
                                            FROM Test_Obj_1__c e where id=:ids];
    
    if(testlist.size()>0)
    {
        Test_Obj_1__c fund = testlist[0];
 

        for (ProcessInstanceHistory ps : tobj.ProcessSteps)
        {
            tobj.Test_Approver__c = ps.ActorId;
            tobj.Comments__c = ps.Comments;
            System.debug('zz Approver: ' + tobj.Test_Approver__c);
            System.debug('zz Comments:' + tobj.Comments__c);

        }
    }
}

 

Thanks!

TheDoctorTheDoctor

I'm not seeing any Update statement here that would actually commit the values to the record...

Victor19Victor19
Hi TheDoctor,

I put in an update statement. But even then I don't see a difference. Can you please point out where I am going wrong. Below is the updated code:

trigger Test_Approval on Test_Obj_1__c (after insert, after update) {
Set <id> ids = new Set <id>();
for(Test_Obj_1__c tes1 : Trigger.new){
ids.add(tes1.id);
System.debug('zz ids: ' + ids);
}


List<Test_Obj_1__c> testlist = [SELECT e.Id, e.Test_Approver__c, e.Comments__c, (Select Id, IsPending, ProcessInstanceId, TargetObjectId, StepStatus, OriginalActorId, ActorId, RemindersSent, Comments, IsDeleted, CreatedDate, CreatedById, SystemModstamp From ProcessSteps)
FROM Test_Obj_1__c e where id=:ids];
List<Test_Obj_1__c> updatelist = new List<Test_Obj_1__c>();

if(testlist.size()>0)
{
Test_Obj_1__c tobj = testlist[0];


for (ProcessInstanceHistory ps : fund.ProcessSteps)
{
tobj.Test_Approver__c = ps.ActorId;
tobj.Comments__c = ps.Comments;
System.debug('zz Approver: ' + tobj.Test_Approver__c);
System.debug('zz Comments:' + tobj.Comments__c);
updatelist.add(tobj);
}
update updatelist;
}
}
gbu.varungbu.varun

Hi Victor,

 

I am not sure but you can find Approval History from the Approval Object. To Copy it you can check Parent IT and Status. You can do it by trigger easily.

 

 

Thanks,

Victor19Victor19
Varun-

Thanks for the response! I am able to copy the actorid but not the comments. Do you have some sample code that you could share with me?

Vic!
Victor19Victor19
Varun- I have set up the trigger. But still I am not able to copy the values. I would really appreciate if you could suggest a fix or point out what I am missing in the trigger.


trigger testobj1_Approval_Info on Test_Object1__c (before update) {
public Test_Object1__c currentRequest {get;set;}
Set <id> ids = new Set <id>();
for(Test_Object1__c testobj1 : Trigger.new){

ids.add(testobj1.id);
System.debug('zz ids: ' + ids);
}

List<ProcessInstance> listPI = [Select Id, (Select Id, ActorId, Actor.Name, Comments, StepStatus from Steps where StepStatus = 'Approved' or StepStatus='Rejected' order by CreatedDate) from ProcessInstance where TargetObjectId in :ids];

System.debug('Comments Size: ' + listPI.size());
System.debug('Comments Size: ' + listPI);

if(!listPI.isEmpty()){
list<ProcessInstanceStep> stepss = listPI[0].Steps;


System.debug('Comments Size: ' + stepss.size());
System.debug('Comments Size: ' + stepss);
}
for(ProcessInstance psi : listPI){
//System.debug('zz Comments: ' + psi.comments);
for(ProcessInstanceStep ppt: psi.Steps){
System.debug('zz Comments: ' + ppt.comments);
// for(Test_Object1__c tobj1 : Trigger.new){
//Test_Object1__c oldtestobj1 = Trigger.oldMap.get(tobj1.id);

//if(tobj1.Request_Approval_Status__c != oldtestobj1.Request_Approval_Status__c){
currentRequest.Test_Approver__c = ppt.ActorId;
currentRequest.Comments_Requestor__c = ppt.Comments;
System.debug('zz Approver: ' + ppt.ActorId);
System.debug('zz Approver: ' + ppt.stepStatus);
System.debug('zz Comments:' + ppt.Comments);
}

}
TheDoctorTheDoctor

I'm thinking this is due to the Order of Execution. Approval Processes (which fall under the category of Workflow) are executed after triggers. I think  this might be your problem.

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm