> ## Documentation Index
> Fetch the complete documentation index at: https://docs.refold.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Sync Data from 3rd Party apps

> Create a sample workflow to sync data from Salesforce and API Proxy

Consider a use-case where you want to sync all the opportunities present in Salesforce to your system.

In the Salesforce integration, go to `Workflows` and create a new workflow by clicking on `+Add Workflow` button and name it as `Sync Opportunities`.

## Build Workflow

Follow the steps given to build the workflow:

<Steps>
  <Step title="Add Trigger in Start Node">
    All workflows start with a trigger, which determines when the workflow will run and how data is passed into the workflow.
    For this workflow we will use the Event Based trigger.

    <Tip>
      Learn more about the triggers and its types [here](https://docs.gocobalt.io/build/workflow/triggering_workflow).
    </Tip>

    Click on the `Start Node`, select your native app option and click on `+ Create New Event`.

    <img height="200" src="https://mintcdn.com/cobalt-55/C3P12YsraPVmMY5N/images/Examples/salesforce/event_salesforce.png?fit=max&auto=format&n=C3P12YsraPVmMY5N&q=85&s=92e259917a13a5ce7d1f7a08e075b681" alt="Event for workflow" data-path="images/Examples/salesforce/event_salesforce.png" />

    Give a name to your event and keep the payload as an empty object.
  </Step>

  <Step title="Add Salesforce Node">
    Now to fetch all the opportunities present in your user's account, we need to call Salesforce API.

    Click on `Nodes` option in the top right and drag the Salesforce Node from **Native Apps** section to the workflow builder. Connect this node with Start Node.

    <img height="200" src="https://mintcdn.com/cobalt-55/C3P12YsraPVmMY5N/images/Examples/salesforce/action_salesforce.png?fit=max&auto=format&n=C3P12YsraPVmMY5N&q=85&s=754d7de08157a1d1224f2da26b475a48" alt="Choose Node" data-path="images/Examples/salesforce/action_salesforce.png" />
  </Step>

  <Step title="Fetch Opportunities from Salesforce using Action">
    Click on the Salesforce Node and select `Get Entity with SOQL` action.

    <img height="200" src="https://mintcdn.com/cobalt-55/h1VL7v_8HaANGLZ5/images/Examples/salesforce/soql_salesforce.png?fit=max&auto=format&n=h1VL7v_8HaANGLZ5&q=85&s=ca5cf031f4b31f9e86ad964a4830167c" alt="SOQL Action for workflow" data-path="images/Examples/salesforce/soql_salesforce.png" />

    Add the following under `SOQL query` field and click on `Save`.

    ```
    SELECT Id,Amount,AccountId, CloseDate,ContactId,Description,ExpectedRevenue,LeadSource, Name, OwnerId, Probability,StageName,Type FROM Opportunity
    ```

    <Info>
      You can provide any additonal fields that you want to fetch about opportunities from Salesforce by adding it to this query.
    </Info>
  </Step>

  <Step title="Add Custom Code node">
    We have fetched all the opportunities, but you might require to structure it as a payload which your platform can receive. Let's do this using **Custom Code** node.

    Drag the Custom Code from the Utility Nodes section onto the workflow builder and connect it with Salesforce node.
  </Step>

  <Step title="Mapping Opportunity fields">
    Click on `+ Map Fields` under Input Parameters, add key name as `deals` and in value we will provide the response received through Salesforce node which will be restructured.

    In `Value`, select **Nodes** tab under Insert Variable and click on `+` of the Salesforce Node.

    <img height="200" src="https://mintcdn.com/cobalt-55/C3P12YsraPVmMY5N/images/Examples/salesforce/map_salesforce.png?fit=max&auto=format&n=C3P12YsraPVmMY5N&q=85&s=c790dbf9166b578236247f6d745ba22c" alt="Mapping API Response for workflow" data-path="images/Examples/salesforce/map_salesforce.png" />

    You can now do mapping using JavaScript code. Try the sample code provided below:

    ```
    function yourFunction(params) {
    const res = params.deals.map(ele=>{
          const output =  {
              id: ele.Id,
              title: ele.Name,
              description: ele.Description || 'Description not available',
              close_date: ele.CloseDate,
              amount: ele.Amount,
              expected_revenue: ele.ExpectedRevenue,
              status: ele.StageName

          }
          return output
      })
      return {"result": res || [] }
    }
    ```
  </Step>

  <Step title="API Proxy in Workflow">
    You have successfully fetched all opportunities and structured the payload. Now to receive it in your server, you need to configure an API Proxy.

    In Refold Dashboard, navigate to `Developer` > `API Proxies` and click on `New Action`. Configure an API endpoint, where you want to receive the response.

    If you want to test, go to [webhook.site](https://webhook.site/) and copy `Your unique URL` and configure this as a POST Request and `Save`.

    <img height="200" src="https://mintcdn.com/cobalt-55/h1VL7v_8HaANGLZ5/images/Examples/salesforce/proxy_salesforce.png?fit=max&auto=format&n=h1VL7v_8HaANGLZ5&q=85&s=a685febbf54d4054ef47f807f3ab4bfe" alt="Configuring API Proxy workflow" data-path="images/Examples/salesforce/proxy_salesforce.png" />

    <Tip>Learn more about API Proxies [here](https://docs.gocobalt.io/build/basics/proxies).</Tip>
  </Step>

  <Step title="Use API Proxy in Workflow">
    From Native Apps nodes, add your org's Node and connect it with Custom Code.

    Click on the node and select the API Proxy that you created from the Actions. Add the response from Custom Code node in data field and click on `Save`.

    <Note>
      Ensure that you add a field in the API Proxy where you can pass the JSON data and then added it to the API Call body as well in the POST request.
    </Note>

    <img height="200" src="https://mintcdn.com/cobalt-55/h1VL7v_8HaANGLZ5/images/Examples/salesforce/proxy_workflow.png?fit=max&auto=format&n=h1VL7v_8HaANGLZ5&q=85&s=c8179720627effe0c68a46384aa15918" alt="Configuring API Proxy in the workflow" data-path="images/Examples/salesforce/proxy_workflow.png" />
  </Step>
</Steps>

## Test Workflow

Once your workflow is built, you can perform both node level and workflow level testing to check it.

Before testing a workflow, ensure the following pre-requisites are completed:

* Linked Account configured with authentication completed with the integration.
* Sample Payload for testing is available & configured.

<Tip>
  Learn in depth how you can configure the pre-requisites in our Workflow Testing guide [here](https://docs.gocobalt.io/build/workflow/workflow_testing#pre-requisites-for-workflow-testing).
</Tip>

Based on the type of testing that you want to perform, follow the steps below:

#### Testing Node

<Steps>
  <Step title="Select Node">
    Click on the node you want to test.
  </Step>

  <Step title="Run Node">
    In the node, switch to `Input/Output` tab and click on `Run Node` button.
    <Warning>Testing for Group Node and Pagination is currently not supported.</Warning>

    <img height="200" src="https://mintcdn.com/cobalt-55/rCTCKBbhiwGPdeWb/images/implementation/node_output_sync.png?fit=max&auto=format&n=rCTCKBbhiwGPdeWb&q=85&s=400c3cb930fd843cb5c1539c732e6f48" alt="Checking Input and Output of Test execution" data-path="images/implementation/node_output_sync.png" />
  </Step>
</Steps>

<Check>Once the node executes successfully, a checkmark appears on the node, indicating it has been tested.</Check>

#### Testing Entire Workflow

Open the testing modal present at the bottom.

Click on `Run Workflow` button to perform a test execution and a **Test Run** log is generated with output of each node.
<Warning>Since testing for Group nodes is not supported currently, you might not receive the logs of the group node.</Warning>

<img height="200" src="https://mintcdn.com/cobalt-55/rCTCKBbhiwGPdeWb/images/implementation/workflow_output_sync.png?fit=max&auto=format&n=rCTCKBbhiwGPdeWb&q=85&s=256c9b54adf117e9508d79438dcc843f" alt="Checking Output of Workflow Test run" data-path="images/implementation/workflow_output_sync.png" />

Once you are satisfied with the testing results, you can close the testing modal and move to the next steps for implementation.

<Check>
  Hurray!!

  You have successfully created and tested a Salesforce workflow to sync all the opportunities to your system.
</Check>
