整理下平时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);
控制Tab是否可见:
Xrm.Page.ui.tabs.get("tab_Name").setVisible(true);
控制Section是否可见:
Xrm.Page.ui.tabs.get("tab_Name").sections.get("section_name").setVisible(true);
设置必填:
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("required");
设置可选:
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("none");
设置字段可见:
Xrm.Page.getControl(sFieldName).setVisible(true);
隐藏字段:
Xrm.Page.getControl(sFieldName).setVisible(false);
获取当前 Entity Id:
var entityId = Xrm.Page.data.entity.getId();
获取当前 Entity 的 Name:
var entityName = Xrm.Page.data.entity.getEntityName();
获取当前 User Id:
var userId = Xrm.Page.context.getUserId();
判断当前 form 的状态:
if (Xrm.Page.ui.getFormType() == 1){ //1:Create } else{ //0:Undefined //2:Update //3:Read Only //4:Disabled //6:Bulk Edit }
添加事件:
Xrm.Page.getControl(sFieldName).getAttribute().addOnChange(fnOnChange);
有時需要獲取當前頁面,所有賦值字段的情況(Debug需要,比如儅出現unsaved changes):
console.log(Xrm.Page.data.entity.getDataXml());
阻止当前页面保存,需要勾选 Pass execution context as first parameter
exeContext.getEventArgs().preventDefault();标签:Xrm,getControl,对字段,getAttribute,sFieldName,JS,var,Dynamics,Page From: https://www.cnblogs.com/jiangyuhu/p/17705209.html