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
Bryan RevelantBryan Revelant 

Apex Trigger on Reassign Approvals

Apex Trigger on Reassign Approvals,

I have created an Add error on if the users try and reassign to a different approver. The code however doesnt seem to fire on reassign. I thought that If i said that the orginalActorid deosnt match the Actorid then fire a adderror when clicked.

Then I thought that if steps = reassigned value then through error but it doesnt fire either. Any help would be great. i would rather not create a VF but i guess i could try if this is not possible.

trigger ADRUserReassign on ADRUser__c (after update) {
 

public String ADendid;

       list<ADRUser__c> ad =[Select id, Name, User__c from ADRUser__c where id =:ADendid];
        list<ADRUser__c> ad2Update = new list<ADRUser__c>();
        map<id, ADRUser__c> ADaddMap = new map<id, ADRUser__c>([Select id from ADRUser__c where id =:ADendid]);
        list<ProcessInstance> PI = new list<ProcessInstance>();
        map<id, id> PIVaddMap = new map<id, id>();
       
        system.debug(' ID Value on Target Object ' + ADaddMap);
                       
      
        for (ProcessInstance lstProcess : [Select id, Status, CreatedDate, TargetObjectId,
                                    (Select Id,ProcessInstanceId, StepStatus,
                                        Comments,OriginalActorId,ActorId
                                    From steps order by CreatedDate DESC)
                                From ProcessInstance 
                                where targetobjectid=:ADendid order by CreatedDate DESC]){
            PI.add(lstProcess);
            PIVaddMap.put(lstProcess.TargetObjectId, lstProcess.id);
           
            }
       
   
      
        for(ProcessInstance lstPI :  PI){
     //  if(lstPI.Status=='Reassigned'){
        for(ProcessInstanceStep lstSteps : lstPI.Steps){
    //    if(lstSteps.OriginalActorId != lstSteps.ActorId){
            if(lstSteps.StepStatus == 'Reassigned'){
     lstPI.addError('You cannot reassign - try again');
    id ADRUserAddId = PIVaddMap.get(lstPI.TargetObjectId);
       ADRUser__c AAdd =  ADaddMap.get(lstPI.TargetObjectId);
       lstPI.addError('You cannot reassign - try again');
//         if (lstSteps.OriginalActorId != lstSteps.ActorId){

//  lstSteps.addError('You cannot reassign - try again');
        }
            }
                }
                }
     //           }
              //  } //End void
//}//End main
Vinit_KumarVinit_Kumar
I don't see any trigger context variable ex. trigger.new in your Trigger code.How are ayou accessing the new value if you are not using Trigger context variable ??

As per the salesforce docs addError() method works only with Trigger.new and Trigger.old.Go through the link to learn more :-

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm#apex_System_SObject_addError

Bryan RevelantBryan Revelant
Actually, I think that this isnt going to work because the Trigger is on my ADRUser object. It seems you cannot have a trigger on the Approval Step object. Unfortunately, I cant have a trigger on the approval object because it would effect all Approvals on all Apps. I dont see a way that this can be done.
Vinit_KumarVinit_Kumar
Bryan,

Go through the below link which might help you :-

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008xsiIAA
Bryan RevelantBryan Revelant
I currently already have something like that. However Approvers are not allowed to reassign. If you tap into the Approval Object like the Link you sent you still run into the reassign link. I dont see a way around it out side of a VF or just scrubbing the whole approval object.
indyindy
Bryan -  I am search for similar solution, any luck on how to stop the user to reassign approval process. Please share if you have any solution.

Thanks,
Indrasen
Bryan RevelantBryan Revelant
well that was 4 years ago but I would think that you would soql the current approver then update something on the object you could then throw an error
indyindy
Bryan - Thank You for your quick reply.  Could you please eloborate the solution. I am in deadlock situation now.

Thanks,
Indy