无法将箭头函数与CRM WebApi v9和typescript一起使用



我正在将js代码升级到Dynamics365的新V9版本,在使用Xrm.WebApi时无法使用箭头函数(也将js升级到ts(。

例如,这不起作用:

Xrm.WebApi.retrieveMultipleRecords(
'mks_entitlementline',
`$select=mks_name, _mks_parententitlementid_value&$filter=_mks_parententitlementid_value eq '${eId}'`).then(
(results) => {
if (!this.commonUtils.isUndefinedOrNull(results) && results.entities.length > 0) {
this.usesELS();
} else {
this.notUsingELS();
}
// filter contact lookup                        
this.filterContactLookup("", eId);
this.refreshPriorities(eId);
if (this.commonUtils.isUndefinedOrNull(this.formContext.getAttribute<Xrm.Attributes.LookupAttribute>('primarycontactid').getValue())) {
this.formContext.getControl<Xrm.Controls.LookupControl>('primarycontactid').setDisabled(false);
}
}).catch(error => {
console.log("ERROR -> entitlementSlaManagementOnUpdate: ", error);
Xrm.Utility.alertDialog("E----", () => { });
});

但这确实(在我看来更丑陋(:

Xrm.WebApi.retrieveRecord("role", searchedId, "$select=name")
.then(
function (role: { roleid: string, name: string }) {
outArr.push({ Id: role.roleid, Name: role.name, Type: "role" });
if (rolesAndTeams.length === outArr.length) {
if (!error) {
_onOk(outArr);
}
_onErr(errorObject)
}
},
function (err) {
errorObject = err;
error = true;
})

我收到的错误是:
Xrm.WebApi.retrieveMultipleRecords(…(.then(…(.catch不是函数

基本上告诉我"catch"是无效的,但我不知道为什么它不是,因为它对ts编译器来说是可以的。。。我还尝试将tsconfig文件配置为在es5和es2017上输出,但也不起作用。

所以。。。箭头函数可以与Xrm.WebApi一起使用吗?如果是…我做错了什么/没做错什么?

提前感谢!

我不认为问题是由箭头函数引起的。我认为catch就是问题所在。若返回值的类型为any,编译器将不会告诉您任何信息。我不知道是不是这样,但如果你看看CRM API,你会看到以下签名:

Xrm.WebApi.retrieveMultipleRecords(entityLogicalName, options, maxPageSize).then(successCallback, errorCallback);

没有提到catch,但您可以将errorCallback传递给then

顺便说一句,这就是第二个例子中传递errorHandler的方式。

所以试试这个:

Xrm.WebApi.retrieveMultipleRecords(
'mks_entitlementline',
`$select=mks_name, _mks_parententitlementid_value&$filter=_mks_parententitlementid_value eq '${eId}'`).then(
(results) => {
if (!this.commonUtils.isUndefinedOrNull(results) && results.entities.length > 0) {
this.usesELS();
} else {
this.notUsingELS();
}
// filter contact lookup                        
this.filterContactLookup("", eId);
this.refreshPriorities(eId);
if (this.commonUtils.isUndefinedOrNull(this.formContext.getAttribute<Xrm.Attributes.LookupAttribute>('primarycontactid').getValue())) {
this.formContext.getControl<Xrm.Controls.LookupControl>('primarycontactid').setDisabled(false);
}
},
error => {
console.log("ERROR -> entitlementSlaManagementOnUpdate: ", error);
Xrm.Utility.alertDialog("E----", () => { });
});

最新更新