Search code examples
typescriptdynamics-crmdynamics-crm-onlinedynamics-crm-webapi

Cannot use arrow functions with CRM WebApi v9 and typescript


I´m working on the upgrade of the js code to the new V9 version of Dynamics 365 and I cannot use arrow functions when using Xrm.WebApi (also upgrading js to ts).

For example, this does not work:

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----", () => { });
                    });

but this does (uglier in my opinion):

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;
                        })

The error I´m receiving is:
Xrm.WebApi.retrieveMultipleRecords(...).then(...).catch is not a function

Basically tells me that 'catch' is not valid but I don´t know why it isn´t since it is ok for the ts compiler... I also tried configuring tsconfig file to output on es5 and es2017 but it didn´t work either.

so... can arrow functions be used with Xrm.WebApi? if so... what I´m doing wrong/not doing?

Thanks in advance!


Solution

  • I don't think the problem is caused by arrow functions. I think catch is the problem. Compiler won't tell you anything, if return value is of type any. I don't know if that is the case, but if you look into CRM API, you will see following signature:

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

    There is no mention of catch, but instead you can pass errorCallback to then.

    BTW this is the way, you pass errorHandler in the second example.

    So try this:

    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----", () => { });
                    });