1

select Id, Name from Contact where Email in ('[email protected]','[email protected]')

Can anyone help me to query this using the salesforce query api endpoint.

The endpoint that I have tried

https://{myWorkplaceDomainUsed}--sandbox.cs67.my.salesforce.com/services/data/v42.0/query/?q=select+Id+from+Contact+where+Email=%[email protected]%27

and the response that I get is:

{
    "totalSize": 0,
    "done": true,
    "records": []
}

Records are present in salesforce but the result is not displayed.

I even tried escaping query string with "\". The URL that I used is:

https://{myWorkplaceDomainUsed}--sandbox.cs67.my.salesforce.com/services/data/v42.0/query/?q=select+Id+from+Contact+where+Email=\'[email protected]\'

and the malformed query is the resulting respone.

What will be the correct way to query my SOQL using "IN" clause?

Thanks in advance

2
  • 1
    ytiq has provided the answer, but workbench is always available for you to try to figure out the right format as well and quickly test. Utilities --> REST Explorer Commented Dec 31, 2019 at 13:14
  • update response with code for 'IN' and also added escaping for '+'
    – ytiq
    Commented Jan 2, 2020 at 7:52

1 Answer 1

1

You should put ' ', but you don't need to escape them try that one https://{myWorkplaceDomainUsed}--sandbox.cs67.my.salesforce.com/services/data/v42.0/query/?q=select+Id+from+Contact+where+Email='[email protected]'.

If you want IN you can use it like this https://{myWorkplaceDomainUsed}--sandbox.cs67.my.salesforce.com/services/data/v42.0/query/?q=select+Id+from+Contact+where+Email+IN+('[email protected]', '[email protected]')

UPDATE: Code for IN close with escaping for '+' emails

List<String> emails = new List<String>();
// init emails;

String emailString = '(\'' + String.join(emails, '\', \'') + '\''.replaceAll('\\+', '\\\\+');

String query = '... Email IN ' + emailString;
4
  • Ytiq, The IN clause query works fine when I don't have any email with the "+" character sign on it. When there is the "+" character it simply returns no record at all. Looks like this thinks "+" as space. As I think the single quote ( ' ' ) should avoid the inside quote character as string right? Could you please help me on querying an email with "+" sign in it. Thank you! Commented Jan 1, 2020 at 4:14
  • Also My Email address will be a dynamic variable. Commented Jan 1, 2020 at 4:27
  • Then uriEncode +, thought maybe it's a space. Didn't know that email can have +
    – ytiq
    Commented Jan 1, 2020 at 6:32
  • Just add your variable as a string `'Select... Email=\''+variable+'\'...'
    – ytiq
    Commented Jan 1, 2020 at 6:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .