Salesforce SOAP API: POST Request with SOQL Query Envelope [Custom SObjects]
Looking for Salesforce Training & HandsOn Projects?
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
Step-1: Launch the Object Manager and add a custom object called “Invoice__c”
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.
Step-6: Similarly we can see the customer information saved
The Request Object
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
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-
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
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”
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
Analyze Response Object
Step-16: If we analyze the response again, we can now see the Customer Name coming in instead of Customer Id.
Comments
Post a Comment