CRM 2011 - 使用 javascript 设置货币字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7177780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CRM 2011 - Set value of currency field with javascript
提问by ThdK
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!
任何帮助将非常感激!谢谢!
回答by Bryan Weaver
I have used this before even though I am not a fan of the eval.
尽管我不是 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
http://crmscape.blogspot.com/2011/03/crm-2011-odata-json-and-crm-forms.html
回答by Adi Katz
//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)
正如错误所述,属性只需要数字或空值。为了遵守第一个 isNaN 检查 parseFloat 是否返回一个数字。如果它返回 undefined,它会尝试从默认值(如果提供)中获取数字。如果未定义且不是数字,则分配空值。如果您不需要默认值或者默认值总是已知的(即 null 或 0.0),您可以省略第二个 isNaN 测试