How-To Show Current Approver on Target Object

November 27, 2024

The issue arises from some lacklustre approval reporting functionality in Salesforce. This solution is great, as you can see who the current approver is on the object you created the Flow for – this way, the reporting is much more robust, and you can report on the specific object.

Follow this guide on how you can get the Current Assigned To from the Approval History window, onto a look up field on your record.

Verify and Create Custom Fields

Before building the Flow, ensure that the necessary custom fields exist on your target object. These fields will store the current approver, pending approver and recent timestamp on approval steps.

 

  • Navigate to Object Manager:
    • Go to Setup
    • Click on Object Manager.
    • Select Object (Opportunity, Quote, Contract, etc.,)
  • Create Custom Fields
    • Go to Fields & Relationships.
  • Create “Current Assigned To” Field:
    • Click New.
    • Select Lookup Relationship as the data type.
    • Click Next.
    • Choose User as the related object.
    • Click Next.
    • Field Label: Current Assigned To
    • Field Name: Current_Assigned_To__c
    • Save.
  • Create “ Last Approval Step Timestamp” Field:
    • Field Label: Last Approval Step Timestamp
    • Field Name: Last_Approval_Step_Timestamp__c
    • Select Date/Time as the data type.
    • This field keeps track of when the approval process moves to a new step, so the Flow can know and react to these changes.
  • Create “Pending Approver” Field (Optional):

This is optional but will display the upcoming approver after the current assigned approver.

    • Repeat the above steps to create another Lookup Relationship field.
    • Field Label: Pending Approver
    • Field Name: Pending_Approver__c
    • Click Save.

Set up the necessary field-level permissions, such as read-only on the page layout for these fields, so that the end user cannot edit them.

 

 

Configure your Approval Process to stamp Date/Time field

For each approval step in your Approval Process, add a Field Update action that sets Last_Approval_Step_Timestamp__c to NOW(). This way, the timestamp updates every time the approval process moves forward, triggering the Flow.

 

  1. Navigate to Approval Processes:
    • Go to Setup → Approval Processes → [Your Specific Approval Process for Your Object].
  2. Edit Each Approval Step:
    • Click Edit next to an approval step.
  3. Add Field Update Action:
    • Scroll to Approval Actions.
    • Click Add New → Field Update.
    • Action Name: Update Last Approval Step Timestamp
    • Field to Update: Last_Approval_Step_Timestamp__c
    • Specify New Field Value:
      • Use a Formula: NOW()
    • Click Save.
  4. Repeat: Do this for each approval step within the Approval Process.

 

 

Set Up Necessary Flow Variables

Flow variables are super important for storing data in a Flow. We’ll set up Record Variables to keep track of the records we get from Get Records elements.

  1. Navigate to Flow Builder:
    • Go to Setup.
    • In the Quick Find box, type Flows and select Flows.
    • Click New Flow.
    • Choose Record-Triggered Flow and click Create.
  2. Access Flow Manager:
    • In the Flow Builder, click on the Manager tab on the left sidebar.
  3. Create Record Variables:
  • ApprovalProcessInstance
    • Click New Resource.
    • Resource Type: Variable
    • API Name: ApprovalProcessInstance
    • Data Type: Record
    • Object: ProcessInstance
    • Allow multiple values (collection): Unchecked
    • Available for Input/Output: Unchecked
  • CurrentApprovalWorkitem
    • Click New Resource.
    • Resource Type: Variable
    • API Name: CurrentApprovalWorkitem
    • Data Type: Record
    • Object: ProcessInstanceWorkitem
    • Allow multiple values (collection): Unchecked
    • Available for Input/Output: Unchecked
  • NextApprovalWorkitem
    • Click New Resource.
    • Resource Type: Variable
    • API Name: NextApprovalWorkitem
    • Data Type: Record
    • Object: ProcessInstanceWorkitem
    • Allow multiple values (collection): Unchecked
    • Available for Input/Output: Unchecked

 

Create the Record-Triggered Flow

Set up the Flow to fire when a Contract record gets updated, so we can start the approval process.

  • Configure the Trigger:
    • Object: Opportunity, Quote, Contract, etc.,
    • Trigger the Flow When: A record is updated
    • Set Entry Conditions (example below):
      • Field: Approval_Status__c
      • Operator: Equals
      • Value: Pending Approval
      • Using an Approval Status field is crucial, as we want to make ensure the fire only flows when it’s in a Pending Approval status.
    • Condition Requirements: Depending on your fields, you may need to add more fields as required and change this accordingly
    • When to Run the Flow for Updated Records:
      •  Every time a record is updated and meets the condition requirements
    • Optimize the Flow For: Actions and Related Records, which runs after the record is saved

This ensures that the Flow activates only when intended, preventing unintended executions.

  • Create new Scheduled Path
    • Path Label: Scheduled Update (or your preferred name)
    • API Name: Scheduled_Update
    • Time Source: Contract: Last Approval Step Timestamp
    • Offset Number: 1
    • Offset Options: Minutes After

Adding to the Scheduled path ensures that it will always be the current actor. However, when we add the following elements below the Run Immediately path, when Submitting for Approval, and Approving it, it will always be behind one actor when updating the Current Assigned To field. This results in an undesired effect.

 

 

Configure Flow Elements

Let’s add some elements to fetch approval process data, make decisions based on the approver type, and update the custom fields accordingly.

Get Approval Process Instance

  1. Add Get Records Element:
    • Create a Get Records element.
    • Label: Get Approval Process Instance
    • API Name: Get_Approval_Process_Instance
  2. Configure Get Records:
    • Object: ProcessInstance
    • Filter Conditions:
      • Field: TargetObjectId
      • Operator: Equals
      • Value: {!$Record.Id} (References the ID of the triggering Contract record)
    • Sort Order:
      • Field: CreatedDate
      • Order: Descending
    • How Many Records to Store: Only the first record
    • How to Store Record Data: Choose fields and assign variables (advanced)
    • Where to Store Field Values
      • Together in a record variable
    • Assign Fields to Variables:
      • Record: {!ApprovalProcessInstance}
      • Field: ID (cannot edit)
      • Field: Id
      • (Optionally, assign other fields as needed)

Get Current Approval Workitem

  1. Add Get Records Element:
    • Create another Get Records element beneath Get Approval Process Instance element.
    • Label: Get Current Approval Workitem
    • API Name: Get_Current_Approval_Workitem
  2. Configure Get Records:
    • Object: ProcessInstanceWorkitem (Approval Request)
    • Filter Conditions:
      • Field: ProcessInstanceId
      • Operator: Equals
      • Value: {!ApprovalProcessInstance.Id}
    • Sort Order:
      • Field: CreatedDate
      • Order: Descending
    • How Many Records to Store: Only the first record
    • How to Store Record Data: Choose fields and assign variables (advanced)
    • Assign Fields to Variables:
      • Record: {!CurrentApprovalWorkitem}
      • Field: ID (cannot edit)
      • Field: ActorId
      • Field: CreatedDate
      • (Optionally, assign other fields as needed)

Decision: Is Actor a User?

  1. Add Decision Element:
    • Create a Decision element, connecting it after the Get Current Approval Workitem element.
    • Label: Is Actor a User?
    • API Name: Is_Actor_a_User
  2. Configure Decision Outcomes:
  • Outcome 1: Yes – User
    • Label: Yes – User
    • Condition:
      • Resource: {!CurrentApprovalWorkitem.ActorId}
      • Operator: Starts With
      • Value: 005
      • We do this as Salesforce User IDs start with 005.
  • Default Outcome: No – Not a User
    • Label: No – Not a User
  • Update Current Assigned To Field (If Actor is a User)
  • Add Update Records Element:
    • Create an Update Records element, connected to the Yes – User outcome of the Decision element.
    • Label: Update Current Assigned To
    • API Name: Update_Current_Assigned_To
  • Configure Update Records:
    • How to Find Records to Update and Set Their Values: Use the record that triggered the Flow
    • Specify Conditions: None (since you’re updating the triggering record)
    • Set Field Values for the Record:
      • Field: Current_Assigned_To__c
      • Value: {!CurrentApprovalWorkitem.ActorId}

Clear Current Assigned To Field (If Actor is Not a User)

  1. Add Update Records Element:
    • Create a Update Records element, connected to the No – Not a User outcome of the Decision element.
    • Label: Clear Current Assigned To
    • API Name: Clear_Current_Assigned_To
  2. Configure Update Records:
    • How to Find Records to Update and Set Their Values: Use the record that triggered the Flow
    • Specify Conditions: None
    • Set Field Values for the Record:
      • Field: Current_Assigned_To__c
      • Value: Leave blank

(Optional) Handle Pending Approver

If you also want to manage the Pending Approver field, follow these steps. This is optional and can be added after ensuring the Current Assigned To field updates correctly.

Get Next Approval Workitem

  1. Add Get Records Element:
    • Create a Get Records element, connected after the Update Current Assigned To element.
    • Label: Get Next Approval Workitem
    • API Name: Get_Next_Approval_Workitem
  2. Configure Get Records:
    • Object: ProcessInstanceWorkitem (Approval Request)
    • Filter Conditions:
      • Field: ProcessInstanceId
      • Operator: Equals
      • Value: {!ApprovalProcessInstance.Id}
      • Additional Condition: CreatedDate greater than {!CurrentApprovalWorkitem.CreatedDate}
        • Explanation: Assumes the next approval step has a later CreatedDate.
    • Sort Order:
      • Field: CreatedDate
      • Order: Ascending
    • How Many Records to Store: Only the first record
    • How to Store Record Data: Choose fields and assign variables (advanced)
    • Where to Store Field Values: Together in a record variable
    • Assign Fields to Variables:
      • Record: {!NextApprovalWorkitem}
      • Field: ID (cannot edit)
      • Field: ActorId
      • Field: CreatedDate

Decision: Is There a Next Approval Step?

  1. Add Decision Element:
    • Create a Decision element, connected it after the Get Next Approval Workitem element.
    • Label: Is There a Next Approval Step?
    • API Name: Is_There_a_Next_Approval_Step
  2. Configure Decision Outcomes:
  • Outcome 1: Yes – Next Approver Exists
    • Label: Yes – Next Approver Exists
    • Condition:
      • Resource: {!NextApprovalWorkitem.Id}
      • Operator: Is Null
      • Value: False
  • Default Outcome: No – No Next Approver
    • Label: No – No Next Approver

 

Update Pending Approver Field

If Next Approver Exists:
  1. Add Update Records Element:
    • Create an Update Records element, connected to the Yes – Next Approver Exists outcome of the Decision element.
    • Label: Update Pending Approver
    • API Name: Update_Pending_Approver
  2. Configure Update Records:
    • How to Find Records to Update and Set Their Values: Use the record that triggered the Flow
    • Specify Conditions: None
    • Set Field Values for the Record:
      • Field: Pending_Approver__c
      • Value: {!NextApprovalWorkitem.ActorId}
If No Next Approver Exists:
  1. Add Update Records Element:
    • Create an Update Records element, connect this to the No – No Next Approver outcome of the Decision element.
    • Label: Clear Pending Approver
    • API Name: Clear_Pending_Approver
  2. Configure Update Records:
    • How to Find Records to Update and Set Their Values: Use the record that triggered the Flow
    • Specify Conditions: None
    • Set Field Values for the Record:
      • Field: Pending_Approver__c
      • Value: Leave blank

Your Flow should look similar to this if you have completed the optional steps:

 

 

Test the Flow

After setting up the Flow, it’s time to take it for a test drive to ensure its functionality!

  • Activate the Flow
  • Save and Activate:
  • Create a Test Record
  • Navigate to the Target Object

 

  1. Create a New Record:
    • Click New to create a new Contract record.
    • Fill in necessary fields, ensuring that Approval_Status__c is set to a value that does not trigger the Flow (e.g., Draft).
    • Save the record.

Submit the Record for Approval

  1. Initiate Approval Process:
    • Open the newly created Contract record.
    • Click Submit for Approval.
    • Follow the prompts to submit the record.
  2. Verify Field Updates

 

 

 

If you run into any issues or have any questions, feel free to reach out – Happy automating!

Published On: November 27, 2024Categories: Tutorials1760 wordsViews: 1375

Leave A Comment