整理下平时CRM开发中用到的一些基本的js操作,或许对初学Dynamics的新手有所帮助
var entityId = Xrm.Page.data.entity.getId();
var entityName = Xrm.Page.data.entity.getEntityName();
var userId = Xrm.Page.context.getUserId();
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.getAttribute(sFieldName).setRequiredLevel("required");
Xrm.Page.getControl(sFieldName).setVisible(true);
Xrm.Page.getControl(sFieldName).setVisible(false);
Xrm.Page.getControl(sFieldName).setDisabled(true);
Xrm.Page.getControl(sFieldName).setDisabled(true);
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("required");
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("none");
Xrm.Page.getControl(sFieldName).getAttribute().addOnChange(fnOnChange);
if (Xrm.Page.ui.getFormType() == 1){
//1:Create创建
}
else{
//0:Undefined尚未定义
//2:Update更新
//3:Read Only只读
//4:Disabled已禁用
//6:Bulk Edit批量编辑
}
window.parent.Xrm.Utility.openEntityForm("Entity Name", id);
Xrm.Page.getControl(sFieldName).setFocus();
Xrm.Page.getControl("student").setVisible(false);
Xrm.Page.ui.tabs.get("tab_2").setVisible(false);
Xrm.Page.getControl("lt_id").setNotification("该身份证号已被注册!", "IDCard2");
Xrm.Page.getControl("lt_id").clearNotification("IDCard2");
Xrm.Page.data.save().then(function () { parent.window.location.reload(); });
Xrm.Utility.openEntityForm(Xrm.Page.data.entity.getEntityName(), Xrm.Page.data.entity.getId());
Xrm.Page.ui.close();
Xrm.Page.ui.controls.getAll().forEach(function (item) { typeof (item.setDisabled) === 'function'
//锁定所有字段
istruedisabled("tab_1");
//禁用窗体字段方法
function istruedisabled(entityname) {
var tabControl = Xrm.Page.ui.tabs.get(entityname);
if (tabControl != null) {
Xrm.Page.ui.controls.forEach(
function (control) {
control.setDisabled(true);
});
}
}
小Monkey还会不断补充的
到此就结束啦,快去练习一下吧!欢迎大佬和小Monkey沟通。
感谢大佬指正 小Monkey
如果你觉得有用的话,就留个赞吧!蟹蟹