3

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?

2 Answers 2

5

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)
1
  • Thanks, I'd neglected to consider the '-' since it worked in the developer console enclosed in quotes.
    – Grokodile
    Jun 4, 2014 at 10:22
1

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.

You must log in to answer this question.

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