1

I have a flow which calls an approval process on a custom object. The approval process changes a picklist field to either confirmed or not fulfilled based on the response. I have an Apex trigger on the same custom object which sends an email, should theoretically run after the field is updated by the approval process. But the trigger does not work for some reason. Manually updating the picklist value however fires the trigger and the email is sent.

trigger OrderStatusUpdateTrigger on A7Order__c (after update) {
    OrderStatusUpdateHandler.handleTrigger(Trigger.oldMap, Trigger.newMap);
}

Handler:

public class OrderStatusUpdateHandler {
    public static void handleTrigger(Map<Id, A7Order__c> oldOrders, Map<Id, A7Order__c> newOrders) {
       
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        for (A7Order__c updatedOrder : newOrders.values()) {
            A7Order__c oldOrder = oldOrders.get(updatedOrder.Id);
            if (oldOrder.Order_Status__c != updatedOrder.Order_Status__c) {
              
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
               
                String recipientUserId = '0055g00000JyO8N'; 
                email.setTargetObjectId(recipientUserId);

                
                if (updatedOrder.Order_Status__c == 'Confirmed') {
                    email.setTemplateId('00X5g000004P9ib');  
                } else if (updatedOrder.Order_Status__c == 'Not Fulfilled') {
                    email.setTemplateId('00X5g000004P9ig');  
                }
                email.setWhatId(updatedOrder.Id);
                email.setSaveAsActivity(false);
                emails.add(email);
            }
        }
        if (!emails.isEmpty()) {
            Messaging.sendEmail(emails);
        }
    }
}

The field update on approval process has the re-evaluate workflow rules enabled. What am I doing wrong here? Is there a workaround?

3
  • Is the value of the field actually changing? Salesforce's automations won't perform a DML if no change actually occurred, which may be what you're experiencing.
    – sfdcfox
    Commented Jan 29 at 12:50
  • @sfdcfox yes, the value of the picklist is changing from the initial default value to Confirmed in the record but the trigger wont fire. Commented Jan 29 at 12:59
  • @sfdcfox The only workaround I can think of is to create another field which updates on the approval process, if this field is checked then update the records using a flow and then send the emails in that flow or call the trigger there. Not sure if that will work, seems like a lot of work to send an email. I could send an email directly from the approval process but I need to attach a pdf to the email after approval. Commented Jan 29 at 13:06

0

You must log in to answer this question.