Answered by:
odata: Unknown function 'replace'

Question
-
I created a webresource that retrieves records with odata and javascript. It is a pity that I can't use a replace function in the odata query. I get the error "Unknown function 'replace'" when I try replace like this: http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=replace(CompanyName, ' ', '') eq 'AlfredsFutterkiste'
I read a bit about the background here: http://stackoverflow.com/questions/30630672/odata-unknown-function-replace
The replace function is part of the odata URI conventions for version 2.0 (http://www.odata.org/documentation/odata-version-2-0/uri-conventions/), but not in version 4.0 (?). It is also listed as filter expression for NAV 2016 (https://msdn.microsoft.com/en-us/library/hh169248%28v=nav.90%29.aspx). Is there any hope that the replace function will ever become available for CRM? It is on some 'to do' list? Or did I miss some information/alternative? I use CRM Online.
What do I try to do here? The web resource receives a phone number in the URL. I want to check if this phone number is inside the fields Telephone1 and MobilePhone in the Contacts. I can deal with a lot of different factors. This is the last factor that is bugging me: users want to use characters like space, /, -, (, ), in there. I want to ignore these special characters. The replace function inside the query would be great to do that.
I have found a workaround, but (as with most workarounds) it isn't pretty... With Jason Lattimer's Workflow Utilities (http://jlattimer.blogspot.de/2015/05/crm-string-workflow-utilities.html), I can copy the values inside CRM to another field and replace the values there. I can then have the odata query search those alternative fields.
Note: This is my first post here. Sorry for not having the links clickable. I got the message "Body text cannot contain images or links until we are able to verify your account."
Thursday, December 31, 2015 5:49 PM
Answers
-
Unfortunately the full OData spec hasn't been implemented with the older 2011 endpoint or the new v4.0 Web API endpoint. It's possibly this functionality might come to the new endpoint but it won't be until another major spring or fall release.
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Thursday, December 31, 2015 8:25 PM
- Marked as answer by Wim_80 Friday, January 1, 2016 9:35 AM
Thursday, December 31, 2015 8:25 PMModerator
All replies
-
Unfortunately the full OData spec hasn't been implemented with the older 2011 endpoint or the new v4.0 Web API endpoint. It's possibly this functionality might come to the new endpoint but it won't be until another major spring or fall release.
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Thursday, December 31, 2015 8:25 PM
- Marked as answer by Wim_80 Friday, January 1, 2016 9:35 AM
Thursday, December 31, 2015 8:25 PMModerator -
I found a workaround that works in my specific case. To ignore the special characters in phone number fields, I use wildcards. % is "A substitute for zero or more characters" (http://www.w3schools.com/sql/sql_wildcards.asp). In the encoded odata line % will need to appear as %25 (http://www.w3schools.com/tags/ref_urlencode.asp).
So if I want to look up 15424912972 in the mobile phone field and ignore special characters, I have to place wildcards between all characters (not a big deal with JavaScript). The filter criterium then looks like this:
$filter=substringof('1%255%254%252%254%259%251%252%259%257%252', MobilePhone)
I found this article (http://stackoverflow.com/questions/21163475/crm-2011-odata-substringof-wildcards) about the _ wildcard, and that made me lookup other SQL wildcards that I could use. And it worked :-)
Friday, January 29, 2016 9:08 AM