Skip to main content
SFDC Developers
Flow

Fixing Null Variables in Salesforce Approval Flow Emails

Vinay Vernekar · · 4 min read

The Problem: Why Reassignment Breaks Merge Fields

In the Salesforce ecosystem, Approval Processes are powerful, but they operate with specific constraints when it comes to context. A common frustration for architects and developers is the "null variable" issue: when an approver is changed—either manually or via an automated process—the email template triggered by the approval process fails to populate merge fields correctly.

This happens because the context of the email alert is tied to the ProcessInstanceStep object. When a record is reassigned, the relationship between the original requestor, the record, and the new target approver can shift, causing the standard merge fields (like {!ApprovalRequest.Comments}) to resolve to null or display outdated info. In this guide, we’ll explore how to bypass these limitations using modern automation patterns.

Strategy 1: Transitioning from Email Alerts to Flow-Driven Notifications

The most robust way to handle reassignment is to move away from native Approval Process Email Alerts and toward Flow-driven notifications. Native alerts have limited logic; Flow-based notifications allow us to re-query the record state at the exact moment of execution.

The "After-Update" Approach

Instead of relying on the standard Approval Email template, create a Record-Triggered Flow on the ProcessInstanceWorkitem object. This object represents a pending approval request.

  1. Trigger: Set the flow to trigger when a record is updated.
  2. Conditions: Filter for ProcessInstanceWorkitem where ActorId has changed.
  3. Action: Use the Send Custom Notification or Send Email action within the Flow.

By building this logic in Flow, you gain access to the ActorId (the new approver) immediately, allowing you to fetch their User profile data to build a custom, reliable email body that won't result in null pointers.

Strategy 2: Using Invocable Apex to Capture Context

If you have complex logic that Flow cannot handle—such as parsing deep relationship fields that are returning null—Invocable Apex is your best friend. Apex allows you to perform SOQL queries that ensure you have the correct data before the email is even constructed.

Here is a simple pattern for an Invocable method to fetch valid approver context:

public class ApprovalContextHandler {
    @InvocableMethod(label='Get Valid Approver Info')
    public static List<String> getApproverDetails(List<Id> workItemIds) {
        List<ProcessInstanceWorkitem> items = [SELECT Actor.Name, ProcessInstance.TargetObjectId 
                                               FROM ProcessInstanceWorkitem 
                                               WHERE Id IN :workItemIds];
        // Process logic here to ensure variables aren't null
        return new List<String>{ items[0].Actor.Name };
    }
}

By passing the WorkItemId into an Invocable method, you can perform a clean query. Even if the approval process "forgets" the context during the redirect, your code remembers it.

Strategy 3: The Formula Field Fallback

Sometimes, the simplest solution is the most effective. If your email template is pulling data from the record being approved, avoid using the {!ApprovalRequest.FieldName} syntax, which is volatile during reassignments.

Instead, create "Proxy" Formula Fields on the object being approved.

  • Example: Create a formula field named Current_Approver_Name__c.
  • Logic: Use the Approver__r.Name field if it exists, otherwise pull from a custom metadata setting or a related lookup.
  • Benefit: When the email template references {!MyObject__c.Current_Approver_Name__c}, it relies on the record's current state rather than the volatile state of the Approval Instance.

Troubleshooting Checklist for Nulls

When you see null values in your notification, follow this hierarchy of verification:

  • Check the Trigger Order: If your email alert is firing before the ActorId is updated in the database, the variable will naturally be null. Use a Pause element in your Flow to allow the database to commit the new assignment.
  • Validate Field Level Security (FLS): Even if the variable isn't technically null, if the user triggering the email (or the Automated Process User) lacks FLS to the specific field, Salesforce will return null to prevent data leakage.
  • Audit the ProcessInstance: Use Query Editor to inspect the ProcessInstanceWorkitem history. If the OriginalActorId and ActorId are identical, the reassignment logic itself might be failing.

Key Takeaways

  • Move away from legacy email templates: Use Flow-based notifications to gain better control over the execution context.
  • Context is everything: Recognize that Approval Process email alerts are limited by the ProcessInstance lifecycle; if the context changes, the template may fail.
  • Use Formula Fields as proxies: Decouple your email template from the Approval Process variables by using record-level formula fields that resolve data consistently.
  • Leverage Invocable Apex: When Flow isn't enough, write a small Apex class to fetch the exact record relationships you need, ensuring zero-null results in your communications.

By following these patterns, you’ll ensure that your team receives accurate, readable approval notifications regardless of how often, or to whom, the request is reassigned.

Share this article

Vinay Vernekar

Vinay Vernekar

Salesforce Developer & Founder

Vinay is a seasoned Salesforce developer with over a decade of experience building enterprise solutions on the Salesforce platform. He founded SFDCDevelopers.com to share practical tutorials, best practices, and career guidance with the global Salesforce community.

Get weekly Salesforce dev tutorials in your inbox

Comments

Loading comments...

Leave a Comment

Trending Now