Given a scenario, implement Apex to work with various types of page components, including Lightning Components, Flow, Next Best Actions, etc.

After studying this topic, you should be able to:

  • Describe the interaction between Apex and page components on a user interface
  • Determine how Lightning components, flows, and other components can be used to meet user interface requirements

Table of Contents

  • Introduction
  • Invoke Apex in Flows
  • Invoke Flows in Apex
  • Other Apex and Component Interactions
  • Einstein Next Best Action
  • Einstein Recommendations Builder
  • Recommendation Strategy
  • Invoke Apex in Lightning Web Components
  • Lightning Web Component as a Screen Component
  • Custom Property Editor for Custom Screen Components
  • Custom Property Editor for Invocable Actions
  • Requirements & Solutions

Introduction

  • This page describe the interactions between Apex and page components displayed on a user interface such as Lightning Components, Flows and Next Best Actions
    • Interactions between declarative and programmatic processes such as Apex and flows are discussed to provide a better understanding of how server-side logic can be invoked in response to user interaction on components that are displayed on a user interface
    • Implementation of Einstein Next Best Actions on a Lightning page provides an opportunity to illustrate a high-level scenario where Apex, Flows and Lightning components are used to deliver a functionality and user interfaces requirement

Invoke Apex in Flows

  • Invoke Apex Using Flows: Apex code can be executed from a flow by either calling invocable methods or Apex methods from classes that implement the Process.Plugin interface
    • Invocable method
      • Classes with @InvocableMethod annotations are available in flows, processes, and Rest API
      • Recommended by Salesforce
      • Supports bulk operations
      • Invocable methods support list of, or list of lists of, primitive data types, sObject types, and generic sObjects as input/output values
      • Support for sObject data types in invocable methods enable one Apex action to be used for multiple types of objects
    • Process.Plugin Interface
      • Classes implementing the Process.Plugin interface are available in flows only
      • Not recommended by Salesforce
      • Does not support bulk operations
      • Blob, Collection, sObject, and Time data types are not supported by Process.Plugin
      • The interface that exposes the Apex class as a service that accepts input values and returns output back to the flow
  • Flow Builder Using the Apex Action: screen flow below uses Apex Action element to call an invocable method

  • Invocable Method Interaction with Flow: Apex class below contains the invocable method that receives input and returns output

  • Callouts in Screen Flows: When a screen flow performs a DML operation and a callout, the callout=true attribute can be added to the invocable method annotation to enable the flow to handle the callout accordingly at runtime and avoid system callout errors
    • A transaction control setting is available for an Apex action that performs a callout

  • Flow Builder Using the Legacy Apex Action: Apex classes that implement the Process.Plugin interface will appear in the Apex Action (Legacy) tab

Invoke Flows in Apex

  • Invoke Flows using Apex: Flows can be invoked from Apex using the Flow.Interview system class
    • Flow Name: Flows can be started from an Apex method by specifying the flow name either statically or dynamically
    • Start Method: the start method of the Flow.Interview class can be used to run autolaunched or user provisioning flows
    • Flow Variable: the getVariableValue method can be used to return the value of the specified flow variable
    • Return Type: the start method does not have a return type, whereas getVariableValue has the return type of object
    • Active Flow: a flow needs to be activated before it can be invoked from Apex through user interaction
  • Invoking a Flow Statically: Apex code below invokes a flow which adds two numeric input values and then returns its sum

  • Invoking a Flow Dynamically: Flows can be called from and Apex method dynamically by using the createInterview method

  • Triggers for Orchestration: the orchestration type determines how an orchestration is launched or distributed

Other Apex and Component Interactions

  • Other components can be added on a Lightning page that can be used to interact with Apex:
    • Visualforce: Visualforce can be embedded on a Lightning page through the Record Detail or Visualforce Page standard Lightning components. Visualforce pages invoke Apex methods using custom controllers and extensions.
    • Flow: The Flow standard Lightning component can be used to load a screen flow on a Lightning page. The flow can then use invocable Apex to send or receive data or perform other server-side operations
    • Custom components: A custom Lightning component can be built using the Aura or LWC programming model. These components have client and server-side controllers for executing Javascript and Apex methods

Einstein Next Best Action

  • Next Best Action is used to create and display actions for users that are tailored to meet unique criteria. It relies on flows, recommendations, strategies, and components, and has standard objects for reporting.
  • Next Best Action requires the use of multiple tools and Lightning components that can be configured to invoke flows which may execute Apex actions. To use Einstein Next Best Action, the following steps are performed:
    • Build a Flow: Build a flow in Flow Builder that is invoked when a recommendation is accepted or rejected (optional)
    • Create Recommendations: Create recommendations that can be displayed by the Einstein Next Best Action component when a record meets criteria
    • Choose the Flow: When creating a recommendation, choose the flow that will be launched when a customers accepts or rejects the recommendation
    • Display Recommendations: Display the recommendations on a Lightning page or App home page using the Einstein Next Best Action component,or on a Visualforce page,or custom app using the lightning:nextBestAction component

  • Build a Flow for Accept and Reject Actions: A flow can be created that is invoked when a recommendation is accepted or rejected. The flow can be configured to run an automated process, perform a callout, send email and more

  • Creating a Recommendation and Choosing a Flow: When creating a recommendation, the selected flow runs on Acceptance only by default. To also run the flow on Rejection, an additional setting is required in Lightning App Builder.

  • Strategy Builder: Strategy Builder is a tool for defining a strategy used by Next Best Action for loading recommendations

  • Einstein Next Best Action component:
    • The Einstein Next Best Action component can be added to a Lightning page to show recommendations to support agents based on the selected Strategy Source and Action Strategy
    • The Next Best Action component when added to a Lightning page displays recommendations for records that meet criteria and invokes flows accordingly in response to a performed action

Einstein Recommendations Builder

  • Einstein Recommendation Builder is a tool that uses artificial intelligence in order to deliver intelligent and personalized recommendations

  • Einstein Recommendation Builder requires three types of objects for building a recommendation

  • The Settings page is used to configure which items to target which recipients and allows providing example records of positive as well as negative interactions for more intelligent recommendations

  • Segment pages are available for both the Recommended Items and Recipient objects to allow recommending specific items only and/or recommending items to specific recipients only

  • By default, all interactions are treated as positive interactions. To provide better recommendations, filters can be used to narrow down specific examples

  • The Scorecard page shows overall recommendation quality and other information and indicates how successful the recommendation is going to be based on the current configuration

Recommendation Strategy

  • The Recommendation Strategy flow type in Flow Builder can be used to build recommendations for Einstein Next Best Actions

  • This example shows how recommendations can be defined based on the field value of a record in a Recommendation Strategy

  • To choose a Recommendation Strategy as the source for recommendations in an Einstein Next Best Action component in Lightning App Builder, the Strategy Source property is set to Flow Builder

Invoke Apex in Lightning Web Components

  • Interactions between Apex and Lightning Web Components
    • Invoking Apex: Imported Apex methods can be invoked from a Lightning web component either via @wire or imperatively
    • Apex Method: To invoke an Apex method, it must be annotated with @AuraEnabled and be static and either global or public. Note that overloading is not allowed for @AuraEnabled methods
    • Importing Apex: The default import syntax can be used to import an Apex method via the @salesforce/apex scoped packages
    • Cacheable Method: To use the wire service, the Apex method must be annotated with @AuraEnabled(cacheable=true). Adding scope=’global’ to the annotation will cache the Apex method in a global cache such as the Salesforce CDN
    • Using @wire: A property or function can be wired using the following syntax: @wire(apexMethod, { apexMethodParams }) propertyOrFunction;
    • Dynamic Parameter: The value of a parameter can be prefaced with $ to indicate that it’s dynamic and reactive
    • Invocation Control: If the invocation of a method needs to be controlled, it should be called imperatively. A single response is received when a method is called imperatively
    • Non-Cacheable Method: When a method is not annotated with (cacheable=true), it must be called imperatively
    • Refresh: When using the imperative approach, data can be refreshed by invoking the method again. When using @wire, the refreshApex() method is used
  • Refresh Wired Data with refreshApex(): This example shows how to use the refreshApex() method to refresh wired data

  • Invoking Apex Methods in Lightning Web Component: this example two ways of invoking Apex in a Lightning web component

  • Access to @AuraEnabled Methods: Apex methods annotated with @AuraEnabled are only accessible to an authenticated user if the user profile or an assigned permission set is explicitly enabled to have access to the containing Apex class. This requirement also applies to guest, portal, and community users
    • Enabling Access via Profile: In the Apex Classes section in Setup, go to the Apex class and click on Security. Then, select the profiles that need to have access to the Apex class. Alternatively, in Profiles in Setup, go to a profile and edit the Enabled Apex Class Access related list and select the necessary Apex Classes that will be made available to its users
    • Enabling Access via Permission Set: In the Permission Sets section in Setup, choose or create a new permission set. Go to Apex Class Access and click on Edit. Then, select the Apex classes that will be made available to users that are assigned the permission set.
  • Sharing Behavior in @AuraEnabled Apex Controllers: Apex classes that contain @AuraEnabled methods, which are exposed to Lightning components, default to with sharing when no sharing behavior is explicitly defined in the class
    • Secure by Default: Defaulting to with sharing ensures Aura components and Lightning web components to be secure by default
    • Enforced Behavior: Previously, a setting called “Use without sharing for @AuraEnabled Apex Controllers with Implicit Sharing” was used to reverse the default behavior, but it has now been retired
    • Defined Explicitly: It is recommended to explicitly define the sharing behavior of an Apex class by using the with sharing or without sharing keywords
  • Access Modifiers of Apex Properties: Aura and Lightning web components respect the access modifiers defined on Apex properties of their Apex controllers

Lightning Web Component as a Screen Component

  • Creating a Custom Screen Component: A custom screen component can be created for a flow screen in Flow Builder using a Lightning web component. The lightning__FlowScreen target is added to make the component usable in the flow screen

  • Custom Screen Component Properties: To expose public properties as well as public methods of a custom Lightning web component, the @api decorator is used

  • Loading a Custom Screen Component: Custom screen components will appear under the Custom section in the Screen Components pane of a flow screen element in Flow Builder

Custom Property Editor for Custom Screen Components

  • Creating a Custom Property Editor: A custom property editor, which is also built using a Lightning web component, can be created for a custom flow screen component. A standard and custom property editor is compared below.

  • Registering a Custom Property Editor: To register a custom property editor, its name is specified using the configurationEditor attribute in the configuration file of the custom screen component

s

  • Custom Property Editor Example: This custom property editor, called voucherInputConfig, replaces the standard property editor of the example voucherInput custom screen component. The following shows its component markup

  • Custom Property Editor JavaScript: The JavaScript class of the custom property editor uses the inputVariables interface to access field data of the custom screen component. Custom validation can also be run using the validate interface

  • Custom Property Editor Validation: In this example, the custom property editor returns an error when the voucher is set as a premium voucher and the maximum usage limit is set to more than what is allowed

Custom Property Editor for Invocable Actions

  • Custom Property Editors for Invocable Actions: Custom property editors can also be created for invocable actions to allow configuring Apex actions in Flow Builder more easily. This example shows an invocable action that performs a callout

  • Custom Property Editor JavaScript: The following shows an example JavaScript class for a custom property editor that is used to configure an Apex action in Flow Builder

  • Registering a Custom Property Editor: Unlike custom flow screen components, registering a custom property editor for an Apex action is performed in the @invocableMethod annotation of the Apex method

Requirements & Solutions

Reference FoF Slides