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
Malik Butler 5Malik Butler 5 

Lead owner approval

I am trying to make a lead owner change approval process. I thought I was good with the code but nothing is triggering. Can anyone help?

trigger LeadReassignment on Lead (before update) {
    for (Lead obj: trigger.new){


        List<Lead> ld = [SELECT Id, OwnerID, Reassignment_Status__c FROM Lead WHERE Id IN :Trigger.old];

        List<ProcessInstance> pi = [SELECT SubmittedById,SystemModstamp,TargetObjectId, Status,  (SELECT ID, ProcessNodeId, StepStatus, Comments,TargetObjectId,ActorId,CreatedById,IsDeleted,IsPending, OriginalActorId,ProcessInstanceId,RemindersSent,CreatedDate 
        FROM StepsAndWorkitems ) FROM ProcessInstance pi WHERE pi.TargetObjectId IN :Trigger.old order by createddate DESC limit 1];


        If (ld[0].Reassignment_Status__c == 'Pending' && pi[0].Status == 'Approved') {
            obj.OwnerID = pi[0].SubmittedById;
            obj.Reassignment_Status__c = '';
            system.debug('Reassignment Approved - Owner Changed from '+ld[0].OwnerId+'to ' +pi[0].SubmittedById);
                                                        }

         If (ld[0].Reassignment_Status__c == 'Pending' && pi[0].Status == 'Rejected') {
            obj.Reassignment_Status__c = '';

            system.debug('Reassignment Rejected - Owner not changed');

                                                         }
 
ShirishaShirisha (Salesforce Developers) 
Hi Malik,

Greetings!

I would suggest you to capture the debug logs to see which part of the code is not firing.Also,please refer the below blog for sample code on Lead Approval process for owner change:

https://www.jitendrazaa.com/blog/salesforce/dynamic-approval-process-based-on-the-apex-and-trigger/

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri