Sample Flow to Apply Service Credit via a Record-Triggered Flow

This page describes a sample flow to apply a service credit to an active billing contract by editing two custom fields on the contract header. The two custom fields are a number to hold the credit amount, and a checkbox to indicate that the credit amount should be applied. The flow is triggered when this checkbox is selected on a billing contract's record page, or in the list view. By editing the custom fields in the list view and saving your changes, you can apply service credits to multiple billing contracts at once.

This sample flow does not include error handling. For an illustration of how to build error handling into a flow, see either of the sample flows for renewing contracts at Sample Flows to Renew Contracts.

Warning:

This page does not give step-by-step instructions on how to create the sample flow. The information is intended for administrators who are already experienced at creating Salesforce flows. Refer to the Salesforce Help if you need information about how to create flows.

Prerequisites

This sample flow has the following prerequisites:

  1. On the Billing Contract object, create the following custom fields.

    Label API Name Data Type
    Apply Service Credit Apply_Service_Credit__c Checkbox
    Credit Value Credit_Value__c Number
  2. In the Flow Builder, go to the Toolbox and create a variable of Data Type = Text to store the ID of the change request that is created to add the service credit line to the active contract. Name this variable ChangeRequestId.

    Screenshot of New Resource window in the Flow UI

  3. In the Flow Builder, go to the Toolbox and create a variable of Data Type = Date to store the date that Billing Central calculates to be the contract's next billing date. Name this variable NextBillingDate.

    Screenshot of New Resource window in the Flow UI

The Sample Flow

This sample flow is a Record-Triggered Flow that requires the user to enter a service credit value in the Credit_Value__c custom field on an active billing contract, and to then select the Apply_Service_Credit__c checkbox. (See "Prerequisites" step 1 for details of creating these custom fields.) Billing Central then creates a change request to add a service credit line to the contract, calculates the next billing date for the service credit line, and applies the change request to the contract.

Diagram of Sample Flow to Apply a Service Credit via a Record-Triggered Flow

An explanation of each numbered item is provided in the table below.

# Flow Element Sample Flow Suggestion Sample Flow Setup
1 Flow Element: Start

Create a new Record-Triggered Flow.

In the window that appears, set:

Object: Billing Contract

Trigger the Flow When: A record is updated

Condition requirements: All conditions are met (AND)

Then add the following conditions:

ffbc__Type__c Equals Contract
ffbc__Status__c Equals Active
Apply_Service_Credit__c Equals {!$GlobalConstant.True}

When to Run the Flow for Updated Records: Every time a record is updated and meets the condition requirements

Note:

These conditions mean that the flow will be triggered when an active contract is saved with the Apply_Service_Credit__c checkbox selected.

Screenshot of Select Object window in the Flow UI
2 Flow Element: Apex Action

Add an Action element. Choose the Create Change Requests action, then complete the input and output values.

Set Input Values

Contract ID to create change request for: {!$Record.Id}

Store Output Values - assign this to the variable you created in "Prerequisites" step 2.

ID of newly created change request: {!ChangeRequestId}

Screenshot of New Action window in the Flow UI
3 Flow Element: Get Records Add a Get Records element.

In the Get Records window, set:

Object: Product

Condition requirements: All conditions are met (AND)

Then add the following condition:

ProductCode Contains Credit

How Many Records to Store: Only the first record

How to Store Record Data: Choose fields and let Salesforce do the rest

Select Product Fields to Store in Variable: ID

Note:

This means that the first product found with Credit in its product code will be set as the product on the service credit line.

Screenshot of Get Records window in the Flow UI
4 Flow Element: Apex Action

Add an Action element. Choose the Calculate Next Billing Dates action, then complete the input and output values.

Set Input Values

Contract ID to calculate next billing date for: {!$Record.Id}

Store Output Values - assign this to the variable you created in "Prerequisites" step 3.

Next Billing Date calculated by action: {!NextBillingDate}

Screenshot of New Action window in the Flow UI
5 Flow Element: Decision

Add a Decision element to validate whether the Next Billing Date returned by the Apex action is null.

Create a Next Billing Date is Null outcome and set the condition to:

{!NextBillingDate} Is Null {!$GlobalConstant.True}

Screenshot of Decision window in the Flow UI
6 Flow Element: Assignment

For the Next Billing Date is Null outcome, add an Assignment element and set the following assignment:
{!NextBillingDate} Equals {!$Flow.CurrentDate}

Note:

This means that if the Next Billing Date returned by the Apex action is null, it is automatically set to today's date.

Screenshot of Assignment window in the Flow UI
7 Flow Element: Create Records

Add a Create Records element and complete the window as follows:

How Many Records to Create: One

How to Set the Record Fields: Use separate resources, and literal values

Object: Billing Contract Line Item

Set Field Values for the Billing Contract Line Item

Field Value
ffbc__BillingType__c One-off
ffbc__Contract__c {!ChangeRequestId}
ffbc__Description__c Outage
ffbc__EndDate__c {!$Record.EndDate__c}
ffbc__FirstBillDate__c {!NextBillingDate}
ffbc__ProductService__c {!Get_Service_Credit_Product.Id}
ffbc__Quantity__c 1
ffbc__StartDate__c {!$Record.StartDate__c}
ffbc__UnitPrice__c {!$Record.Credit_Value__c}
Note:

The values in this table will be used to create the service credit contract line item. You can replace Outage with whatever you want to use as the service credit line's description.

Screenshot of Create Records window in the Flow UI
8 Flow Element: Apex Action

Add an Action element. Choose the Apply Change Requests action, then complete the input and output values.

Set Input Values

Change Request ID to be applied: {!ChangeRequestId}
Disable creation of credit notes when applying the change request: {!$GlobalConstant.False}
Generate Billing Schedules using a batch job that runs asynchronously: {!$GlobalConstant.False}

Screenshot of New Action window in the Flow UI
9 Flow Element: Update Records

Add an Update Records element and complete the window as follows:

How to Find Records to Update and Set Their Values: Use the billing contract record that triggered the flow

Condition Requirements to Update Record: None - Always Update Record

Set Field Values for the Billing Contract Record

Field Value
Apply_Service_Credit__c {!$GlobalConstant.False}
Credit_Value__c  
Note:

This will reset the custom fields on the contract header so that they can be updated again if another service credit needs to be applied to the same contract in the future.

It is not mandatory to reset the Credit_Value__c field. It can be left unchanged. Any value in the field is applied only if the Apply_Service_Credit__c checkbox is reselected.

Screenshot of Update Records window in the Flow UI
10 Flow Element: End End of flow.  
  Activate the flow Remember to save and activate the flow to make it available for use.