我们经常要对表单里各种类型的字段进行读取或赋值,下面列出各种类型的读写方法及注意事项:
1. lookup 类型
var state = Xrm.Page.getAttribute("new_state");if (state != null){Xrm.Page.getAttribute("new_state").setValue(null);}
var state = new Array();state = Xrm.Page.getAttribute("new_state").getValue();
if(state != null){var stateText = state[0].name;var stateId = state[0].id;
var stateType = state[0].entityType;
alert("State is: " + stateText + ", ID: " + stateId + "of type: " + stateType);
}
特别要注意这里取出来的值,一定要加[0],不然取不到值
var state = new Array();state[0] = new Object();
state[0].id = "{BA0762E4-64D2-E111-909E-00155D6C871A}";
state[0].name = "New York";state[0].entityType = "new_state";
Xrm.Page.getAttribute("new_state").setValue(state);
2. Option sets
var sval = Xrm.Page.getAttribute("new_optionset").getSelectedOption().text;var sval = Xrm.Page.getAttribute("new_optionset").getSelectedOption().value;
分别读取显示文本和值
另外,我们还可以用下面的方法来获取text和value:
var sval = Xrm.Page.getAttribute("new_optionset").getText();var sval = Xrm.Page.getAttribute("new_optionset").getValue();
function SetOSValue(osName, osLabel)
{var options = Xrm.Page.getAttribute(osName).getOptions();
for(i = 0; i < options.length; i++){if (options[i].text == osLabel)
Xrm.Page.getAttribute(osName).setValue(options[i].value);}}
3. Datetime
var myContactBirthday;
myContactBirthday = Xrm.Page.getAttribute("birthdate").getValue();
alert("Contact birthday is: " + myContactBirthday);var year = myContactBirthday.getFullYear();
var month = myContactBirthday.getMonth(); // from 0 to 11var day = myContactBirthday.getDate(); // from 1 to 31month = month + 1;alert("Year: " + year + ", Month: " + month + ", Day: " + day);
var currentDateTime = new Date();Xrm.Page.getAttribute("new_myDate").setValue(currentDateTime);
4. Currency
var myCurrencyField;
myCurrencyField = Xrm.Page.getAttribute("new_currencyfield").getValue();
alert("The value of this Currency field is: " + myCurrencyField);
var myCurrencyValue = 6.25;
Xrm.Page.getAttribute("new_currencyfield").setValue(parseFloat(myCurrencyValue));
5. number, text
var myNumber;
myNumber = Xrm.Page.getAttribute("new_number").getValue();
alert("The number in the field is: " + myNumber);
Xrm.Page.getAttribute("new_text").setValue("abc");Xrm.Page.getAttribute("new_number").setValue(123);
Dynamic CRM 2013学习笔记 系列汇总
Dynamic CRM 2013学习笔记(十七)JS读写各种类型字段方法及技巧
原文地址:http://blog.csdn.net/hufengwen/article/details/42362161