Often times, we have a decision to make between creating automation using a Flow or by using Apex. Whenever possible, we need to use Flow. But sometime we just can't get around using Apex, and that's okay. But instead of writing nothing but Apex, it can be better to write a Flow that does most of the work and calls Apex only for the portions the Flow cannot do. To do that, we need to use Invocable Variables. Let's look at an example in which we want to input an Opportunity Id and label it as old. The Flow passes in an Opportunity Id and a Boolean to an Apex class.
Step 1
Go to Setup/Object Manager. Go to Opportunity. Create a new checkbox field called 'Old Opp'. Make sure your profile has access to it and save.
Step 2
Go to the Developer Console. Click File/New/Apex Class. Name the Apex Class 'ApexFromFlow'. Click Ok. Delete all of the code in the class and paste in the following:
public without sharing class ApexFromFlow { public class FlowInputs { @InvocableVariable public String OppId; @InvocableVariable public Boolean isOld; }
@InvocableMethod public static void updateOpp (List<FlowInputs> request) {
String OppId = request[0].OppId;
Boolean isOld = request[0].isOld;
Opportunity opp = [SELECT Id, Old_Opp__c from Opportunity WHERE Id = : OppId];
opp.Old_Opp__c = isOld; database.update(opp);
}
}
The code accepts two variables in FlowInputs, a String and a Boolean. We have named them OppId and isOld respectively. The InvocableMethod accepts as its input, all of the Invocable Variables from FlowInputs. In the InvocableMethod, we assign the first entry of the FlowInputs list to new variables for readability. At this point we have successfully passed values into the InvocableMethod from the Flow. This is where we start to do stuff. Per our goal, we find the existing Opportunity using the OppId variable and the update the Old_Opp__c field with the isOld variable. Finally, we commit the change to the database.
Click Save.
Step 3
Go Setup/Flow and create a new Flow. Under Elements tab, click and drag the Action element onto the canvas. On the New Action window, click the Filter By picklist and select Type. Now, select Apex Action. Click inside the Search Apex actions search box. You will see your Apex Class 'ApexFromFlow' pop up. Give it a Label and API Name. I usually just name it the same as the Apex Class. In this case, 'ApexFromFlow'. Below you find the Set Input Values section. Next to OppId and isOld, click the toggle to set it to Include. Now you need click into the 'Enter value of search resources' search box and create the New Resource you will use to pass the value to Apex. Do this for both variables and click Done.
EDIT 1
You can bulkify this code with the following update:
public without sharing class ApexFromFlow { public class FlowInputs { @InvocableVariable public String OppId; @InvocableVariable public Boolean isOld; } @InvocableMethod public static void updateOpp (List<FlowInputs> requests) { // get all Opportunity Ids List<Id> OppIds = new List<Id>(); for (FlowInputs request : requests) { OppIds.add(request.OppId); } // get all Opportunities List<Opportunity> Opps = [SELECT Id, Old_Opp__c FROM Opportunity WHERE Id IN : OppIds]; // loop through all opportunities and requests and update List<Opportunity> OppsToUpdate = new List<Opportunity>(); for (FlowInputs request : requests) { for (Opportunity Opp : Opps) { if (Opp.Id == request.OppId) { String OppId = request.OppId; Boolean isOld = request.isOld; OppsToUpdate.add(Opp); } } } database.update(OppsToUpdate); } }
To get started with Cloud Pacific, go here to complete a simple form. A Cloud Pacific Account Manager will collaborate with you to clarify your needs and goals, and customize a service package and roadmap that will help your business grow and thrive on Salesforce. Reach out to us here!