我们经常要对表单里各种类型的字段进行读取或赋值,下面列出各种类型的读写方法及注意事项:
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学习笔记 系列汇总