You need to sign in to do that
Don't have an account?
John 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;
}
}
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;
}
}
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_search_parameterized.htm
How does one apply the order-by I tried, or a where clause witha LIKE statement?
I tried
"q": "Acme", and get past the 400, but the response is an empty arracy ==> "[]".
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.
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.