I need to retrieve the corresponding ID for a sectors name in our CRM application.
I am using the CRM's API through a get request in JavaScript. I have tried using encodeURI, encodeURIComponent, and escape. The get request works with some sector names but others do not work and return an error.
//The url was masked but the query is the same.
let URL = "http://domain/instance/api/data/v8.2/new_occurrencereportsectors?$select=new_occurrencereportsectorid,new_name&$filter=new_name eq ";
//This part works
let encodedURI = encodeURI(URL);
//This is the string I am trying to pass to the CRM API. This does not work.
let query = "Ontario - Outside the Greenbelt / Ontario - à l'extérieur de la ceinture";
//This is me trying out all the enocdings.
let encodedQuery = encodeURI(query);
encodedQuery = encodeURIComponent(encodedQuery);
encodedQuery = escape(encodedQuery);
//This is the string which I am using for the get request.
let finalString = encodedURI + encodedQuery;
//Note this is an example so I am just putting the printed.
//URL into the search bar in the browser.
console.log(finalString);
I expected the return value to be an ID which will be in the format {XXXXXXXX}.
The output is a syntax error. Please see below for the error message. I left out the inner error because it is lengthy and I didn't see it telling anything/
"error":{
"code":"","message":"Syntax error: character '%' is not valid at position 19 in 'new_name eq Ontario%2520-%2520Outside%2520the%2520Greenbelt%2520%2F%2520Ontario%2520-%2520%25C3%25A0%2520l'ext%25C3%25A9rieur%2520de%2520la%2520ceinture'."
So I figured out my issue. Dynamics CRM wasn't liking single quotes. The functions escape and encodeURI don't encode single quotes. But apparently in Dynamics CRM 365 single quotes are encoded by using two single quotes.
For example 'You're name', will become 'You''re name'. Leaving a single quote on the outside since the parameter has spaces.