CRM 2011 - Set value of currency field with javascript
Solution 1
I have used this before even though I am not a fan of the eval.
function SetMoneyAttribute(value, attribute) {
Xrm.Page.getAttribute(attribute)
.setValue(parseFloat(eval(value)));
}
here is a blog post about setting form fields with queried values.
http://crmscape.blogspot.com/2011/03/crm-2011-odata-json-and-crm-forms.html
Solution 2
//mimic crm object model
var Xrm = {
Page : {
getAttribute : function(sAttr) {
return {
setValue : function(nValue) {
alert(sAttr + ': ' + nValue);
}
};
}
}
};
function mySetValue(sAttr, nValue, nDefault) {
Xrm.Page.getAttribute(sAttr)
.setValue(
!isNaN(nValue = parseFloat(nValue)) ||
!isNaN(nValue = nDefault)
? nValue
: null);
}
//call with various types of values
mySetValue("new_attr1",0);
mySetValue("new_attr2","");
mySetValue("new_attr3",4);
mySetValue("new_attr4",34.3434);
mySetValue("new_attr5","545.43");
mySetValue("new_attr6",{},0);
//mySetValue("new_attr7",entities.d.results[0]["Price"], 100.00);
As the error states the attributes requires only numbers or null. To comply the first isNaN checks if parseFloat returns a number. If it returns undefined it tries to get the number from the default value (if supplied). If that is undefined and not a number then it assign a null value. You may omit the second isNaN test if you don’t need a default or if the default is always known (i.e. null or 0.0)
Comments
-
ThdK almost 2 years
I really can't find how i can retrieve the value from a currency field and set it as a value for another currency field for another entity.
My following code is not working:
var entities = retrieveRecords("trainingSet?$filter=trainingId eq guid'" + GetLookUpData("trainingid").id + "'"); if (entities != null) { if (entities.d.results.length > 0) { if (entities.d.results[0]["Price"] != null) { alert(entities.d.results[0]["Price"]); Xrm.Page.getAttribute("price").setValue(entities.d.results[0]["Price"].getValue()); Xrm.Page.getAttribute("price").setSubmitMode("always"); } } }
Error sais that the control only except numbers or null.
Any help would be really appreciated! Thanks!
-
ThdK over 12 yearsThank you! Great article too :)
-
GotDibbs over 12 yearsYou should only need to use parseFloat, you shouldn't need the eval that you currently have in that function. Same goes for picklist values and using parseInt (note you should specify the radix when using parseInt).