I'm trying to use CRM Web API in JavaScript to perform a record creation operation inside a For clause.
This is what my code does:
1. Call retrieve multiple to bring all product related to a work order
2. Then runs a For clause to execute a child record creation (estimated item) for every retrieved product.
Everything works fine, but if I have to create several records, the asynchronous nature of the Web API requests leads me to exceed the 52 limit.
This is the function I'm working on (record creation highlighted in the code):
function quotelogic(FirstPrimaryItemId){
//obtain the work order record ID
var woid = FirstPrimaryItemId;
console.log(woid);
var workorderid = convertGuid(woid);
// Add new Records
Xrm.WebApi.retrieveMultipleRecords("msdyn_workorderproduct", "?$select=myprefix_estimatequantity, myprefix_estimateqty, msdyn_product, msdyn_description, myprefix_estimateunit, myprefix_estimateamount, myprefix_usedquantity, myprefix_unitamount, myprefix_totalamount, msdyn_linestatus, msdyn_name&$filter=_msdyn_workorder_value eq (" + woid + ")").then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
//console.log(result.entities[i]);
//window.alert(result.entities[i].myprefix_nextstepaging);
var estimatedquantity = result.entities[i].myprefix_estimateqty;
var description = result.entities[i].msdyn_description;
var estimatedunit = result.entities[i].myprefix_estimateunit;
var estimatedamount = result.entities[i].myprefix_estimateamount;
var quantity = result.entities[i].myprefix_usedquantity;
var unitamount = result.entities[i].myprefix_unitamount;
var totalamount = result.entities[i].myprefix_totalamount;
var linestatus = result.entities[i].msdyn_linestatus;
var product = result.entities[i].msdyn_name;
var productid = result.entities[i].msdyn_workorderproductid;
if (product.includes("Travel")==false && product.includes("Labour")==false && (linestatus==690970000 || linestatus==753950003))
{
// define the data to create a record
var data =
{
"myprefix_estimatedquantity": estimatedquantity,
"myprefix_estimatedunit": estimatedunit,
"myprefix_estimatedamount": estimatedamount,
"myprefix_items": product,
"myprefix_description": description,
"myprefix_WorkOrderId@odata.bind": "/msdyn_workorders("+workorderid+")"
}
// create estimated item record
Xrm.WebApi.createRecord("myprefix_estimateditem", data).then(
function success(result) {
console.log("Estimated Part record created");
// perform operations on record update
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
}
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
Any input would be very helpful