在 Dynamics CRM 9.0 以下版本中使用 Xrm.Page,而在 9.0 及以上版本中则需要使用 formContext 数据上下文对象。
//formContext :数据上下文对象,通过窗体上传过来
var formContext = executionContext.getFormContext();
//当前用户id
var userId = Xrm.Utility.getGlobalContext().userSettings.userId;
//当前用户名
var userName = Xrm.Utility.getGlobalContext().userSettings.userName;
//获取字段值
var fieldValue = formContext.getAttribute("fieldname").getValue();
//设置字段值
formContext.getAttribute("fieldname").setValue("new value");
//获取选项集字段值
var fieldValue = formContext.getAttribute("fieldname").getValue();
//设置选项集字段值
formContext.getAttribute("fieldname").setValue("new value");
//查找字段赋值
var value = [{
id: 'entityId',
entityType: '实体名称',
name: '名称'
}];
//隐藏和显示字段
formContext.getControl("fieldname"