CRM 2011 - Set value of currency field with javascript
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'" + GetLoo开发者_运维技巧kUpData("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!
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
//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)
精彩评论