0

当我尝试使用 XrmServiceToolkit.Soap.Create 函数创建从 JS 到 CRM 的新记录时,它需要很长时间才能完成。所以 JS 继续执行另一个函数,在函数 RecalculateSurface 中我检索了由第一个函数创建的记录,因此我无法获取记录的值,因为它尚未保存。

function save()

{
//some code
delivery.attributes["eoz_unit"] = Unit;
delivery.attributes["description"] = quoteDetail.Description;
delivery.attributes["quoteid"] = { id: quoteId, logicalName: "quote", type: "EntityReference" };
 XrmServiceToolkit.Soap.Create(delivery);   //function 1
RecalculateSurface();
}
function RecalculateSurface()
{
// code to retreive the record created in function 1  

}

任何想法让函数 RecalculateSurface() 等待记录的保存?XrmServiceToolkit.Soap.Create 函数返回所创建记录的 ID。

4

1 回答 1

1

所以,创建函数有一个可选的回调函数参数......

XrmServiceToolkit.Soap.Create(businessEntity, [callback(result)]);

您应该使用该选项,而不仅仅是传入实体

function save() {
    //some code
    delivery.attributes["eoz_unit"] = Unit;
    delivery.attributes["description"] = quoteDetail.Description;
    var quoteidObj = {
        id: quoteId,
        logicalName: "quote",
        type: "EntityReference" 
    };
    delivery.attributes["quoteid"] = quoteidObj;
    // do function 1
    XrmServiceToolkit.Soap.Create(delivery, function(result){  
        RecalculateSurface();
    });   
}

function RecalculateSurface() {
    // code to retrieve the record created in function 1  
}

这应该会导致 XrmServiceToolkit.Soap.Create 调用异步操作,并且仅在完成时调用 RecalculateSurface()。

如果您想扩展解决方案以使用 Promise,那么您可以执行以下操作:

function save() {
    //some code
    delivery.attributes["eoz_unit"] = Unit;
    delivery.attributes["description"] = quoteDetail.Description;
    var quoteidObj = {
        id: quoteId,
        logicalName: "quote",
        type: "EntityReference" 
    };
    delivery.attributes["quoteid"] = quoteidObj;
    callCreate(delivery).then(function(){
        RecalculateSurface()
    });
}

function callCreate(delivery){
    return new Promise(function(resolve,reject) {
        XrmServiceToolkit.Soap.Create(delivery, function(result){
            resolve();
        });
    });
}

function RecalculateSurface() {
    // code to retrieve the record created in function 1  
}
于 2020-07-24T16:43:37.303 回答