Tags
crm, Microsoft Dynamics CRM, Money Field, mscrm, MSCRM 2011, MSCRM 2013, oData, REST, Set Money Field values in CRM jScript while using oData(REST)
Hello Everyone,
Just wanted to point out a small issue with Money field while using oData(REST). When you use oData to retrieve values for a Entity1 record and set it to another Entity2 record, then some field values need data conversion(like Money Field).
In case while you use your oData Query link to fetch values of a record in I.E. and you end up with the screen below:
The you would probably need some Setting change in your I.E.
- Open Internet Settings
- Move to Content Tab
- Click Settings under Feeds and Web Slices
- Remove Check from Advanced option Turn on Feed reading view
After you are done with the above settings then probably after an I.E. restart you will see the following screen as a result of your oData Query.
Now coming back to the issue detail, in one of my JScripts I was fetching the Product Value from Product Entity using oData(REST) and Setting up the same value to one of my Custom Entity field. Even though the values being fetched were correct but still while Setting up the Money Field it threw following Error on page.
“This control only accepts number or null as input.”
I then came across a blog which explains about this in details here. It says that some values needs conversion (like Money Field). Hence I changed my jScript from
Xrm.Page.getAttribute(price).setValue(value);
to
Xrm.Page.getAttribute(price).setValue(parseFloat(eval(value)));
and it worked perfectly onwards without any errors.
My oData Query to fetch values from Product Entity and Set to field in Custom Entity is as follows:
function SetPrice(results,price) { //here is where you’d add the code to handle the data returned debugger; if (results[0].StandardCost.Value != null) { var value = results[0].StandardCost.Value; Xrm.Page.getAttribute(price).setValue(parseFloat(eval(value))); //Xrm.Page.getAttribute(price).setValue(value); } else {Xrm.Page.getAttribute(price).setValue(null);} } function getPrice(product,price) { debugger; var pdt= Xrm.Page.data.entity.attributes.get(product); if (pdt.getValue() == null) { return; } pdtId = pdt.getValue()[0].id; var pagecontext = Xrm.Page.context; var serverUrl = pagecontext.getServerUrl(); var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; var oDataSelect = oDataPath + "/ProductSet?$select=StandardCost&$filter=ProductId" + " eq guid'" + pdtId + "'"; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { SetPrice(data.d.results,price); }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + oDataSelect ); } }); }
Hope this was helpful.
Thanks !!!
😀