Salesforce SOAP API - POST Request With SOQL Query Envelope [Custom SObjects]

This article is in continuation of my Previous Article on executing SOQL Queries using SOAP API Requests.

In this article, we will discuss the detailed steps on executing SOQL queries for Custom Salesforce SObjects using SOAP API. In this demo, we will look into how to query fields with lookup relationships. This article holds good both for Custom and Standard SObjects.
 

Analyze Metadata & Data


To start with a demo lets’ add a custom object to the Org,

Step 1
 
Launch the Object Manager and add a custom object called “Invoice__c”
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 
Step 2
 
We have added a couple of fields to this object, especially focusing on a lookup field called “Customer__c” which is a lookup field for the “Account” object.
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 
Step 3
 
Add a new record to the “Invoice” object, choose a value from the “Account” lookup for the “Customer” field. Also, fill the rest of the fields as per need

Step 4
 
Click “Save” to save the record
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 
Step 5
 
After saving the record successfully, we can review the record and can see a new Invoice number added

Step 6
 
Similarly we can see the customer information saved
 

The Request Object


Now build the Request Object (For details refer to my Previous Article on this)
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 
Step 7
 
Since this is going to be the Post request, select “POST” as the request type

Step 8
 
Specify the SOAP Endpoint submit this request to. Select “Body”/”raw”/”XML” options to specify that the body of this request contains XML data

Step 9
 
Get the updated Session-Id and update within the request envelope

Step 10
 
Specify any valid SOQL query as a query string to this request envelope. It is important to note that in this query we are querying

“Customer” lookup field (Customer__c) to see how it works with SOAP API

Step 11
 
Click “Send” to submit the request
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 

The Response Object

 
Step 12
 
If we closely analyze the Response Object we can see it contains only Customer Id and does not contain the actual name of the customer.
 
This outcome is not user-friendly and makes not much sense if we present it to the end-users, probably they won’t be able to understand which Customer we are talking about. We can easily fix this issue by making some changes to the SOQL query as shown below,
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 
Step 13
 
Before we make any changes to the SOQL query, we need to find out what should look for. Since we are querying the Account lookup, we need to analyze the “Account” Object in Salesforce Org
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 
Step 14
 
Since we need to show the name of the customer which is going to be the “Account Name” Field of “Account”. In SOQL queries we need to use the field names and not the field labels. So we will use “Name” for the field label “Account Name”
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 

Modify Request Object

 
Step 15
 
Now based on information we have in Step-13 & Step-14, we change the query a little bit (from Customer__C to Customer__r.Name).

Customer__c represents the custom look column in the object definition, while Customer__r represents the relationship object instance of “Account” object itself which gives us the power to refer the Parent Object (Account) fields directly from within the SOQL query for the Child Object (Invoice)
  • Click “Send” to submit the request
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 

Analyze Response Object

 
Step 16
 
If we analyze the response again, we can now see the Customer Name coming in instead of Customer Id.
 
Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
 

Conclusion

 
POST Requests with SOQL Query Envelopes is a very powerful implementation offered by Salesforce SOAP APIs. It is recommended not to use very complex queries as that might increase the processing time and might lead to request timeout issues with SOAP API Endpoint. So be intelligent which choosing your tools to deal with the specific problem statement.

Hope you enjoyed this article. Please leave your comments to let me know how you do like the content and how you do find it helpful to learn the topic.