Generative Flow Examples | MuleSoft Documentation
# Generative Flow Examples
Use these examples to help you get started with the generative flow feature and understand how to create messages for your own use cases.
## Implement a New Case Notification
Your company receives numerous customer inquiries and support requests daily. You want to implement email notifications whenever a new case is created in Salesforce so you can respond quickly to customers and ensure timely case resolution to increase customer satisfaction.
To generate a flow for this task, enter a message like: `Generate a flow that sends an email when a case is created in Salesforce`.
This message generates XML code for the new flow, similar to this:
```
<flow name="sendEmailOnNewCaseFlow">
  <salesforce:new-object-listener objectType="Case" doc:name="On New Object" doc:id="8b5dec5e-31a2-4b78-9188-b681891898be" config-ref="Salesforce_Sfdc_config"> **(1)**
    <scheduling-strategy>
      <fixed-frequency />
    </scheduling-strategy>
  </salesforce:new-object-listener>
  <salesforce:query-result-list doc:name="Query result list" doc:id="79da7485-339c-4ab6-956a-0684210b2ded" config-ref="Salesforce_Sfdc_config"/>
  <salesforce:query-result-stream-by-id doc:name="Query result stream by id" doc:id="bdf16fc8-017f-439b-b1fc-3bff23d881d8" config-ref="Salesforce_Sfdc_config"/>
  <ee:transform doc:name="Transform Message" >
    <ee:message > **(2)**
      <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload map {
  CaseNumber: $.CaseNumber,
  Subject: $.Subject,
  Description: $.Description,
  Status: $.Status,
  Priority: $.Priority
}]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <email:send config-ref="Email_SMTP" fromAddress="user1@domain.com" subject="New Case Created"> **(3)**
    <email:to-addresses >
      <email:to-address value="user3@domain.com" />
      <email:to-address value="user2@domain.com" />
    </email:to-addresses>
    <email:body contentType="text/html" > **(4)**
      <email:content ><![CDATA["<h1>New Case Created:</h1>"]]></email:content>
    </email:body>
  </email:send>
</flow>
```

This flow starts with a Salesforce Connector `on-new-object-listener` trigger so that when a new case is created, the flow is triggered.  
---  
The payload for the new case is set in variables using a DataWeave script within the Transform component.  
The email is sent using the Email Connector **Send** operation with the configured SMTP settings.  The subject of the email is set to `New Case Created: ` followed by the case subject.  
The body of the email is set to HTML format and includes the case ID, subject, and description.  
### Complete the Flow
After reviewing and inserting the generated flow into your project:
  1. Type the target email address to use in your use case, for example:
`<email:to-address value="demo@gmail.com" />`
  2. Add the target email, `${email.username}` to your configuration properties file.
  3. Optionally, enter a value for the scheduling strategy in the `fixed-frequency` attribute. The **Scheduler** initiates the flow to poll for new cases according to the value you set, for example:
```
<scheduling-strategy>
  <fixed-frequency frequency="5000" />
</scheduling-strategy>
```

This example flow is configured to poll for new cases every 5000 milliseconds.
  4. Use built-in snippets to add configurations for your Salesforce instance and email inbox.


The following is an example of the completed flow:
```
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
  xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
  xmlns:http="http://www.mulesoft.org/schema/mule/http"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
  xmlns:tls="http://www.mulesoft.org/schema/mule/tls"
  xmlns:email="http://www.mulesoft.org/schema/mule/email"
  xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
  http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
  http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
  http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd
  http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd
  http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">
  <salesforce:sfdc-config name="Salesforce_Config">
    <salesforce:basic-connection username="${salesforce.username}" password="${salesforce.password}" securityToken="${salesforce.token}" />
  </salesforce:sfdc-config>
  <email:smtp-config name="Email_SMTP">
    <email:smtps-connection host="${email.host}" user="${email.username}" password="${email.password}">
      <tls:context>
        <tls:trust-store insecure="true" />
      </tls:context>
    </email:smtps-connection>
  </email:smtp-config>
  <flow name="sendEmailOnNewCase">
    <salesforce:new-object-listener objectType="Case" config-ref="Salesforce_Config">
      <scheduling-strategy>
        <fixed-frequency frequency="5000" />
      </scheduling-strategy>
    </salesforce:new-object-listener>
    <set-variable variableName="caseNumber" value="#[payload.CaseNumber]" doc:name="caseNumberVar" doc:id="vysmen" />
    <set-variable variableName="status" value="#[payload.Status]" doc:name="statusVar" doc:id="yeakie" />
    <email:send config-ref="Email_SMTP" doc:name="Send Email" subject="#['Case ' ++ vars.caseNumber ++ ' was created']">
      <email:to-addresses>
        <email:to-address value="demo@gmail.com" />
      </email:to-addresses>
      <email:body contentType="text/html">
        <email:content>
          <![CDATA[#['New case ' ++ vars.caseNumber ++ ' was created with status ' ++ vars.status]]]>
        </email:content>
      </email:body>
    </email:send>
  </flow>
</mule>
```

## Update a Phone Number
Provide a streamlined process for Salesforce System Administrators by using an API call to efficiently and securely update a phone number.
To generate a flow for this task, enter a message like: `Create a flow that listens for an API call and updates the phone number of a Salesforce Account object`.
This message generates XML code for the new flow, similar to this:
```
<flow name="updatePhoneNumber" doc:id="1">
  <http:listener doc:name="Listener" doc:id="2" config-ref="HTTP_Listener_config" path="/updatePhoneNumber"/> **(1)**
  <ee:transform doc:name="Set Payload" doc:id="3"> **(2)**
    <ee:message>
      <ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
  "accountId": payload.accountId,
  "phoneNumber": payload.phoneNumber
}]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <salesforce:update doc:name="Update Account" doc:id="4" config-ref="Salesforce_Config" type="Account"> **(3)**
    <salesforce:records>#[payload]</salesforce:records> **(4)**
  </salesforce:update>
</flow>
```
## Update a Phone Number
Provide a streamlined process for Salesforce System Administrators by using an API call to efficiently and securely update a phone number.
To generate a flow for this task, enter a message like: `Create a flow that listens for an API call and updates the phone number of a Salesforce Account object`.
This message generates XML code for the new flow, similar to this:
```
<flow name="updatePhoneNumber" doc:id="1">
  <http:listener doc:name="Listener" doc:id="2" config-ref="HTTP_Listener_config" path="/updatePhoneNumber"/> **(1)**
  <ee:transform doc:name="Set Payload" doc:id="3"> **(2)**
    <ee:message>
      <ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
  "accountId": payload.accountId,
  "phoneNumber": payload.phoneNumber
}]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <salesforce:update doc:name="Update Account" doc:id="4" config-ref="Salesforce_Config" type="Account"> **(3)**
    <salesforce:records>#[payload]</salesforce:records> **(4)**
  </salesforce:update>
</flow>
```

The flow starts with an HTTP Listener that listens for an API call on the `/updatePhoneNumber` path.  
---  
In the **Transform** component, the payload is set using the **< ee:transform>** operation to specify the account ID and phone number to update.  
The Salesforce Connector **Update** operation is used to update the Account object in Salesforce.  The `type` attribute is set to `Account` to specify the object type to update.  
The payload is passed as the records to update in Salesforce.  
### Complete the Flow
To run this flow, use built-in snippets to add configurations for your Salesforce instance. For more information about built-in snippets, see Working with Code Snippets.
The following is an example of the completed flow:
```
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
   xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
   xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
   xmlns:http="http://www.mulesoft.org/schema/mule/http"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
   xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
             http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
             http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
             http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd">
  <http:listener-config name="HTTP_Listener_config">
    <http:listener-connection host="0.0.0.0" port="8081" />
  </http:listener-config>
  <salesforce:sfdc-config name="Salesforce_Config">
    <salesforce:basic-connection username="${salesforce.username}" password="${salesforce.password}" securityToken="${salesforce.token}" />
  </salesforce:sfdc-config>
  <flow name="updateAccountPhone">
    <http:listener config-ref="HTTP_Listener_config" doc:name="Listener" path="/updateAccountPhone" allowedMethods="POST" doc:id="listener" />
    <set-variable variableName="accountIdVar" value="#[payload.accountId]" mimeType="application/json" doc:name="Set Account ID var" doc:id="set-variable-accountId" />
    <set-variable variableName="phoneVar" value="#[payload.phoneNumber]" mimeType="application/json" doc:name="Set Phone var" doc:id="set-variable-phone" />
    <salesforce:update type="Account" config-ref="Salesforce_Config" doc:name="Update Salesforce Account" doc:id="update-account">
      <salesforce:records>
        <![CDATA[#[output application/json ---
        [{
          "Id": vars.accountIdVar,
          "Phone": vars.phoneVar
        }]]]]>
      </salesforce:records>
    </salesforce:update>
    <logger doc:name="Logger" message="Phone number updated for Account with Id: #[vars.accountIdVar]" doc:id="logger" />
  </flow>
</mule>
```

## Synchronize Account Objects
This flow enables real-time updates between Salesforce and your database to ensure the data is always current and accurate across both Salesforce and your database. As soon as the phone field changes in Salesforce, it updates immediately in the database and vice versa.
To generate a flow for this task, enter a message like: `Create a flow that syncs the phone field of account objects between a Salesforce org and a database`.
This message generates XML code for the new flow, similar to this:
```
<flow name="syncAccountPhone" doc:id="1">
  <http:listener doc:name="Listener" doc:id="2" config-ref="HTTP_Listener_config" path="/syncAccountPhone" allowedMethods="POST" /> **(1)**
  <set-variable variableName="accountId" value="#[payload.accountId]" doc:name="Set Account ID" doc:id="3" /> **(2)**
  <set-variable variableName="phone" value="#[payload.phone]" doc:name="Set Phone" doc:id="4" /> **(3)**
  <db:select doc:name="Select Account from Database" doc:id="5" config-ref="Database_Config">
    <db:sql><![CDATA[SELECT * FROM Account WHERE Id = :accountId]]></db:sql> **(4)**
    <db:input-parameters><![CDATA[#[{
      "accountId": vars.accountId
    }]]]></db:input-parameters>
  </db:select>
  <ee:transform doc:name="Create Update Data" doc:id="6"> **(5)**
    <ee:message>
      <ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
  "Id": vars.accountId,
  "Phone": vars.phone
}]]></ee:set-payload>
    </ee:message>
  </ee:transform>
  <salesforce:update config-ref="Salesforce_Config" doc:name="Update Salesforce Account" doc:id="7" type="Account">
    <salesforce:records><![CDATA[#[payload]]]></salesforce:records>
  </salesforce:update> **(6)**
  <db:update doc:name="Update Account in Database" doc:id="8" config-ref="Database_Config">
    <db:sql><![CDATA[UPDATE Account SET Phone = :phone WHERE Id = :accountId]]></db:sql> **(7)**
    <db:input-parameters><![CDATA[#[{
      "accountId": vars.accountId,
      "phone": vars.phone
    }]]]></db:input-parameters>
  </db:update>
  <set-payload value="#['Phone field synced successfully']" doc:name="Set Payload" doc:id="9" /> **(8)**
</flow>
```

Configure the HTTP **Listener** operation to listen for a POST request on the `/syncAccountPhone` path.  
---  
Extract the account ID from the payload and store it in a variable named `accountId`.  
Extract the phone number from the payload and store it in a variable named `phone`.  
The Database Connector **Select** operation retrieves the account record from the database based on the account ID.  
Use a DataWeave **Transform** operation to create the updated data for the Salesforce Account object.  
Use the Salesforce Connector **Update** operation to update the Salesforce account.  
The account information is also updated in the database using a Database Connector **Update** operation.  
The payload is set to `Phone field synced successfully` to indicate a successful sync.  
### Complete the Flow
After reviewing and inserting the generated flow into your project:
  1. Update the name of the database table and fields to match what is configured in your MySQL database.
  2. To run this flow, use built-in snippets to add configurations for your Salesforce instance.
Configure the HTTP **Listener** operation to listen for a POST request on the `/syncAccountPhone` path.  
---  
Extract the account ID from the payload and store it in a variable named `accountId`.  
Extract the phone number from the payload and store it in a variable named `phone`.  
The Database Connector **Select** operation retrieves the account record from the database based on the account ID.  
Use a DataWeave **Transform** operation to create the updated data for the Salesforce Account object.  
Use the Salesforce Connector **Update** operation to update the Salesforce account.  
The account information is also updated in the database using a Database Connector **Update** operation.  
The payload is set to `Phone field synced successfully` to indicate a successful sync.  
### Complete the Flow
After reviewing and inserting the generated flow into your project:
  1. Update the name of the database table and fields to match what is configured in your MySQL database.
  2. To run this flow, use built-in snippets to add configurations for your Salesforce instance.


The following is an example of a completed flow:
```
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
   xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
   xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
   xmlns:http="http://www.mulesoft.org/schema/mule/http"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
   xmlns:db="http://www.mulesoft.org/schema/mule/db"
   xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
             http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
             http://www.mulesoft.org/schema/mule/salesforce http://www.mulesoft.org/schema/mule/salesforce/current/mule-salesforce.xsd
             http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd">
  <salesforce:sfdc-config name="Salesforce_Config">
    <salesforce:basic-connection username="${salesforce.username}" password="${salesforce.password}" securityToken="${salesforce.token}" />
  </salesforce:sfdc-config>
  <db:config name="Mysql_Database_Config">
    <db:my-sql-connection host="${mysql.host}" port="${mysql.port}" user="${mysql.username}" password="${mysql.password}" database="${mysql.database}" />
  </db:config>
  <flow name="SyncAccountPhoneToDatabase">
    <salesforce:modified-object-listener objectType="Account" config-ref="Salesforce_Config" doc:name="On Modified Object" doc:id="listener">
      <scheduling-strategy>
        <fixed-frequency />
      </scheduling-strategy>
    </salesforce:modified-object-listener>
    <ee:transform doc:name="Transform Message" doc:id="transform">
      <ee:message>
        <ee:set-payload>
          <![CDATA[%dw 2.0
          output application/json
          ---
          {
            Id: payload.Id,
            Phone: payload.Phone
          }]]>
        </ee:set-payload>
      </ee:message>
    </ee:transform>
    <db:update doc:name="Update Phone in Database" doc:id="update">
      <db:sql>
        <![CDATA[UPDATE Account SET Phone = :Phone WHERE Id = :Id]]>
      </db:sql>
      <db:input-parameters>
        <![CDATA[#[{
          Phone: payload.Phone,
          Id: payload.Id
        }]]]>
      </db:input-parameters>
    </db:update>
  </flow>
</mule>
```

## Automate New Record Notification
Automate email notifications to users when a new database record is updated to minimize manual intervention, which improves efficiency and reduces the chances of errors or delays.
To generate a flow for this task, enter a message like: `Generate a flow to send an email when a record is created in the database`.
This generates XML code for the new flow, similar to this:
```
<flow name="sendEmailFlow">
  <db:listener table="record" doc:name="On Table Row" doc:id="dbListener" watermarkColumn="CREATION_DATE">
    <scheduling-strategy> **(2)**
      <fixed-frequency frequency="1" timeUnit="SECONDS"/>
    </scheduling-strategy>
  </db:listener> **(1)**
  <set-variable variableName="record" value="#[payload]" doc:name="Set Variable" doc:id="setVariable"/> **(3)**
  <email:send config-ref="Email_SMTP" fromAddress="sender@example.com" subject="New Record Created"> **(4)**
    <email:to-addresses>
      <email:to-address value="recipient@example.com"/> **(5)**
    </email:to-addresses>
    <email:body contentType="text/plain">
      <email:content><![CDATA[New record created with ID: #[vars.record.id]]]></email:content>
    </email:body>
  </email:send>
</flow>
```

The flow starts with a Database Connector **Listener** component that listens for new records in the database `record` table.  
---  
The scheduling strategy is set to trigger the flow at a fixed frequency of every one second.  
The payload of the listener is stored in a variable named `record` using the **Set Variable** operation.  
An email is sent from the address `sender@example.com` using the **Email Send** component.  
The email is sent to the address `recipient@example.com`.  
### Complete the Flow
After reviewing and inserting the generated flow into your project:
  1. Update the sender email address, recipient email addresses, and email body for your specific use case.
  2. Update the database table name so that it corresponds to the database table on which the flow listens for new records.
  3. Update the `watermarkColumn` in the Database **Listener** to whichever database column is used to indicate new records.
The values that are taken from this column are used to filter the contents of the next poll, so that only rows with a greater watermark value are processed.
  4. To run this flow, use built-in snippets to add configurations for your database and email inbox.
The flow starts with a Database Connector **Listener** component that listens for new records in the database `record` table.  
---  
The scheduling strategy is set to trigger the flow at a fixed frequency of every one second.  
The payload of the listener is stored in a variable named `record` using the **Set Variable** operation.  
An email is sent from the address `sender@example.com` using the **Email Send** component.  
The email is sent to the address `recipient@example.com`.  
### Complete the Flow
After reviewing and inserting the generated flow into your project:
  1. Update the sender email address, recipient email addresses, and email body for your specific use case.
  2. Update the database table name so that it corresponds to the database table on which the flow listens for new records.
  3. Update the `watermarkColumn` in the Database **Listener** to whichever database column is used to indicate new records.
The values that are taken from this column are used to filter the contents of the next poll, so that only rows with a greater watermark value are processed.
  4. To run this flow, use built-in snippets to add configurations for your database and email inbox.


The following is an example of the completed flow:
```
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
  xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
  xmlns:http="http://www.mulesoft.org/schema/mule/http"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:salesforce="http://www.mulesoft.org/schema/mule/salesforce"
  xmlns:db="http://www.mulesoft.org/schema/mule/db"
  xmlns:email="http://www.mulesoft.org/schema/mule/email"
  xmlns:tls="http://www.mulesoft.org/schema/mule/tls"
  xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
  http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
  http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
  http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
  http://www.mulesoft.org/schema/mule/tls http://www.mulesoft.org/schema/mule/tls/current/mule-tls.xsd">
  <db:config name="Mysql_Database_Config">
    <db:my-sql-connection host="${mysql.host}" port="${mysql.port}" user="${mysql.username}" password="${mysql.password}" database="${mysql.database}" />
  </db:config>
  <email:smtp-config name="Email_SMTP">
    <email:smtps-connection host="${email.host}" user="${email.username}" password="${email.password}">
      <tls:context>
        <tls:trust-store insecure="true" />
      </tls:context>
    </email:smtps-connection>
  </email:smtp-config>
  <flow name="sendEmailFlow">
    <db:listener table="records" doc:name="On Table Row" doc:id="dbListener" watermarkColumn="CREATION_DATE" config-ref="Mysql_Database_Config">
      <scheduling-strategy>
        <fixed-frequency frequency="1" timeUnit="SECONDS" />
      </scheduling-strategy>
    </db:listener>
    <set-variable variableName="record" value="#[payload]" doc:name="Set Variable" doc:id="setVariable" />
    <email:send config-ref="Email_SMTP" fromAddress="sender@example.com" subject="New Record Created">
      <email:to-addresses>
        <email:to-address value="recipient@example.com" />
      </email:to-addresses>
      <email:body contentType="text/plain">
        <email:content>
          <![CDATA[New record created with ID #[vars.record.id]]]>
        </email:content>
      </email:body>
    </email:send>
  </flow>
</mule>
```

## See Also
  * Working with Code Snippets
  * Creating Integrations Using Agentforce
  * Scheduler (<scheduler/>)
  * Set Payload (<set-payload/>)
  * Set Variable (<set-variable/>)
  * Defining and Securing Properties for a Mule Application