Sweet & Simple: Using SOQL Relationship Queries with Salesforce.com Connector


The best things in life are often sweet and simple. However, “S & S” is an easy concept to understand and appreciate but often hard to implement. For example, a sweet and simple way to attract traffic to our blog would be to show women in bikinis playing with cats. In reality that is rather hard to pull off for a technical site. There simply is no budget to publish anything like “API Illustrated, Swimsuit Edition” or “ESBN, the Body Issue”. Instead, this article will focus on sweet and simple features in our products that can make life easier for integration developers.

With 100,000+ customers, Salesforce.com is one of the most popular integration endpoints for ESB implementations.  There are a couple of commonly asked questions when it comes to Salesforce.com: how do you reduce the number of API calls since there are daily limits per instance, and how do you retrieve all the related records in one query?  The SOQL Relationship Queries help accomplish both goals, as a developer can make just one API call against different SObject types that are also related.

Here we will illustrate how the Anypoint Salesforce.com Connector fully supports the .  We will use the archetypal Header-Line hierarchy data structure, for example, Opportunity and Opportunity Line Items.  This represents the classic use case in which an Order (i.e. opportunity) can contain multiple products (i.e. opportunity line items).  To use a relationship query, simply put the nested statement into the usual Query Text box: 

The relationship query used in this example is show below.  Note that the where clause here is applicable to the opportunity sobject, not opportunity line items:
SELECT Id, Name, Account.Name, (SELECT Quantity, UnitPrice, TotalPrice, PricebookEntry.Name, PricebookEntry.Product2.Family FROM OpportunityLineItems) FROM Opportunity WHERE LastModifiedDate >= #[flowVars[‘watermark’]]

Then to parse the results returned by relationship queries, note that the payload data will now have hierarchy, more than one level of element mapping inside the Data Mapper will be needed. For the example above, within the same Data Mapper transformer, there are two levels.  First, there is one level for the opportunity itself: 
Then another level for all the related opportunity line items: 

Here, JSON output used here strictly for demonstrative purpose. Any output types supported by the Data Mapper can be used. And that is everything you need to perform relationship SOQL queries. If you don’t find the capability to handle complex data structure with one simple query and transformation sweet, well then, there is always the Internet:

We'd love to hear your opinion on this post

8 Responses to “Sweet & Simple: Using SOQL Relationship Queries with Salesforce.com Connector”

  1. This doesn’t seem to be working for me. I don’t see the related object in the datamapper. Can you please upload a quick code sample?


    • Forgot to mention, you cannot use DataSense for related query. You must construct the query manually (but you can use SFDC workbench to help you with that.)

  2. Thanks, Adrian. After writing the SOQL query, I drag the datamapper control into my flow. Should the datamapper control be intelligent enough to automatically pick up the related fields from the payload? Or do I need to manually configure the datamapper?

    • Ah, that part I don’t remember the details. How many fields you have? If data sense not enabled, it may not be able to pick up automatically but you can always manually construct the schema in Data Mapper via the UI.

  3. Hi Adrian,

    Datasense is enabled in the SFDC Connector. I tried a simple query like this: “Select Id, Name, (Select Id, Name From Contacts) From Account”.

    However, the payload is automatically set to List and the only fields I see in the Datamapper are all of the Account fields.

    Do you think I need to manually construct the schema in the DataMapper so I can see the related object?

    • Hi EK, if you want to use Related Queries, then don’t use datasense and yes, construct the schema manually.

  4. Hi, just a question. If it is known Salesforce API doesn’t retrieve all dependent records from the child object (Max. 1,000), how does Mulesoft processes the queryMore action over the child object list?

    Is there any Mulesoft object or component that could handle this querylocator management natively over the child object records at the subquery?

    • Hi Wilmer, thank you for your question. The connector does paging under the covers. So, as you iterate over the response, the connector will automatically and transparently query the next page or results. You can learn more here If you have any other questions, feel free to ask them on the MuleSoft Forums!