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.
Scenario
- 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
- 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.
- 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.
- 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 theSFAccount2
Salesforce instance.- From the Designer dashboard, click (or 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.
- Enter a name that identifies the purpose of your flow; for example:
Sync new account in SFAccount1 to SFAccount2
- Select as the event that is to trigger the flow. Select the connection account for the SFAccount1 Salesforce instance.
- Click the (+) and then scroll down to Saleforce.
- 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 (Click image to view full size.)
- From the Designer dashboard, click (or in earlier versions).
- 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.
- From the Dashboard, click (or in earlier versions).
- Enter a name that identifies the purpose of your flow; for example:
Sync deleted accounts in SFAccount1 to SFAccount2
- 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 - Add a 'Set variable' node (from the Toolbox) for a defaultTimecheck
variable.
You want to delete the Account objects in the
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.SFAccount2
instance that were deleted from theSFAccount1
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.Figure 3. Configuring the Set variable node - 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.
- In the first branch, click the (+) and then add a Salesforce Retrieve accounts action for the SFAccount1 instance.
- 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 - 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.
- 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.
-
Figure 5. Configuring the Salesforce Retrieve accounts action for SFAccount2 instance - 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
Test your flow
- 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.
- 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.
- 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
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.