当我尝试使用功能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。
答案 0 :(得分:1)
因此,create函数具有可选的回调函数参数...
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
}