I try to find duplicates in CRM for given names per WEB API. I use a API call like this:
https://orgname.crm.dynamics.com/api/data/v9.2/RetrieveDuplicates(BusinessEntity=@p1,MatchingEntityName=@p2,PagingInfo=@p3)?@p1=%7B%22%40odata.type%22%3A%22Microsoft.Dynamics.CRM.account%22%2C%22name%22%3A%22ACME%20Corp%22%7D&@p2=%27account%27&@p3=%7B%27PageNumber%27%3A1%2C%27Count%27%3A50%7D
with @p1 being an urlecoded json:
{"@odata.type":"Microsoft.Dynamics.CRM.account","name":"ACME Corp"}
All work without problems until a name contains brackets like ACME (USA) Corp. This gives me the error message "Bad Request - Error in query syntax."
The error only happens if there is text after the closing bracket, so this
https://orgname.crm.dynamics.com/api/data/v9.2/RetrieveDuplicates(BusinessEntity=@p1,MatchingEntityName=@p2,PagingInfo=@p3)?@p1=%7B%22%40odata.type%22%3A%22Microsoft.Dynamics.CRM.account%22%2C%22name%22%3A%22ACME%20%28USA%29%22%7D&@p2=%27account%27&@p3=%7B%27PageNumber%27%3A1%2C%27Count%27%3A50%7D
{"@odata.type":"Microsoft.Dynamics.CRM.account","name":"ACME (USA)"}
works as expected and retrieves existing duplicates.
But this gives a bad request error:
https://orgname.crm.dynamics.com/api/data/v9.2/RetrieveDuplicates(BusinessEntity=@p1,MatchingEntityName=@p2,PagingInfo=@p3)?@p1=%7B%22%40odata.type%22%3A%22Microsoft.Dynamics.CRM.account%22%2C%22name%22%3A%22ACME%20%28USA%29Corp%22%7D&@p2=%27account%27&@p3=%7B%27PageNumber%27%3A1%2C%27Count%27%3A50%7D
{"@odata.type":"Microsoft.Dynamics.CRM.account","name":"ACME (USA)Corp"}
I tried the brackets, slash escaped brackets, double slash escaped brackets, all the former urlencoded.
Brackets fives "Bad Request", single slash gives "Invalid JSON. An unrecognized escape sequence '\(' was found in a JSON string value.", double slash gives no error but also no duplicates even if they exist.
This seems to be caused by an internal serialization flaw.
A workaround could be to identify the record you need to check for duplicates by using its ID. Your query would then look like this:
https://orgname.crm.dynamics.com/api/data/v9.2/RetrieveDuplicates(BusinessEntity=@p1,MatchingEntityName=@p2,PagingInfo=@p3)?@p1={"@odata.type":"Microsoft.Dynamics.CRM.account","accountid":"b6047a80-b59d-ef11-a72c-0022489e68d8"}&@p2='account'&@p3={'PageNumber':1,'Count':50}
When you do not have an ID, you could use a simple query like this:
https://orgname.crm.dynamics.com/api/data/v9.2/accounts?$filter=name eq 'ACME (USA) Corp.' and statecode eq 0&$top=50