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
Keith Stephens 18Keith Stephens 18 

rest api dynamic soql

Hello all,
I am making a rest api call from the developer console that just calls SF AIP /query?q passing in a simple SOQL statment.
salesforce.com/services/data/v40.0/query?q=SELECT+Attny_Phone_From_Contact__c,Attorney_Email_from_Contact__c,Attorney_Email__c,Attorney_Fax__c,Attorney_First_Name__c,Attorney_Full_Name__c,Attorney_Last_Name__c,Id,Plaintiff_First_Name__c,Plaintiff_Full_Name__c,Plaintiff_Last_Name__c+FROM+Case+limit+3';

This works fine, but now I want to add the logged in users email address, everything I have tried is not working.
example String userEmail = UserInfo.getUserEmail();
And adding userEmail in my soql string ect..

How can I get the email to be passed in the query.
Thanks,
Keith.
James LoghryJames Loghry

Hi Keith,

You just need to be careful about properly escaping quotes in the query.

Here's what a valid query request looks like in workbench:
 

/services/data/v41.0/query?q=Select+Id+From+User+Where+Email='myusername@gmail.com'

For your example, you just need to plug in the userEmail field in the correct place and escape the single quotes accordingly.  The string should look like:
'/services/data/v40.0/query?q=SELECT+Attny_Phone_From_Contact__c,Attorney_Email_from_Contact__c,Attorney_Email__c,Attorney_Fax__c,Attorney_First_Name__c,Attorney_Full_Name__c,Attorney_Last_Name__c,Id,Plaintiff_First_Name__c,Plaintiff_Full_Name__c,Plaintiff_Last_Name__c+FROM+Case+WHERE+Attorney_Email_from_Contact__c=\''+userEmail+'\'+limit+3';
James LoghryJames Loghry
The stupid code functionality cut that off, but it should look like: 

'/services/data/v40.0/query?q=SELECT+Attny_Phone_From_Contact__c,Attorney_Email_from_Contact__c,Attorney_Email__c,Attorney_Fax__c,Attorney_First_Name__c,Attorney_Full_Name__c,Attorney_Last_Name__c,Id,Plaintiff_First_Name__c,Plaintiff_Full_Name__c,Plaintiff_Last_Name__c+FROM+Case+WHERE+Attorney_Email_from_Contact__c=\''+userEmail+'\'+limit+3';
Keith Stephens 18Keith Stephens 18
Not exacly what I was looking for, in work bench I get malformed query, and when I execute the api from the developrs console I get unexpected token url.
for readability sake I shorted the query, but this is what I want to do.
query?q=select Attorney_Full_Name,\''+userEmail + '\'+from+case+limit 3';   I do not want the where clause that you added.
Thanks,
Keith.