Dynamics 365 JS Code Snippets
00 新建WebSource
Web资源命名规则:new_/js/xxxx.js
显示名称:xxxx.js
描述:[模块名称]xxxx实体脚本
//[售前模块]竞争对手(项目线索)脚本
if (typeof (Manufacturer) == "undefined") {
Manufacturer = { __namespace: true };
}
Manufacturer = {
_executionContext: null,
_formContext: null,
OnLoad: function (executionContext) {
executionContext = executionContext;
_formContext = executionContext.getFormContext();
},
}
01 检索记录
retrieveRecord
Xrm.WebApi.retrieveRecord("account","a8a19cdd-88df-e311-b8e5-6c3be5a8b200","?$select=name,revenue").then(
functionsuccess(result){
console.log("Retrieved values: Name: "+ result.name +", Revenue: "+ result.revenue);
// perform operations on record retrieval
},
function(error){
console.log(error.message);
// handle error conditions
}
);
retrieve multiple
//Basic retrieve multiple
Xrm.WebApi.retrieveMultipleRecords("account","?$select=name&$top=3").then(
functionsuccess(result){
for(var i =0; i < result.entities.length; i++){
console.log(result.entities[i]);
}
// perform additional operations on retrieved records
},
function(error){
console.log(error.message);
// handle error conditions
}
);
//Basic retrieve multiple with FetchXML
var fetchXml ="?fetchXml=<fetch mapping='logical'><entity name='account'><attribute name='accountid'/><attribute name='name'/></entity></fetch>";
Xrm.WebApi.retrieveMultipleRecords("account", fetchXml).then(
functionsuccess(result){
for(var i =0; i < result.entities.length; i++){
console.log(result.entities[i]);
}
// perform additional operations on retrieved records
},
function(error){
console.log(error.message);
// handle error conditions
}
);
02 表单状态及状态码
var fromType = formContext.ui.getFormType();
Form type | Return Value |
---|---|
Undefined | 0 |
Create | 1 |
Update | 2 |
Read Only | 3 |
Disabled | 4 |
Bulk Edit | 6 |
03 保存方式
executionContext.getEventArgs().getSaveMode();
Entity | Save Mode | Return Value |
---|---|---|
All | Save | 1 |
All | Save and Close | 2 |
All | Deactivate | 5 |
All | Reactivate | 6 |
Send | 7 | |
Lead | Disqualify | 15 |
Lead | Qualify | 16 |
User or Team owned entities | Assign | 47 |
Activities | Save as Completed | 58 |
All | Save and New | 59 |
All | Auto Save | 70 |
04 获取用户安全角色
var currentRoles = Xrm.Utility.getGlobalContext().userSettings.roles.getAll();
if(currentRoles.some(item=> item.name ==="系统管理员"))
{
Xrm.Page.getControl("name").setDisabled(false);
}
else
{
Xrm.Page.getControl("name").setDisabled(true);
}
05 获取当前实体的Guid
var entityId = Xrm.Page.data.entity.getId();
06 获取当前实体Name
var entityName = Xrm.Page.data.entity.getEntityName();
07 获取用户Guid
var userId = Xrm.Page.context.getUserId();
08 字段取值
var oResult = Xrm.Page.getAttribute("name").getValue();
var oResult = Xrm.Page.getControl(sFieldName).getAttribute().getValue();
09 字段赋值
0901 一般类型
Xrm.Page.getAttribute("sFieldName").setValue(object);
0902 选项类型
Xrm.Page.getAttribute("sFieldName").setValue(1);
//OptionSet的对应选项的Value
0903 查找类型
一般的
Xrm.Page.getAttribute("sFieldName").setValue([{ id:"record id", name: "sValue", entityType: "Entity Name" }]);
Xrm.WebApi.retrieveRecord返回的查找字段赋值
if (typeof (SalesQuotation) == "undefined") {
SalesQuotation = { __namespace: true };
}
SalesQuotation = {
_executionContext: null,
_formContext: null,
OnLoad: function (executionContext) {
_executionContext = executionContext;
_formContext = executionContext.getFormContext();
},
/**
* 根据[项目线索]赋值
* 项目名称
* 船东(查找字段)
* */
setFieldValFromProjecLeadst: function () {
console.log("SetQuotaEntry bbbbbb");
debugger;
var leads = _formContext.getAttribute("cssc_project_leads").getValue();
if (leads != null) {
var leadsId = leads[0].id.replace('{', '').replace('}', '');
Xrm.WebApi.retrieveRecord("cssc_project_leads", leadsId, "?$select=cssc_project_name,_cssc_shipowners_value").then(
function success(result) {
_formContext.getAttribute("cssc_project_name").setValue(result.cssc_project_name);//项目名称
_formContext.getAttribute("cssc_shipowners").setValue(SalesQuotation.structureLookUp(result, "_cssc_shipowners_value"));//船东
},
function (error) {
console.log(error.message);
}
);
}
},
structureLookUp: function (result, field) {
var id = result[field];
if (id != null) {
var entityType = result[field + "@Microsoft.Dynamics.CRM.lookuplogicalname"];
var name = result[field + "@OData.Community.Display.V1.FormattedValue"];
var lookup = new Array(1);
lookup[0] = new Object();
lookup[0].id = id;
lookup[0].name = name;
lookup[0].entityType = entityType;
return lookup;
}
else {
return null;
}
},
}
0904日期类型
//注意,日期类型必须得new Date()
Xrm.Page.getAttribute(sFieldName).setValue(new Date());
10 字段必填、非必填
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("required");
Xrm.Page.getAttribute(sFieldName).setRequiredLevel("none");
11 字段只读、非只读
Xrm.Page.getControl(sFieldName).setDisabled(true);
Xrm.Page.getControl(sFieldName).setDisabled(false);
12 字段可见、不可见
Xrm.Page.getControl(sFieldName).setVisible(true);
Xrm.Page.getControl(sFieldName).setVisible(false);
13 添加点击事件
示例代码
//[售前模块]竞争对手(项目线索)脚本
if (typeof (Manufacturer) == "undefined") {
Manufacturer = { __namespace: true };
}
Manufacturer = {
_executionContext: null,
_formContext: null,
OnLoad: function (executionContext) {
executionContext = executionContext;
_formContext.getAttribute("cssc_project_leads").addOnChange(SalesQuotation.setFieldValFromProjecLeadst);
},
}
14 选项卡显示、隐藏
var tabObj = _formContext.ui.tabs.get("tab_2");
tabObj.setVisible(true);
tabObj.setVisible(false);
15 Section显示、隐藏
var tabObj = _formContext.ui.tabs.get("tab_2");
var sectionObj = tabObj.sections.get("section_2");
sectionObj.setVisible(true);
sectionObj.setVisible(false);
16 锁定表单所有字段
Xrm.Page.ui.controls.forEach(function (control, index) {
control.setDisabled(true);
});
17 可编辑子网格
所有列均不可编辑
Onrowselect: function (executionContext) {
var entityObject = executionContext.getFormContext().data.entity;
entityObject.attributes.forEach(function (attribute, i) {
var subgridControl = attribute.controls.get(0);
subgridControl.setDisabled(true);
});
}
指定列不可编辑
Onrowselect: function (executionContext) {
var entityObject = executionContext.getFormContext().data.entity;
entityObject.attributes.forEach(function (attribute, i) {
var subgridControl = attribute.controls.get(0);
if(attribute.getName()=="cssc_name"){
subgridControl.setDisabled(true);
}
});
}
18 设置提示信息(表单、字段)
表单
Xrm.Page.ui.setFormNotification("notification content","WARNING","notification name");//type:"INFORMATION","ERROR","WARNING"
Xrm.Page.ui.clearFormNotification('notification name');//clear all form notification when parameter is null
字段
Xrm.Page.getControl("attributeName").setNotification("notification content");
Xrm.Page.getControl("attributeName").clearNotification();
19 快速创建表单获取父实体信息
function quickCreateOnLoad(){
var parentRecordReference = Xrm.Utility.getPageContext().input.createFromEntity;
if (parentRecordReference == null){
return;
}
console.log(parentRecordReference.id);
console.log(parentRecordReference.entityType);
console.log(parentRecordReference.name);
}
此处评论已关闭