# Example Salesforce Flows for Advocate

{% hint style="warning" %}
**Important:** You must have the core Salesforce for Advocate integration set up in order for these flows to work properly. For instructions, refer to [Integrate with Salesforce for Advocate](https://integrations.impact.com/impact-brand/docs/integrate-with-salesforce-for-advocate). These flows are configured to send data to the single Advocate Program ID established during your main Salesforce integration setup. It is not possible for these flows to attribute data to multiple programs.
{% endhint %}

Your Salesforce for Advocate integration relies on *flows* to share data between impact.com and Salesforce. At minimum, a *user upsert* (or *attribution*) flow is required. Optionally, an *event tracking* flow can be added. Additional flows may be useful for your program as well. Work with your impact.com-assigned implementation engineer or our [support team](https://app.impact.com/secure/advertiser/support/customer-support-portal-flow.ihtml) for guidance on the recommended approach.

Below are generic examples of an attribution flow that upserts users from Salesforce to impact.com, as well as an event tracking flow that sends order details from Salesforce to impact.com.

## Attribution flow

Attribution flows can be set up to upsert users from Salesforce to impact.com whenever new Leads contain a `Referred By Code`.

{% stepper %}
{% step %}

### Create a new flow

1. In your Salesforce account, launch Flow Builder.
2. In the top menu bar, select **New Flow**.
3. Select **Record-Triggered Flow** from the options, then select **Create.**
4. Under *Select Object*, search for `Lead`.
5. Under *Configure Trigger*, select **A record is created or updated**.
6. Under *Set Entry Conditions*, select **None**.
7. Ensure the field *Optimize the Flow for* is set to **Actions and Related Records**.
8. Select **Done**.
   {% endstep %}

{% step %}

### Create a `Referred By Code` field

1. In your Salesforce account, open the **Setup** page.
2. Go to **Object Manager**.
3. Select **Lead**.
4. Select **Fields & Relationships** from the sidebar.
5. Select **New**.
6. Choose the **Text** data type, then select **Next**.
7. In the **Label** field, enter `Referred By Code`.
8. Optionally, add a description.
9. Select **Next**.
10. Select which profiles should have access to the field depending on your organization structure, then select **Next**.
11. Select which page layouts should display this field. **All** is the recommended option.
12. Select **Save**.
    {% endstep %}

{% step %}

### Create a decision based on the `Referred By Code` field

1. In Salesforce, within your new flow, select **+ \[Add]** between the *Start* and *End* of the flow.
2. Select **Decision** from the *Logic* section.
3. In the *Label* field, add a title: `Has Referred By Code Been Added?`.
4. Optionally, add a description.
5. Ensure **New Outcome** is selected.
6. Under *Condition Requirements to Execute Outcome*, select **All Conditions Are Met (AND)** from the dropdown list.
7. In the *Resource* field, select **Record / Lead** from the dropdown list, then scroll down to select **Referred By Code** The final result should look like: `$Record > Referred By Code`.
8. From the *Operator* dropdown list, select **Is Null**.
9. In the *Value* field, type `false` and hit **Enter** or **Return**.
10. Select **Default Outcome** and change the label to `Referred By Code is blank` or similar.
11. Select **Done**.
    {% endstep %}

{% step %}

### Create an action to send the Referred Lead to impact.com

1. In Salesforce, within your flow, select **+ \[Add]** on the *Has Referred By Code* branch of the flow.
2. Select **Action** under the *Interaction* section of the list.
3. In the *New Action* window, choose **Type in the Filter By** dropdown.
4. Select **Apex Action** from the list.
5. Select the **Action** field, then select **Upsert User by ID**.
6. In the *Label* field, type `Upsert Referred User to impact.com`.
7. Optionally, add a description.
8. For both the `accountId` and `userId` fields, select **Lead** from the dropdown list, then scroll down to select **Email**.
9. Use the sliders to map the fields you want to send between Salesforce and impact.com.

{% hint style="warning" %}
**Required:** The `Referred By Code` created in step 2 needs to be mapped back to the impact.com `Referred By Code` field. This field mapping is required to pass the referral attribution connection between the Salesforce instance and the Advocate tenant.
{% endhint %}
{% endstep %}

{% step %}

### Review and test the flow

If the Flow looks complete, select **Save** to save your work.

You can use the Test and Debug tools to run the flow and review the outcomes. To fully test the Flow, create a new lead with a “Referred By Code” manually added to that field. The user should appear in your Advocate program's [participants list](https://help.impact.com/en/support/solutions/folders/155000000143) after a few minutes.
{% endstep %}
{% endstepper %}

## Opportunity-Closed Won flow

You can set up an Opportunity-Closed Won flow to send an event to impact.com when the associated Opportunity gets to the Closed Won stage.

{% stepper %}
{% step %}

### Create a new flow

1. In your Salesforce account, launch Flow Builder.
2. In the top menu bar, select **New Flow**.
3. Select **Record-Triggered Flow** from the options, then select **Create**.
4. Under *Select Object*, search for **Opportunity**.
5. Under *Configure Trigger*, select **A record is created or updated**.
6. Under *Set Entry Conditions*, from the *Condition Requirements* dropdown list, select **All Conditions Are Met (AND)**.
7. Select **Field**, then select **customerEmail**.
8. Select **Operator**, then select **Is Null**.
9. In the *Value* field, type `false` and hit **Enter** or **Return**.
10. Select **Add Condition** to create a second condition.
11. Select **Field**, then select **StageName**.
12. Select **Value** and select **Closed Won** from the dropdown menu.
13. Ensure the field *Optimize the Flow for* is set to **Actions and Related Records**.
14. Select **Done**.
    {% endstep %}

{% step %}

### Create an action to send the `Closed Won` event to impact.com

1. In Salesforce, within your new flow, select the **+ \[Add]** after the *After Last* label in the Flow.
2. Select **Action** under the *Interaction* section of the list.
3. In the *New Action* window, choose **Type** in the *Filter By* dropdown.
4. Select **Apex Action** from the list under the *Filter By* dropdown.
5. Select the **Action** field and select **Track Event by Email**.
6. In the *Label* field, type `Send Closed Won to impact.com`.
7. Add an optional description.
8. Enter values for the following fields:
   * **CampaignId:** The 5-digit `ID` of the Advocate program. If you already have a Performance program and are using event sharing, the `CampaignId` will be the 5-digit `ID` of the Performance program.
   * **CustomerEmail:** The lowercase email address you initially used for this field.
   * **EventDate:** Set to `NOW` if you want it to reflect the time that the Flow is triggered, or it can be tied to another date field from the Opportunity or Lead.
   * **EventTypeId:** Set to `closedWon`.
   * **OrderId:** Set to the `ID` of the Opportunity.
   * **CustomerId:** Set as the same email address you used in `CustomerEmail`.
9. Select **Done**.
   {% endstep %}

{% step %}

### Review and test the flow

If the Flow looks complete, select **Save** to save your work.

You can use the Test and Debug tools to run the flow and review the outcomes. To fully test the Flow, convert the lead from above into an Opportunity and then push it to a Closed Won stage. The `closedWon` event should appear in that [participant's profile](https://help.impact.com/en/support/solutions/articles/155000002217-advocate-participant-profiles-explained) in impact.com after a couple of minutes.
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://integrations.impact.com/integration-guides/for-brands/advocate/advocate-plugin-integration/integrate-with-salesforce-for-advocate/example-salesforce-flows-for-advocate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
