

# Start your system's automation from the guide

To let agents start a process in your system, such as a return, a refund or an escalation, build a flow module that calls the operation of your system's target that starts it. The agent then runs the module from the guide, and the module returns your system's answer. With Salesforce, choose the operation from the table in [Update records and run automation in your system](real-time-data-integration-automation.md); each runs as the integration user. The account and case guide that the stack creates has no button for your module: you call it from a copy of the guide flow, as [Customize the guide](real-time-data-integration-adapt.md) describes.

The example in these steps is an Initiate return action, which uses `runFlow`: an autolaunched flow, `Initiate_Return`, that takes the case, a reason and a refund amount, records the return request, and returns a code that says what happened. The module acts on the case in the calling contact's `caseId` attribute; for which case that is, see [The case view](real-time-data-integration-agent-experience.md#real-time-data-integration-agent-case-view). Call the module from your copy of the guide flow, where it acts on the last case the agent opened, or from agent assist, where it acts on the case open in the guide.

**To prepare the flow in Salesforce**

1. Create the autolaunched flow `Initiate_Return`, with these Text variables available for input: `caseId`, `reason`, `refundAmount` and `comments`. Add a Text variable available for output, `outcome`, and set it to a code such as `created`, `invalid` or `noCase`. Check the inputs in the flow itself, and check whether the return already exists, because Connect Customer can run a failed call again.

1. Activate the flow.

1. Make sure the integration user has the **Run Flows** permission and access to the objects the flow uses. The setup script in [Create the integration user and its access](real-time-data-integration-credentials.md#real-time-data-integration-credentials-salesforce) grants **Run Flows** when `runFlows` is `true`, the default.

**To build the Initiate return module in Connect Customer**

1. Create a flow module named, for example, **Initiate return**, with the inputs `reason`, `refundAmount` and `comments`, the output field `outcome`, and the branches Ran, Refused and Failed. Write its description for the agent, such as **Starts a return on the open case. reason is Damaged, WrongItem, NotAsDescribed or NoLongerNeeded**, and use the same text as the tool's description when you add the module to an AI agent.

1. Add a **Check contact attributes** block on `$.Attributes.caseId`. The block takes the first condition that matches, so list the conditions in the order the stack's modules use: first one **Contains** condition for each refused character, `' \ % & # ? / +`, each going to a **Return** block with the branch Refused; then, last, a condition that the value starts with `500`, which continues to the next block. Send **No match** to the Refused **Return** block too. Check that `reason` is one of the values the flow accepts (the sample takes `Damaged`, `WrongItem`, `NotAsDescribed` or `NoLongerNeeded`), and that `refundAmount` and `comments` hold no `'` or `\`. Send any value that fails to a **Return** block with the branch Refused.

1. Add an **External Tool** block. For the application, choose the stack's integration, `Salesforce - {{stack-name}}`, and for the tool `gateway_{{gateway-id}}__salesforce___runFlow`. Set its three inputs: `domainName` to your org's My Domain name, the part of its host before the first dot; `flowApiName` to **Initiate\_Return**; and `body` to the flow's input variables:

   ```
   {"inputs": [{"caseId": "$.Attributes.caseId",
                "reason": "$.Modules.Input.reason",
                "refundAmount": "$.Modules.Input.refundAmount",
                "comments": "$.Modules.Input.comments"}]}
   ```

1. On the block's success branch, add a **Return** block with the branch Ran, and set `outcome` to the flow's output: ``$.ExternalTool.ResultData[0].outputValues.outcome``, with the backticks. On its error branch, add a **Return** block with the branch Failed.

1. Publish the module, and create a version and an alias.

1. Run it from your copy of the guide flow with an **Invoke flow module** block, as in [Extend the guide with flow modules](real-time-data-integration-modules.md).

In your copy of the guide flow, show a view after the module, with a line for each `outcome` code, such as "Return created."

Errors come back in two ways. If the gateway or Salesforce refuses the call, for example because the flow isn't active or autolaunched, an input name is wrong or the integration user lacks **Run Flows**, the **External Tool** block takes its error branch and the module returns Failed; see [Flows](real-time-data-integration-troubleshoot.md#real-time-data-integration-troubleshoot-flows). If the flow runs but can't do the work, it says so in `outcome`, so compare that value before you tell the agent the return was created.

The other operations in the table work the same way, with their own inputs. The gateway's `runFlow` tool runs any active autolaunched flow the integration user can run, so the integration user's access, which your Salesforce administrator sets, decides which flows the gateway can start.