Cache Scope with Salesforce Contacts

(2 reviews)


Caching is the temporary storing a result of some operations, in this case, caching fetched data from Salesforce. In this example, when we ask for the data again, results are returned from cache instead of making a new call to Salesforce.



This document describes the details of the example within the context of Anypoint™ Studio, Mule's graphical user interface (GUI). Where appropriate, the XML configuration is provided.

This document assumes that you are familiar with Mule Runtime and the Anypoint Studio interface. Further, this example assumes that you have a basic understanding of Mule flows and Mule Global Elements.

Example Use Case

In this example, the Salesforce querying operation is encapsulated within the cache scope component:

  1. The query to Salesforce is performed to fetch Contacts.
  2. Additional calls to Salesforce are avoided and data is returned from cache.
  3. The result is printed.

Check the Anypoint Studio console while running the example to see when the cache is used.

Set Up and Run the Example

Complete the following procedure to create, then run this example in your own instance of Anypoint Studio. Skip to the next section if you prefer to simply review this example.

  1. Open the Example project in Anypoint Studio from Anypoint Exchange. Don't run the application.
  2. Log in to your Salesforce account. From your account menu (your account is labeled with your name), select Setup.
  3. In the left navigation bar, under the Personal Setup heading, click to expand the My Personal Information folder.
  4. Click Reset My Security Token. Salesforce resets the token and emails you the new one.
  5. Access the email that Salesforce sent and copy the new token onto your local clipboard.
  6. In your application in Studio, click the Global Elements tab.
  7. Double-click the Salesforce global element to open its Global Element Properties panel. In the Security Token field, paste the new Salesforce token you copied from the email. Alternatively, configure the global element in the XML Editor.
  8. Change the contents of the Username and Password fields to your account-specific values, then click OK to save your changes.
  9. In the Package Explorer, right-click the connect-with-salesforce project name, then select Run As > Mule Application. Studio runs the application on the embedded server.
  10. In your browser, access http://localhost:8081/ and the Contacts are displayed in browser in JSON format.
  11. Go back to Anypoint Studio and click the console view and search for a log similar to this one:INFO 2018-02-01 14:44:56,781 [[MuleRuntime].cpuLight.02: [cache-scope-with-salesforce-contacts].cache-scope-with-salesforce-contactsFlow.CPU_LITE @7f8feb5] [event: 0-16100290-0756-11e8-b986-f85971bd4f61] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Querying Salesforce (not from Cache)
  12. Switch to your browser and access http://localhost:8081/ again.
  13. In Anypoint Studio console, search the logs to verify that caching is performed. The log message stating that the call Querying Salesforce (not from Cache) was performed is not in the console logs.
  14. The Contacts are displayed in JSON format in the browser and returned from the cache.
  15. Stop the Mule application by clicking the square, red terminate button in the Console.

How it Works

Using a single flow, this application queries Salesforce CRM for contacts and prints them in a browser. From the console log you can check when caching is performed. Feel free to experiment more with this example.

Salesforce Connector performs a query in your Salesforce Account: to get Id, FirstName, LastName, Email, and Phone attributes of Contact objects with a limit set to 200 records. To keep the example simple, the caching is illustrated via logs in console and Contacts are displayed in JSON format in a browser.


Published by
MuleSoft Organization
Published onSep 6, 2018
Asset overview

Asset versions for 2.1.x

Asset versions


ContactNo values left to add
SalesforceNo values left to add
SalesMarketingNo values left to add