整理下平时CRM开发中用到的一些基本的js操作
取值:
var oResult = Xrm.Page.getAttribute(sFieldName).getValue(); var oResult = Xrm.Page.getControl(sFieldName).getAttribute().getValue();
赋值:
一般类型:Xrm.Page.getAttribute(sFieldName).setValue(object); OptionSet类型: Xrm.Page.getAttribute(sFieldName).setValue(1);//OptionSet的对应选项的Value Lookup类型: Xrm.Page.getAttribute(sFieldName).setValue([{ id:"record id", name: "sValue", entityType: "Entity Name" }]); DateOnly类型: Xrm.Page.getAttribute(sFieldName).setValue(new Date());
提示指定错误:
Xrm.Page.getControl(sFieldName).setNotification(sErrorMessage);
清除错误提示:
Xrm.Page.getConrol(sFieldName).clearNotification();
设置Read Only:
Xrm.Page.getControl(sFieldName).setDisabled(true);
设置Enable:
Xrm.Page.getControl(sFieldName).setDisabled(false);
设置必填:
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("required");
设置可选:
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("none");
获取当前 Entity Id:
var entityId = Xrm.Page.data.entity.getId();
获取当前 User Id:
var userId = Xrm.Page.context.getUserId();
添加事件:
Xrm.Page.getControl(sFieldName).getAttribute().addOnChange(fnOnChange);