首页 > 解决方案 > 如果您不先执行 alert(),则使用 Xrm.WebApi.createRecord 创建事件会失败

问题描述

我有一个使用 Xrm.WebApi.createRecord 创建事件记录的小方法

function createChangeRequest(emailData) {
var createRequestObj = null;

try {
    //create CR object
    createRequestObj = new Object();
    createRequestObj.title = emailData.name;
    createRequestObj.description = emailData.description;

    var senderId = emailData.senderId.replace('{', '').replace('}', '');
    var account = "";

    if (emailData.senderEntity == 'contact') {
        try {
            //alert(senderId);
            Xrm.WebApi.retrieveRecord("contact", senderId, "$select=fullname&$expand=parentcustomerid_account($select=accountid,name)")
                .then(function (data) {
                    if (data.parentcustomerid_account.accountid != null) {
                        //alert(data.parentcustomerid_account.accountid);
                        account = data.parentcustomerid_account.accountid;

                        //set the lookup value
                        createRequestObj["customerid_account@odata.bind"] = "/accounts(" + account.toUpperCase() + ")";
                    }
                },
                    function (error) {
                        Xrm.Utility.alertDialog(error.message);
                    });
        } catch (e) {
            Xrm.Utility.alertDialog(e.message);
        }

        //set the lookup value
        createRequestObj["primarycontactid@odata.bind"] = "/contacts(" + senderId + ")";
    }
    else if (emailData.senderEntity == 'account') {
        //set the lookup value
        createRequestObj["customerid_account@odata.bind"] = "/accounts(" + senderId + ")";
    }

alert('wibble');

    Xrm.WebApi.createRecord("incident", createRequestObj).then(function (result) {           
        //get the guid of created record
        var recordId = result.id;

        //below code is used to open the created record
        var windowOptions = {
            openInNewWindow: false
        };
        //check if XRM.Utility is not null
        if (Xrm.Utility != null) {
            //open the entity record
            Xrm.Utility.openEntityForm("incident", recordId, null, windowOptions);
        }
    },
    function (error) {
        Xrm.Utility.alertDialog('Create error - ' + error.message);
    });
}
catch (e) {
    Xrm.Utility.alertDialog('General Error - ' + e.message);
}
}

这可以按预期工作,但是如果我删除或注释掉alert('wibble');它,它会跟随错误回调并给出消息“创建错误 - 发生意外错误”

我不知道为什么会这样,或者看到用户单击“确定”引起的短暂延迟应该使它起作用的任何原因。

标签: javascriptdynamics-crmmicrosoft-dynamicsdynamics-365dynamics-crm-webapi

解决方案


这可能是因为Xrm.WebApi方法的异步行为。此调用返回的 Promise 对象必须在浏览器中自行方便地运行 :)

移动Xrm.WebApi.createRecord成功回调中的行,Xrm.WebApi.retrieveRecord如下所示:

Xrm.WebApi.retrieveRecord("contact", senderId, "$select=fullname&$expand=parentcustomerid_account($select=accountid,name)")
            .then(function (data) {
                if (data.parentcustomerid_account.accountid != null) {
                    //alert(data.parentcustomerid_account.accountid);
                    account = data.parentcustomerid_account.accountid;

                    //set the lookup value
                    createRequestObj["customerid_account@odata.bind"] = "/accounts(" + account.toUpperCase() + ")";

                    Xrm.WebApi.createRecord("incident", createRequestObj).then(function (result) {           
                        //get the guid of created record
                        var recordId = result.id;

                       //below code is used to open the created record
                       var windowOptions = {
                           openInNewWindow: false
                       };
                       //check if XRM.Utility is not null
                       if (Xrm.Utility != null) {
                           //open the entity record
                           Xrm.Utility.openEntityForm("incident", recordId, null, windowOptions);
                       }
                   },
                   function (error) {
                       Xrm.Utility.alertDialog('Create error - ' + error.message);
                   });

                 }
                },
                function (error) {
                    Xrm.Utility.alertDialog(error.message);
                });

推荐阅读