Manipulate Complex Internet Data in Flows Without Code Using Apex-Defined Data Types

Flows now have the ability to manipulate the kinds of complex data objects that are typically returned from calls to web services. Create Apex-defined variables in flows and directly process JSON returned from web calls.

Where: This change applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.

Why: Previously, Apex Actions and External Service Registrations were used to manipulate web data, but that data had to be in the form of primitive data types like strings and numbers. Enterprise data from the web retrieved via integration solutions like Mulesoft is rarely composed solely or primarily of primitive data types.

With the new Apex-defined resources in flows, a flow no longer needs to use Apex code to process complex JSON returned from web calls. A developer defines an Apex class to serve as a pattern for automatic conversion from web to a flow, and then full manipulation of the resulting objects can be carried out in a flow using declarative approaches and no additional code. This solution is particularly useful for connecting flows to rich external web objects accessed via Mulesoft and REST calls. If a data type is not supported, flows can pass the value to an Aura component and you can use Apex to operate on it.

For example, a car dealership has a screen flow that lets customers search the dealership’s inventory data, which is stored in another database. The dealership creates an Apex class in their org to define the Car data type. Then, the dealership creates a screen flow that includes two Apex-defined variables. The flow passes the variables between the flow, an Apex action, and an Aura component.

The Car Apex class defines fields, such as contact, registration, and model. The @AuraEnabled annotation is required for each field that you want to expose to Flow.
public class Car {

@AuraEnabled
public Contact contact {get;set;}

@AuraEnabled
public String registration {get;set;}

@AuraEnabled
public String model {get;set;}

}

The entire Car object, including its nested contact, is stored in an Apex defined variable {!Car_Inventory}.

New Resource window that creates the Car_Inventory Apex-defined variable

A flow screen collects an input value for the car model and stores it in the {!Car_Model} text variable. The text variable is assigned to the {!Car_Inventory.model} Apex-defined variable.

Edit Assignment window that sets the {!Car_Inventory.model} variable to the {!Car_Model} value

An invocable method defines the Find Cars Apex action that performs logic to return car results.
global class FindCars {

    @InvocableMethod(label='Find Cars')
   //Perform logic to return car results based on the model. 

        Return results;
    }

The Find Cars Apex action results are stored in the flow as the {!Car_Result} Apex-defined variable.

Edit Apex action window that stores the output value in the {!CarResult} variable

An Aura component defines the DisplayCar screen component with the CarResult attribute. The attribute’s type is the Car Apex class that defines the Apex-defined data type. The screen component displays the attribute’s value.
<aura:component implements="lightning:availableForFlowScreens" access="global">
	<aura:attribute name="CarResult" type="Car" access="global"/>

<!--Add some styling to the component and display the attribute’s value--> 
<div style=”box”>
        <p>{!v.CarResult.model} : {!v.CarResult.buildDate}</p>
</div>
</aura:component>

In the DisplayCar screen component, the Car Result attribute contains the {!CarResult} Apex-defined variable and displays it’s value during runtime.

DisplayCar screen component properties that sets the Car Result attribute to the {!CarResult} variable

When using Apex-defined data types, keep these considerations in mind:
  • Supported data types in an Apex class are Boolean, Integer, Long, Decimal, Double, Date, DateTime, and String. Single values as well as lists are supported for each data type. Multiple Apex classes can be combined to represent complex web objects.
  • The first time you open an element or resource window in an org with over 200 Apex classes that have the @AuraEnabled annotation, the window may take a while to load.
  • As of Summer ‘19, if a flow invokes Apex, the running user must have the corresponding Apex class assignment in their profile or permission set.