Triggering a Refresh of the Next Best Action Component

Next Best Action works best when it can react to changes in your Salesforce environment and produce a fresh set of recommendations. While you can always refresh your recommendations with a full page refresh, there are some other techniques that users and developers can use to make the process more subtle, automatic and responsive.

Trigger 1: On It’s Own When the Page It’s on Changes

If a property on the record itself is changed, NBA will update automatically:


Trigger 2: When a Flow On the Same Page Makes Changes

Next Best Action will update even if the flow changes a related object.

In this example, the flow is modifying a Survey record, which is related to the Ship record page that is the current context for the NBA component:

Using related objects in an expression.

The expression being used in the strategy is of the form:

Note that this smart refreshing involves lightning cache, and it may not work in all situations.

Refresh is also triggered if the change is made on a different page or tab.


Trigger 3: From Community Cloud special event integrations.

See page.

Trigger 4: From a Lightning Component on the Same Page

The Next Best Action component subscribes automatically to a lightning application event called lightning:nextBestActionsRefresh. When it receives this event, the component makes a new request to the NBA strategy execution endpoint. If the response it receives is different from what it’s currently showing, it will quietly refresh the visible recommendations.

This works both in Lightning Experience and in Communities pages. To learn more about firing application events from your lightning component, see this.

How the Presence or Absence of RecordId in the Payload Controls Refresh

The nextBestActionsRefresh application event can include in its payload a recordId attribute and corresponding value.

If the NBA component sits in a record page, a refresh will only be triggered if the event provides the same recordId as part of its payload. If the NBA component is on a non-record page such as a community page, the event must not have a recordId value in its payload, or the refresh will not be triggered.

Example: Firing a Refresh Request Application Event

Add this registerEvent tag to your component:

<aura:registerEvent name=”refreshEvent” type=”markup://lightning:nextBestActionsRefresh”/>

When configuring your event for firing, note the importance of the recordId attribute in the event payload (See ‘How the Presence or Absence of RecordId in the Payload Controls Refresh ’, above).

function(component, event, helper) {

      var appEvt = $A.get(“e.lightning:nextBestActionsRefresh”);

      if (!$A.util.isEmpty(component.get(“v.myRecordId”))) {

          appEvt.setParam(“recordId”, component.get(“v.myRecordId”));

      }

      appEvt.fire();

}

Trigger 5: From a Flow That’s Firing the Application Event Described Above.

Sometimes your flow doesn’t change any related records but you still want to trigger a refresh. For example, your flow uses a Flow Action to update a non-Salesforce record and that record is used in the current Next Best Action strategy. You can have your flow fire the nextBestActionsRefresh from a lightning component on a flow screen in the flow and it will trigger a refresh.

See sample code.

Trigger 6: By firing a platform event.

This requires the use of a shim lightning component that sits on the same pages, listens for the platform event, and fires the application event described above.