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
Ayub AnsariAyub Ansari 

Query with where clause in Tooling API not working

Hi,

I am trying to access metadata of Profile by using tooling api, when I request query without adding any filter as given below, I am getting response as expected:

string epoint = 'https://ap1.salesforce.com/services/data/v32.0/tooling/query?q=SELECT+Description,+Name,+Metadata,+FullName+FROM+Profile';

But, as I am adding a filter as below, I am getting error- "Bad Request" Status Code= 400. Please note that this query executes as expected in developer console--> Query Editor, but throw specified error when trying to execute apex code in anonymous block.

String epoint = 'https://ap1.salesforce.com/services/data/v32.0/tooling/query?q=SELECT+Description,+Name,+Metadata,+FullName+FROM+Profile+where+Name=\''+'System Administrator'+'\'';

I have already posted this question on developer forum. Any help would be greatly appreciated.

Thanks,
-Ayub
Best Answer chosen by Ayub Ansari
Ayub AnsariAyub Ansari
Hi Mr. 8-  Thanks!! 

As I told- its working on query editor and workbench as expected. Did you execute ?
String epoint = 'https://ap1.salesforce.com/services/data/v32.0/tooling/query?q=SELECT+Description,+Name,+Metadata,+FullName+FROM+Profile+where+Name=\''+'System+Administrator'+'\';

It will throw exception "string literal not allowed". 

I solved the issue myself- basically issue was very simple related to encoding- while making request- I was passing filter value 'System Administrator' but I shoud pass an encoded value like this- 'System+Administrator'.

Thank you Amit once more!

-Ayub.



 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below query :-
/services/data/v32.0/tooling/query?q=SELECT+Description,+Name,+Metadata,+FullName+FROM+Profile+where+Name='System+Administrator'

I tested the same in workbanch it working fine for me:-

User-added image

Please try below Code:-
String epoint = 'https://ap1.salesforce.com/services/data/v32.0/tooling/query?q=SELECT+Description,+Name,+Metadata,+FullName+FROM+Profile+where+Name=\''+'System+Administrator'+'\'';

Please mark this as solution if this will help you

Thanks
Amit Chaudhary
Ayub AnsariAyub Ansari
Hi Mr. 8-  Thanks!! 

As I told- its working on query editor and workbench as expected. Did you execute ?
String epoint = 'https://ap1.salesforce.com/services/data/v32.0/tooling/query?q=SELECT+Description,+Name,+Metadata,+FullName+FROM+Profile+where+Name=\''+'System+Administrator'+'\';

It will throw exception "string literal not allowed". 

I solved the issue myself- basically issue was very simple related to encoding- while making request- I was passing filter value 'System Administrator' but I shoud pass an encoded value like this- 'System+Administrator'.

Thank you Amit once more!

-Ayub.



 
This was selected as the best answer