function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John BrumbelowJohn Brumbelow 

HOWTO run a parameterized search with REST api.

Please help,

We are trying to use the ParameterizedSearch REST-api [ /services/data/v36.0/parameterizedSearch/ ].
We are getting a response of 400 (Bad Request) despite our efforts.
Can someone please inspect the code sample below, and point out what we are doing wrong.
Do note with the samepl code below, one must provide the Instance-Url and Access-Token from a valid login.

=================

HttpWebResponse hwrResponse = null;
StreamReader srResponse = null;

try
{
  string ParamSearchUrlExt = "/services/data/v36.0/parameterizedSearch/"; // <== I have tried 37 as well.

  string csUrl =
      " ~~~ InstanceUrl from Login ~~~ " +
      ParamSearchUrlExt +
      "";

  HttpWebRequest hwrRequest = WebRequest.Create(csUrl) as HttpWebRequest;
  hwrRequest.Method = "POST";
  hwrRequest.Headers.Add("Authorization", "Bearer " + " ~~ AccessToken from Login ~~ ");
  hwrRequest.ContentType = "application/json";
  hwrRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

  string csSearch =
  @"{
      ""q"": ""MySfdcObject__c"",
      ""defaultLimit"": ""100"",
      ""fields"":  [""Id"", ""Name"", ""SomeField__c"", ""SomeOtherField__c"", ""Attachments""],
      ""in"": ""ALL"",
      ""overallLimit"": ""100"",
      ""sobjects"":
      [
        {""name"": ""Name"", ""orderBy"": ""Name ASC NULLS_LAST"", ""where"": ""Name LIKE 'Test%'""},
        {""fields"": [""Id"", ""Name"", ""BodyLength"", ""Description"", ""ContentType""], ""name"": ""Attachments""}
      ]
  }";

  byte[] zbSearch = UTF8Encoding.UTF8.GetBytes(csSearch.ToString());
  hwrRequest.ContentLength = zbSearch.Length;

  using (Stream strSOQL = hwrRequest.GetRequestStream())
  {
    strSOQL.Write(zbSearch, 0, zbSearch.Length);
  }

  hwrResponse = hwrRequest.GetResponse() as HttpWebResponse; // <== fails... Returns 400...

}
catch (Exception e)
{
  // Hanld error...
}
finally
{
  if (!srResponse.IsNull())
  {
    ML.Ignore(() => srResponse.Close());
    ML.Ignore(() => srResponse.Dispose());
    srResponse = null;
  }

  if (!hwrResponse.IsNull())
  {
    ML.Ignore(() => hwrResponse.Close());
    ML.Ignore(() => hwrResponse.Dispose());
    hwrResponse = null;
  }
}
 
Raj VakatiRaj Vakati
Its must be like this 
 
string csSearch =
"{
 "q":"Acme",
  "fields":["Id", "Name", "Phone"],
  "sobjects":[{"name": "MySfdcObject__c"}  
 }"
   
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_search_parameterized.htm
 
John BrumbelowJohn Brumbelow
Thanks for the reply, but what is the "q" then?
How does one apply the order-by I tried, or a where clause witha LIKE statement?
 
John BrumbelowJohn Brumbelow

I tried 

"q": "Acme", and get past the 400, but the response is an empty arracy ==> "[]".

John BrumbelowJohn Brumbelow
I tried "q": "Test" and it worked.
The documentation is severly flawed. It does not explain what "q" is other than to say "A search string that is properly URL-encoded", even though I'm using the POST approach.
 
John BrumbelowJohn Brumbelow
I've tried other values for "q". Only "Test" and "TEST" work. I've tried "Acme", and other small names. Only "Test" works.
At this point, I'm classifying this as a BUG in the documentation. It is obviously meant to match some value setup in SFDC, but the documentation does not indicate anything, and I'm classifying this as a BUG as the return is empty is "q" is not "Test".

Please provide full documentation on this "q" and how it is supposed to be set.