使用Xrm.WebApi.retrieveRecord获取记录时遇严格模式错误求助
Dynamics 365客户端脚本报错:TypeError 严格模式函数属性访问限制问题
错误原因
该错误源于Dynamics 365客户端脚本默认运行在严格模式环境中,严格模式禁止访问函数的caller、callee和arguments属性。结合你的代码来看,核心问题是异步回调函数的作用域丢失,导致隐式触发了严格模式下的禁止操作;另外还存在tab名称大小写不一致的问题(inspectionsTab和inspectionstab),会引发控件找不到的潜在错误。
解决步骤及修正代码
以下是修正后的代码,同时解决了作用域问题和控件名称一致性问题:
if (typeof (ContosoPermit) == "undefined") { var ContosoPermit = { __namespace: true }; } if (typeof (ContosoPermit.Scripts) == "undefined") { ContosoPermit.Scripts = { __namespace: true }; } ContosoPermit.Scripts.PermitForm = { handleOnLoad: function (executionContext) { console.log('on load - permit form'); }, handleOnChangePermitType: function (executionContext) { console.log('on change - permit type'); // 调用处理逻辑,传递执行上下文 ContosoPermit.Scripts.PermitForm._handlePermitTypeSettings(executionContext); }, _handlePermitTypeSettings: function (executionContext) { var formContext = executionContext.getFormContext(); var permitType = formContext.getAttribute("contoso_permittype").getValue(); // 统一tab名称大小写,避免找不到控件 var inspectionsTab = formContext.ui.tabs.get("inspectionsTab"); if (permitType == null) { inspectionsTab.setVisible(false); return; } // 格式化ID,去掉大括号(Xrm.WebApi要求ID不带大括号) var permitTypeID = permitType[0].id.replace(/[{}]/g, ""); // 使用箭头函数保留外部作用域,避免严格模式下的作用域问题 // 添加?$select参数,仅获取需要的字段,提升性能 Xrm.WebApi.retrieveRecord("contoso_permittype", permitTypeID, "?$select=contoso_requireinspections").then( (result) => { inspectionsTab.setVisible(result.contoso_requireinspections === true); }, (error) => { alert('错误:' + error.message); } ); }, __namespace: true }
关键修改说明
- 统一控件名称大小写:Dynamics 365的控件名称区分大小写,将
inspectionstab改为inspectionsTab,确保能正确找到tab控件 - 格式化记录ID:
permitType[0].id返回的ID包含大括号,Xrm.WebApi要求ID不带大括号,因此用正则替换去掉大括号 - 箭头函数回调:使用箭头函数替代普通函数作为
then的回调,确保外部作用域的formContext(或inspectionsTab)能被正确访问,避免因作用域切换触发严格模式下的属性访问限制 - 添加字段选择器:通过
?$select=contoso_requireinspections仅获取需要的字段,减少数据传输量,提升脚本性能
内容的提问来源于stack exchange,提问作者user2172903
相关产品推荐
相关产品推荐

