This article is in continuation of my earlier one How to Develop Custom Salesforce SOAP API Endpoint – I
In this article, we will look for the Service Operations which return records by Indexers (Unique Ids) as part of Response.
Operation 2: Returns a Single Record By Indexer
As we can see the “getInvoicesByAmount” is bound to return a collection of all the matching records meeting the filter criteria, now we add another method to the Service Class which always returns a single record based on the filter criteria.
Such operations are always helpful to handle scenarios like “Get Something By ID, Get Something By Key”, to pinpoint the specific set of information.
For example in the upcoming demo, we will develop an operation that will be able to return the Invoice Information based on Invoice Number. In real-life scenarios, it is a very common implementation that every endpoint should implement one way or the other.
Lets’ go back to the “InvoiceService” class in Visual Studio Code-
Step-23: Add another operation “getInvoiceByNumber” which takes an input parameter “invoiceNumber” representing a unique identifier for Invoices
Step-24: We have a variable of type “Invoice__c” that will hold the invoice information returned by the filtered SOQL query. Consumer Applications will pass on this filter value while invoking this endpoint
Step-25: This information will be returned to the consumer applications as part of the response
That is it for the code. Now deploy the updated version of the service as we did in Step-8
Invoke Custom SOAP API Endpoint using Postman
Launch Postman and start with new API Request.
The Request Object
Step-26: Since we have a new operation in place so we need to update the SOAP Envelope to point to the right operation of choice. Here is the new SOAP envelope for the new operation:
This envelope uses the same convention as explained in Step-15
Here we are passing “INV-000000006” as the value to “invoiceNumber” parameter, which means we want to retrieve the information of Invoice with the matching invoice number
Step-27: Click on the “Send” button to submit the HTTP request with the updated request body
The Response Object
Step-28: Now it is time to analyze the Response Object returned by SOAP API Endpoint as a result of the request we made after updating the request body
This time we will see exactly 1 invoice coming in as part of the response matching the filter criteria as mention in our request body
Salesforce SOAP API Endpoint Limitations
You can have multiple operations in the Service class but consumer applications can only request data using “One Operations at a Time”, which means request body cannot have the call to multiple operations with in the same request.
So lets’ modify the body request and try to include both operations together in single request-
Step-29: Include both the operations to the request body
Step-30: Click “Send” to submit the request
Step-31: Now if we analyze the Respond Object, we would see an error coming up as the response body, explaining that “Only one operation within the same request is allowed”
So it is important to plan your SOAP API based solution with consideration to this known limitation.
Custom API Endpoints empowers to respond to the compelling business needs efficiently. It allows us to deal with requirements involving complex business logic without exposing the complexities to consumer applications.
All the logic running inside API Endpoints is by default executing under System Context (highest privileges), so it is the responsibility of the API developers to make sure proper security restrictions are implemented as required.
It is also worth pointing to understand and plan the enterprise solution based on the API Limits offered by Salesforce Org. Here is the Salesforce Cheat Sheet for understanding API Limits. It is recommended to plan your solutions in line with permissible API Limits for your Salesforce Org.
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.