Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I'm trying to retrieve an Id from Salesforce over the REST API using SOSL.

My query which I can execute without issue in the Developer Console is:

FIND '(415)901-7000' IN PHONE FIELDS RETURNING Account(Id)

This is the phone number of the sForce Account which is present in my Developer Org.

I am calling the REST Api from C# and this is my encoded Uri:

/services/data/v20.0/search/?q=FIND+%27(415)901-7000%27+IN+PHONE+FIELDS+RETURNING+Account(Id)

However, when I send the request I get this in the response:

{
    "message": "No search term found. The search term must be enclosed in braces.",
    "errorCode": "MALFORMED_SEARCH"
}

Attempting to address this with:

FIND {(415)901-7000} IN PHONE FIELDS RETURNING Account(Id)

I send:

/services/data/v20.0/search/?q=FIND+%7B(415)901-7000%7D+IN+PHONE+FIELDS+RETURNING+Account(Id)

But I simply get the following in the response:

{
   "message": "",
   "errorCode": "MALFORMED_SEARCH"
}

What is the correct way to send this SOSL search via the REST API?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

You are using (, ) and - in your search term, all of which are reserved characters in SOSL.

From the documentation:

The following characters are reserved:
? & | ! { } [ ] ( ) ^ ~ * : \ " ' + -

Reserved characters, if specified in a text search, must be escaped (preceded by the backslash \ character) in order to be properly interpreted. An error occurs if you do not precede reserved characters with a backslash. This is true even if the SearchQuery is enclosed in double quotes.

You need to escape your query and surround it in braces rather than single quotes:

FIND {\(415\)901\-7000} IN PHONE FIELDS RETURNING Account(Id)

So the encoded query is:

FIND%20%7B%5C(415%5C)901%5C-7000%7D%20IN%20PHONE%20FIELDS%20RETURNING%20Account(Id)

EDIT:

To get the results you want to may also need to surround your search in double quotes, in which case it becomes:

FIND {"\(415\)901\-7000"} IN PHONE FIELDS RETURNING Account(Id)

With the encoded query:

FIND%20%7B%22%5C(415%5C)901%5C-7000%22%7D%20IN%20PHONE%20FIELDS%20RETURNING%20Account(Id)

EDIT2:

Although I've just noticed that this is one of the numbers for the default Dev Org accounts, so if you're going down the double quote route you need a space after the bracketed part of the phone number to match it.

FIND {"\(415\) 901\-7000"} IN PHONE FIELDS RETURNING Account(Id)

Encoded:

FIND%20%7B%22%5C(415%5C)%20901%5C-7000%22%7D%20IN%20PHONE%20FIELDS%20RETURNING%20Account(Id)
share|improve this answer
    
Thanks, I'd neglected to consider the '-' since it worked in the developer console enclosed in quotes. –  Grokodile Jun 4 at 10:22

When I tried the same query with the developer console the dash in the phone number caused the invalid message for me.

With the dash removed I captured the following HTTP GET Reqeust from the Chrome developer tools:

https://na5.salesforce.com/services/data/v30.0/search/?q=FIND%20%7B(415)%20901%207000%7D%20IN%20PHONE%20FIELDS%20RETURNING%20Account(Id)

From the Docs:

Reserved Characters
The following characters are reserved:
? & | ! { } [ ] ( ) ^ ~ * : \ " ' + -

Reserved characters, if specified in a text search, must be escaped (preceded by the backslash \ character) in order to be properly interpreted. An error occurs if you do not precede reserved characters with a backslash. This is true even if the SearchQuery is enclosed in double quotes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.