Sync Salesforce instances when records are deleted

This tutorial explores the delete action that's available with IBM® App Connect by using a delete operation use case for Salesforce.

Note: Note: This tutorial assumes that you have free or trial accounts for Salesforce, or that you have business accounts. For more information, see How to use IBM App Connect with Salesforce.

Scenario

Suppose that you have multiple Salesforce instances that you want to synchronize by using two flows:
  • The first flow synchronizes new customer Account objects between two Salesforce instances.
  • The second flow retrieves deleted customer Account objects from the first Salesforce instance so it can delete the corresponding objects from the second Salesforce instance.

The flows synchronize two Salesforce instances, SFAccount1 and SFAccount2.

Find or create what you need

  1. The name and password of your two Salesforce accounts. To create a free test account in Salesforce rather than use your business account, register for a Developer account from this URL https://developer.salesforce.com rather than a Trial account. If you connect to App Connect with a Trial account, the Salesforce events don't work.

    For more information, see How to use IBM App Connect with Salesforce.

  2. Create the custom field ExternalId in both Salesforce instances.

    The example flows use a Salesforce custom field that is called ExternalId to link the Account objects in the Salesforce instances.

    For more information, see Create Custom Fields in the Salesforce documentation.

Create your flows

Use App Connect to create the flows from scratch.

Complete the following steps in App Connect.

  1. Create the first flow. This flow is to be triggered when a new account is added to the SFAccount1 Salesforce instance. The flow then creates a new account object with the same properties in the SFAccount2 Salesforce instance.
    1. From the Designer dashboard, click Create > Event-driven flow (or New > Event-driven flow in earlier versions).

      Complete the following steps. As you progress, App Connect automatically saves your changes. If you move away from the flow at any stage, the flow is saved as a draft flow that you can complete at another time.

    2. Enter a name that identifies the purpose of your flow; for example:
      Sync new account in SFAccount1 to SFAccount2
    3. Select Salesforce > New account as the event that is to trigger the flow. Select the connection account for the SFAccount1 Salesforce instance.
    4. Click the (+) and then scroll down to Saleforce.
    5. Select Create account as the action to take. Select the connection account for the SFAccount2 Salesforce instance.
    Figure 1. Configuring the Salesforce Create account action
    Configuring the Salesforce / Create account action

    (Click image to view full size.)

  2. Create the second flow. This flow is to delete the Account objects in the SFAccount2 instance that were deleted from the SFAccount1 instance since this flow last ran.
    1. From the Dashboard, click Create > Event-driven flow (or New > Event-driven flow in earlier versions).
    2. Enter a name that identifies the purpose of your flow; for example:
      Sync deleted accounts in SFAccount1 to SFAccount2
    3. Add a Scheduler node as the event trigger, and configure it to repeat every 1 minute.
      Figure 2. Configuring the scheduler node as the event trigger of flow 2
      Configuring the scheduler node as the event trigger
    4. Add a 'Set variable' node (from the Toolbox) for a defaultTimecheck variable.

      You want to delete the Account objects in the SFAccount2 instance that were deleted from the SFAccount1 instance since the flow last ran. To help delete these objects, add a Set Variable node. Define a defaultTimecheck variable that takes the value of the Previous event time field from the Scheduler and converts it to milliseconds.

      Use this variable as a checking condition on further actions. The actions that follow must run only if the value of defaultTimeCheck is greater than zero. During its first operation, the Scheduler always returns the Previous event time as equal to the Epoch time, which is equal to zero.
      Figure 3. Configuring the Set variable node
      Configuring the Set variable node
    5. Add an If node (from the Toolbox) to check that the value of defaultTimeCheck is greater than zero. If this condition is met, the flow continues.
    6. In the first branch, click the (+) and then add a Salesforce Retrieve accounts action for the SFAccount1 instance.
    7. Configure the Retrieve accounts action to retrieve the accounts from the SFAccount1 instance that were deleted. Add two "Where" conditions for this retrieve action. The Last Modified Date property must be greater than the Previous event time (which is mapped from the Scheduler node) and the Deleted property must be true.

      Set 'Maximum number of items to retrieve' to 1.

      Figure 4. Configuring the Salesforce Retrieve accounts action for SFAccount1 instance
      Configuring the Salesforce / Retrieve accounts action for SFAccount1 instance
    8. Add a For each node (from the Toolbox) to process each of the retrieved Account objects. You can run the For each node serially or in parallel to process the items.
    9. Add a Salesforce Retrieve accounts action for the SFAccount2 instance to retrieve the linked Account object in the SFAccount2 instance by using the ExternalId field reference.

      Set Maximum number of items to retrieve to 1.

    10. Figure 5. Configuring the Salesforce Retrieve accounts action for SFAccount2 instance
      Configuring the Salesforce / Retrieve accounts action
    11. Click the next (+) and add a Salesforce Retrieve accounts action for the SFAccount2 instance. This action deletes any Account object from the SFAccount2 instance if that Account was deleted from the SFAccount1 instance since the flow last ran.

      For Salesforce, App Connect provides a "delete by ID" condition that you can use to locate any SFAccount2 Account ID that matches the SFAccount1 Account IDs that the previous Retrieve accounts action returned.

      Figure 6. Saleforce Delete account action in App Connect
      Saleforce / Delete account action in App Connect

Test your flow

  1. Start flow 1, and then in the SFAccount1 Salesforce instance create an Account object. Flow 1 creates an Account object with the same properties in the SFAccount2 instance.
  2. Start flow 2. The Scheduler node triggers the flow every minute, but doesn't complete actions until an Account object is deleted from the SFAccount1 Salesforce instance.
  3. In the SFAccount1 Salesforce instance, delete the account that was created in step 1.

    The Scheduler node triggers flow 2, and then deletes the corresponding Account object in the SFAccount2 instance.

    Figure 7. Salesforce message for deletion of a record
    Salesforce message for deletion of a record

Conclusion

You created a flow that adds a lead to Insightly and a subscriber to a MailChimp list whenever someone registers to attend your company's expo.