So today we will check how we can Pass Old Values From Flow To Apex. In Salesforce Spring 21 release we can now get Old values in Record Triggered Flow. I have already cover that in details here. So in this Post We will cover below topics:
- How we can call Apex from Flow?
- Use Generic sObject Attribute in Apex and Flow?
- How to pass Old Values From Flow To Apex?
As In Before Save Flow we cannot call Apex action. So we can only pass data in After save. So using it we can replace After Trigger as of now. It looks like going forward we can completely replace Trigger with Flow. Also we will not need the Trigger Pattern or Factory class to control trigger execution. We can simply turn On/Off Flow in Production.
Call Apex from Flow
Initial steps are same. We need to create a Record Triggered Flow. Remember to Use After Save as this will allow us to call Apex. Before Calling the actual Apex action we will do a pre work.
So drop an Assignment Element and Create two collection variable for sObject Type. I have named them newRecordsBulk and oldRecordsBulk.



Now in our Assignment element we need to add old and new record data in their respective variable. Remember to use Add operator instead of Equals.



Pass Generic sObject From Flow To Apex
So next Select a Action element and Select the Apex class which we have created here. In my case it is Account Trigger Using Flow.



Next we need to give a label. Select the Object which we want to pass from Flow to Apex. In this Demo I have used Account. Now Select the variables. For Bulk input we need to use Bulk Variables which we have created.



Pass Old Values From Flow To Apex
Here we are also passing Old record data from Flow to Apex using a collection variable. You have noticed that while I pass the current record as Single variable. I did not pass the Old record data in same way. It is because we are getting exception while doing that. As this is still in early stage so it might get fixed itself. I will update here.
Below is the Apex Class which we have used here.
global class FlowApexDemo {
@InvocableMethod(label='Account Trigger Using Flow' description='Get the accounts from flow')
public static void getAccountIds(List<AccountWrapper> awList) {
for(AccountWrapper aw : awList) {
System.debug('==========Old Values====='+aw.oldInput);
System.debug('==========New Values====='+aw.newInput);
System.debug('==========Old Values Bulk====='+aw.oldInputBulk);
System.debug('==========New Values Bulk====='+aw.newInputBulk);
}
}
Public class AccountWrapper{
@InvocableVariable(label='Records for Old Input' description='Records for Old Input')
public SObject oldInput;
@InvocableVariable(label='Records for New Input' description='Records for New Input')
public SObject newInput;
@InvocableVariable(label='new Records bulk' description='Records for New Input')
public List<SObject> newInputBulk;
@InvocableVariable(label='old Records bulk' description='Records for Old Input')
public List<SObject> oldInputBulk;
}
}
Final Outcome
So we are ready with our Flow. Remember to Save and Activate it. This is how our Final flow will look like.



Now we save any record and check the debug log. We will find that we have old values and new values in separate list.



Finally with the ability to use Generic SObject in Apex Attribute and Pass Old Record Values from Flow to Apex. We can cover most of the scenario of After Insert/Update Triggers. As flow Can be activated /deactivated in production, in future we might use Flow only.
Check my Instagram page for more updates.
So do you have any use case which you will be able to solve using Flow instead of triggers. Let me know in comments. Happy Programming 🙂
1 thought on “Pass Old Values From Flow To Apex”