LinkedIn

Salesforce REST API - Usage of Search and parameterized Search

There can be scenarios where an external system would like to perform a search in salesforce with multiple objects. Recently we got a requirement to search for memberid in both Lead and opportunity and return resulted records. You can execute this in two ways.

1. Execute as 2 separate API SOQL query execution - one for Lead and one for Opportunity. 

2. Execute Search through REST API

The problem with the first approach is that you have to execute it as 2 separate transactions and it is time-consuming. To avoid that we can execute this using REST API search mechanism also. 

Search can be executed through REST using 2 approaches:

1. SOSL based Search

2. Parameterized Search

Let us see both in detail.

1. SOSL Based REST API

You can pass URL encoded text in SOSL query as a parameter and this will return details that match the search criteria.

Please find the details of executing SOSL through REST below:


Let us see how we can execute the above-mentioned search scenario.

I am using workbench REST explore to execute this.

The SOSL query, which I would like to execute is mentioned as below:

FIND {M12345} RETURNING lead(id,name, Member_id__c), Opportunity(id,name, stagename,Member_id__c)

You can use some URLencode tool to encode the above and it will give you below encoded text

FIND%20%7BM12345%7D%20RETURNING%20lead%28id%2Cname%2C%20Member_id__C%29%2C%20Opportunity%28id%2Cname%2C%20stagename%2CMember_id__c%29

And in REST Explorer, you can execute it using below stateement:

/services/data/v49.0/search/?q=FIND%20%7BM12345%7D%20RETURNING%20lead%28id%2Cname%2C%20Member_id__C%29%2C%20Opportunity%28id%2Cname%2C%20stagename%2CMember_id__c%29

Let us see how we can execute the same using Parametrized Search

2. REST API - Parameterized Search

Executes a simple RESTful search using parameters instead of a SOSL clause. Indicate parameters in a URL in the GET method. Or, use POST for more complex JSON searches.

Please find the details of executing parameterized Search through REST below:


Let us see how we can execute the above search using parameterized Search:

/services/data/v49.0/parameterizedSearch/?q=M12345&sobject=Lead&Lead.fields=id,name,Member_Id__C&Lead.limit=10&sobject=Opportunity&Opportunity.fields=id,name,stagename,Member_Id__C&Opportunity.limit=15


You can see above that we can also specify Limit per Object also.

If you would like to get metadata details of the object also, you can modify the search like mentioned below:

/services/data/v49.0/parameterizedSearch/?q=M12345&metadata=LABELS&sobject=Lead&Lead.fields=id,name,Member_Id__C&Lead.limit=10&sobject=Opportunity&Opportunity.fields=id,name,stagename,Member_Id__C&Opportunity.limit=15

And the response looks like below with field metadata details:


You will be able to execute POST approach also:

{

  "q":"M12345",

  "fields" : ["id", "name","Member_Id__c"],

  "sobjects":[{"fields":["id","Name", "stagename","Member_Id__c"],

        "name": "Opportunity", 

        "limit":20},

       {"name": "Lead"}],

  "in":"ALL"

}

Response:


Below is a comparison between Search and parameterized Search approach


References:

https://developer.salesforce.com/docs/atlas.en-us.224.0.api_rest.meta/api_rest/resources_search_parameterized.htm#resources_search_parameterized

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_sosl.htm

https://developer.salesforce.com/docs/atlas.en-us.224.0.api_rest.meta/api_rest/resources_search.htm

Comments

Post a Comment

Popular posts from this blog

Subscribing to Salesforce Platform Events using External Java Client - CometD

Salesforce Security - Restriction Rules and Scoping Rules

How to develop reusable Invocable Apex methods for Flows