

# Extend the guide with flow modules

A flow module is a reusable part of a flow, with inputs, output fields and custom branches; see [Flow modules for reusable functions in Connect Customer](contact-flow-modules.md). The stack creates the ten modules in the table that follows: nine in the stack itself, and Create a case with details in the nested stack `{{stack-name}}-GuideModules-{{random}}`. Your own flows can call any of them, and AI agents can use those with external invocation on.

You can also build modules of your own, such as one that starts your system's automation, and plug one in as the guide's Find the record step. The stack owns its ten modules, and a stack update rewrites them. A module you create is yours, and stack updates never change it, so build your changes as modules of your own rather than editing the stack's.

With the stack's modules and modules of your own, you can do the following:
+ Read a record before routing: call Read the account or Read a case from your own IVR or bot flow, for example to play a case's status to the caller.
+ Create a case with no agent: call Create a case from an after-hours or self-service flow, on the account your flow verified.
+ Give your own AI agent the modules as tools, as the stack's agent assist AI agent uses seven of them.
+ Choose the record the guide opens your own way: replace Find the record with your own module, which can run an invocable Apex action (`runApexAction`) or an autolaunched Salesforce flow (`runFlow`) through the gateway, or call an AWS Lambda function.
+ Start your system's automation from the guide or a flow, with a module that calls one gateway tool, such as the Initiate return example in [Start your system's automation from the guide](real-time-data-integration-run-automation.md).

The following diagram shows who calls a module and what the module calls.

![Three groups. Callers in Amazon Connect Customer: the guide flow and your own flows, each with an Invoke flow module block, and agent assist, which uses the module as a tool and needs external invocation. All three call a flow module, which runs its inputs and checks, with record IDs from the contact's attributes, then one or more External Tool blocks, each calling one gateway tool, such as runFlow or queryAccounts, then ends in a branch and result, such as Ran or Failed, with output fields. The External Tool block calls the system's target on the agentic gateway, which calls your system's API as the integration user; for Salesforce, records, flows, Apex and quick actions.](https://docs.aws.amazon.com/connect/latest/adminguide/images/real-time-data-integration-flow-modules.png)


Every caller goes through the same module, and only the module calls your system, so a check you put in the module applies to the guide, your flows and agent assist alike.

The stack creates these ten modules, each named `{{stack-name}}-account-360-` followed by the suffix in the table. Each one's `v1` alias is the stack output in the first column. The console calls a module's results its branches:


**The guide's flow modules**  

| Module | Inputs | Output fields | Branches | 
| --- | --- | --- | --- | 
| Find the customer, `find-customer` (`FindCustomerModuleRef`) | `by`: `AccountNumber`, `ContactEmail`, `CaseNumber` or `FreeText`; `value` | One, Several and None: up to five accounts (`Id`, `Name`, `AccountNumber`) in `records`. Results: up to five each of accounts, contacts and cases in `searchRecords`. Refused: `reason` `empty`, `invalid`, `apostrophe` or `operator`. | One, Several, None, Results, Refused, Failed | 
| Read the account, `read-account` (`ReadAccountModuleRef`) | None; reads the contact's `accountId` attribute and its `foundCase` attribute, a case number or `-` | In `compositeResponse`: the account with up to six of its cases other than the one in `foundCase`, open cases first and then the newest, and its five newest contacts; the case in `foundCase`; the org's first 10 case statuses; and the account's open and closed case counts. On Refused, `reason` is `invalid`. | Answered, Refused, Failed | 
| Read a case, `read-case` (`ReadCaseModuleRef`) | None; reads the contact's `accountId` and `caseId` attributes | The case in `records`, with its account's details and its four newest comments, four newest emails and six newest files (the case view shows 3, 3 and 5); Trimmed has no Description and no lists; and `reason` | Found, Trimmed, NotFound, Refused, Failed | 
| List the account's cases, `list-cases` (`ListCasesModuleRef`) | `offset`, the number of cases to skip, 0 to 2000; reads the contact's `accountId` attribute | A page of ten cases, open first, then the newest, and the account's open and closed case counts, in `compositeResponse`, and `reason` | Results, Refused, Failed | 
| Update a record, `update-record` (`UpdateRecordModuleRef`) | `objectType` (`Account` or `Contact`), `recordId` (a contact's Id; ignored for Account), `expectedLastModified` (the record's `LastModifiedDate` as read), `Name`, `Phone`, `BillingCity`, `FirstName`, `LastName` and `Email`, each written as given, so an empty value clears it; the account from `accountId`, never from an input | For Updated, and for Refused when Salesforce refuses the write, the Salesforce Composite response: the write, then the account view's data, in `compositeResponse`. For Refused by the module's checks, `reason`: `object`, `invalid`, `scope` (the contact isn't on the account) or `emailInvalid`. An empty `Name` for an Account, or an empty `LastName` for a Contact, is Refused before any Salesforce call, with `reason` `invalid` and `field` naming it. Conflict and Failed return no fields. | Updated, Conflict, Refused, Failed | 
| Create a case, `create-case` (`CreateCaseModuleRef`) | `subject`, `description`, `origin`; the account from `accountId`. A value in `origin` sets the case's Origin; `-` uses the contact's `caseOrigin` attribute, or, when that is empty, `Phone` on a voice contact and `Web` on any other channel; to choose the Origin from your own flow, pass a value. The case's contact is the contact's `matchedContactId` attribute when it holds a contact ID (`003`...), and none otherwise | The new case and the account read again in `compositeResponse`, and `reason`. An empty `subject` is Refused, with `reason` `invalid` and `field` `subject`, before any Salesforce call. | Answered, Refused, Failed | 
| Create a customer, `create-customer` (`CreateCustomerModuleRef`) | `name`, `firstName`, `lastName`, `email`, `phone` | Created: the new account and contact in `compositeResponse`. Duplicate: the result of the check that found the existing customer. Refused: Salesforce's response, or `reason` `invalid`, `contact`, `email` or `phone`; for a `name`, `firstName` or `lastName` that is empty or `-`, or holds `{`, `reason` `invalid` with `field` naming it. MayHaveBeenSaved and Failed return no fields. | Created, Duplicate, Refused, MayHaveBeenSaved, Failed | 
| Find the record, `find-record` (`FindRecordModuleRef`) | The seven inputs of the Find the record contract, which follows | `count`, `accountId1` to `accountId5`, `caseId`, `contactId`, `reason` | One, Several, None, Error | 
| Read the verified account, `read-verified-account` (`ReadVerifiedAccountModuleRef`) | None; reads the contact's `accountId` attribute | In `records`: the account's Name, Phone, AccountNumber, BillingCity, Type, created date and owner, and up to six of its cases, open cases first and then the newest, each with CaseNumber, Subject, Status, IsClosed, Priority, owner, Origin, Type, Reason and created and last modified dates; no contacts. On Refused, `reason` is `invalid`. | Answered, Refused, Failed | 
| Create a case with details, `create-case-details` (`CreateCaseDetailsModuleRef`) | `subject`, `description`, `priority`, `suppliedname`, `suppliedemail`, `suppliedphone`, `suppliedcompany`, `comments`, `origin`; `-` leaves a field out; the account from `accountId` | The new case and the account read again in `compositeResponse`, and `reason`. An empty `subject` is Refused, with `reason` `invalid` and `field` `subject`, before any Salesforce call. | Answered, Refused, Failed | 

The guide flow also calls the gateway directly, outside any module: `updateCase` and then `getCase` for Update case, `createContact` for Add a contact, and `queryAccounts` for four reads: the org's case statuses, a case by its case number, the names of the accounts on the **Which record is this?** screen, and a new case's read-back.

Each module limits what it can act on, and who can run it:
+ No module takes an account ID or a case ID as an input. Each one reads those IDs from the calling contact's attributes and checks them itself, so it acts only on the record your inbound flow, or the agent's search, put on the contact. Update a record's one ID input, `recordId`, names a contact, and the module refuses it, with `reason` `scope`, unless that Salesforce Contact is on the account in `accountId`.
+ Eight of the ten are tools for AI agents. Find the record and Create a case with details aren't: only the guide runs them, and Create a case with details only from the account view's **Create case** card.
+ An AI agent runs a module only when its own security profile lists the module, and agent assist also needs the agent's security profile to list it; see [Give agents agent assist in the agent workspace](real-time-data-integration-ai-assist.md) and [Let self-service read the verified account](real-time-data-integration-ai-data.md).
+ Anyone who can edit flows (**Flows - Edit**) can add an **Invoke flow module** block that runs any module in the instance, so your Connect Customer administrator decides which security profiles have **Flows - Edit**.

**To call a module from your own flow**

1. Set the record IDs the module reads, such as `accountId`, as contact attributes before the call, from a trusted source.

1. Add an **Invoke flow module** block, and choose the module, such as `{{stack-name}}-account-360-read-account`, and its alias `v1`. In a flow you build with CloudFormation, use the stack output, such as `ReadAccountModuleRef`.

1. Set the module's inputs, and connect each of its branches and its Error branch.

1. Read the module's output fields as `$.Modules.ResultData.{{field}}`, for example `$.Modules.ResultData.reason`, and copy any value you need later into a contact attribute.

**Example: play a case's status in your IVR with Read a case**

In this example, your IVR has verified the caller, and your own system has returned the Salesforce IDs of the caller's account and of the case they are calling about. Read a case takes no inputs: it reads the case in `caseId`, only if it is on the account in `accountId`, so it needs both as Salesforce IDs, not numbers.

1. A **Set contact attributes** block sets `accountId` to the account ID and `caseId` to the case ID.

1. An **Invoke flow module** block calls `{{stack-name}}-account-360-read-case`, alias `v1`, with no inputs.

1. Connect its branches. **Found** and **Trimmed** return the case; Trimmed is the read without the description and the related lists, after the full read fails. **NotFound** means no case with that ID is on that account. **Refused** means an ID failed the module's check, with `$.Modules.ResultData.reason` set to `invalid`. **Failed** and the Error branch mean the read didn't run. Send each of the last three to a queue or a message.

1. On **Found** and **Trimmed**, a **Set contact attributes** block copies the case's status, `$.Modules.ResultData.records[0].Status`, into an attribute such as `caseStatus`. The result also holds `CaseNumber`, `Subject`, `Priority` and the other fields of the case in `records[0]`. Write a path with an array index in backticks, ``$.Modules.ResultData.records[0].Status``, as the guide flow does.

1. A **Play prompt** block reads it to the caller, such as **Your case is $.Attributes.caseStatus.**

**To build a module of your own**

1. In the Connect Customer admin website, create a flow module, and define its inputs, its output fields and its custom branches.

1. Check each input and each record ID the module reads from the contact's attributes before you use it.

1. Add an **External Tool** block that calls one tool on your system's target. For the application, choose the integration of the gateway; for the stack's gateway, that is `Salesforce - {{stack-name}}`. Choose the tool by its name, `gateway_{{gateway-id}}__{{target-name}}___{{operation}}`. For an example that starts a Salesforce flow, see [Start your system's automation from the guide](real-time-data-integration-run-automation.md).

1. End each path with a **Return** block that names its branch and sets the output fields, including on the External Tool block's error branch.

1. Publish the module, and create a version and an alias. Call it from your flows, such as a copy of the guide flow, as in the previous procedure.

To let agent assist run your module as well, you need an AI agent of your own, because the stack sets the tools of its own agent assist AI agent:

1. Create the module as a tool, with external invocation on; see [Create module as tools](contact-flow-modules.md#create-module-as-tools).

1. Add the module's version, not an alias, to the tools of your AI agent (see [Create AI agents in Connect Customer](create-ai-agents.md)), and list the module in your AI agent's own security profile, in its allowed flow modules (see [Assigning security profile permissions to AI agents](ai-agent-security-profile-permissions.md)).

1. Make your AI agent the one agents see: name it in the **Connect assistant** block of the flow that takes the contact.

1. Add the module to the agents' security profile, in the **Flow modules** section of its **Edit** page; see [Give agents agent assist in the agent workspace](real-time-data-integration-ai-assist.md).

To have the guide find the record a contact is about your own way, for example from a bot conversation or by a rule in your system, build a module with the Find the record contract and set the stack's **Find the record module** parameter (`FindRecordModuleArn`) to its alias ARN with a stack update. The alias ARN is the module's ARN, a colon and the alias ID: `arn:aws:connect:{{region}}:{{account}}:instance/{{instance-id}}/flow-module/{{module-id}}:{{alias-id}}`. To list a module's alias IDs, run **aws connect list-contact-flow-module-aliases --instance-id {{instance-id}} --contact-flow-module-id {{module-id}} --query "ContactFlowModuleAliasSummaryList[].[AliasName,AliasId]" --output text** at the prompt. The guide then opens the case your module names in the case view, one account in the account view, or several accounts as possible matches. Leave the parameter empty to use the stack's built-in module, which reads the attributes in [How the guide finds the record](real-time-data-integration-start.md#real-time-data-integration-find-record). The contract:

1. **Inputs**, seven required strings, each empty when the contact has no value: `contractVersion` (`1`), `contactId` (the `customerContactId` attribute, which the stack's inbound flow sets from `$.ContactId`; your own inbound flows must set it too), `channel`, `queueArn`, `phone` (the `customerPhone` attribute, the caller's number on a call), `email` (the `contactEmail` attribute) and `caseNumber` (the contact's arrival `caseId`, unless it is a case ID). The guide passes exactly these seven, because Connect Customer refuses an input a module doesn't declare. To use the other attributes, read them from the contact in your module: `$.Attributes.accountId`, `$.Attributes.arrivalCaseId` (the contact's arrival `caseId`, which the guide copies there) and `$.Attributes.contactEmail`. Only `contactId` identifies the contact; the others are hints.

1. **Branches**: One, Several, None and Error. On Error, the guide shows the screen **We couldn't look up this contact's record**, with the note **This contact's record could not be looked up. Search for the customer.**, **Try again**, which runs your module again with the same inputs, and the searches.

1. **Output fields**, strings, `-` when unset. One returns `count` `1`, `accountId1` and optionally `caseId`, `contactId` and `reason` (`bot`, `phone`, `email`, `caseNumber`, `contact` or `rule`). Several returns `count` from 2 to 5 and that many account IDs in `accountId1` onward. None with `reason` `nomatch` shows "No account matched".

1. **What the guide refuses**:
   + an account ID that doesn't start with 001;
   + a case ID that doesn't start with 500;
   + a contact ID that doesn't start with 003;
   + an ID that holds any of `' \ % & # ? / +`;
   + a Several count outside 2 to 5.

   A refused output, or a case that isn't on the returned account, shows the same **We couldn't look up this contact's record** screen. A failed read of the record the module returned shows the error screen.

**Example: open the account by a loyalty number**

In this example, your inbound flow sets the contact attribute `loyaltyId` after it verifies the caller, and an autolaunched Salesforce flow, `Find_Loyalty_Account`, takes `loyaltyId` and returns the account's ID in its output variable `accountId`.

1. Create a flow module with the seven inputs and the four branches of the contract, and the output fields `count`, `accountId1` to `accountId5`, `caseId`, `contactId` and `reason`.

1. Add a **Check contact attributes** block on `$.Attributes.loyaltyId` that sends an empty value, or one that holds any of `' \ % & # ? / +`, to a **Return** block with the branch None.

1. Add an **External Tool** block that calls the `salesforce` target's `runFlow` tool, with `domainName` set to your org's My Domain name, `flowApiName` set to **Find\_Loyalty\_Account** and `body` set to `{"inputs": [{"loyaltyId": "$.Attributes.loyaltyId"}]}`. Send its error branch to a **Return** block with the branch Error.

1. On its success branch, add a **Set contact attributes** block that copies ``$.ExternalTool.ResultData[0].outputValues.accountId``, with the backticks, into the flow attribute `foundAccountId`, and a **Check contact attributes** block on `$.FlowAttributes.foundAccountId` that sends a value that doesn't start with 001 to a **Return** block with the branch None and `reason` set to `nomatch`.

1. End with a **Return** block with the branch One, and set `count` to `1`, `accountId1` to `$.FlowAttributes.foundAccountId`, `reason` to `rule`, and every other output field to `-`.

The guide then opens that account in the account view. Every branch sets every output field, with `-` for the ones it doesn't use.

Publish the module, with a version and an alias, in the stack's instance. Keep its external invocation off and leave it out of every AI agent's tools. Connect Customer checks the guide's **Invoke flow module** block against your module when the stack creates or updates the guide flow, so a module without these inputs and branches fails that step. The module can do anything a flow module can: run an invocable Apex action with `runApexAction` or an autolaunched Salesforce flow with `runFlow`, call another gateway tool or an AWS Lambda function, or use flow logic alone, for example to choose a customer by a loyalty number or to open the customer's most recent open case.