How to solve the scope/asyncron problem in a record.js

UPDATE!!!
I think this is the cause: Mutable variable accesible inside closure

http://bonsaiden.github.io/JavaScript-Garden/#function.closures

But I do not know JS that deep, how to solve the issue

Hello. For various reasons I need to reset the quoted line items list_price to the list_price of the original product_templates when copying a quote (basically I have a discount module tied to an account, so if I change the account as it happens during copy Quote, I need to reset the QLI to the new account's prices). The file is: custom/modules/Quotes/clients/base/views/record/record.js and the code looks like this:

resetQuotedLinePrices: function(){
app.logger.info("Resetting price for quoted line item");
let bundles = this.model.get("bundles");
let product_bundles_item=null;
let product_template_id=null;
let product_templates_bean=null;
for (var i = 0; i < bundles.length; i++) {
product_bundles_item=bundles.models[i].get("product_bundle_items");
if (product_bundles_item.models.length > 0) {
for (let j=0; j<product_bundles_item.models.length;j++)
{
if (!_.isEmpty(product_bundles_item.models[j])) {
product_template_id=product_bundles_item.models[j].get("product_template_id");
product_templates_bean=app.data.createBean("ProductTemplates", { id: product_template_id });
if (!_.isEmpty(product_templates_bean))
{
app.logger.info("Before FETCH : "+product_bundles_item.models[j].get("list_price"));
product_templates_bean.fetch({
fields:["list_price","discount_price"],
success:function (product_template)
{
app.logger.info("New price will be: "+product_template.get("list_price")+" replacing old value: "+product_bundles_item.models[j].get("list_price"));
product_bundles_item.models[j].set("list_price",product_template.get("list_price"));
},
error:function ()
{
app.logger.fatal("Error retrieving product template bean resetQuotedLineItemPrices");
}
});
app.logger.info("AFTER FETCH : "+product_bundles_item.models[j].get("list_price"));
}
else
{
app.logger.fatal("Product Template bean empty");
}
}
}
}
}
}

The problem is that log outputs 25, "New price will be 50 replacing old value 25" and finally AGAIN 25. So as you can see, after that loop inside the success function of the ajax request, the price jumps back to the original one.
I do not know if this is a problem of scope or a problem of ajax request, but normally I would solve this issue with the "let self=this; use self in ajax" trick. This time I do not see how to apply that
  • You may create a custom endpoint which gets account_id and product_template_id. This endpoint may decide the source of QLI discount and send it back. Custom field template enum-mult-parent-api may help you on that. Take a look at enum-api.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • this answser would be fine if QLI would always stay the same. But remember, I am COPYING a quote, using the copy function from the top righthand side dropdown menu. So at the point in time when I am running this I do not have QLI in the database, I have only a VIRTUAL list from the database that will IN THE FUTURE, when the user press save actually become real QLI. So creating an API, selecting all the existing QLI and changing their value would not work in my usecase. So I need a strictly javascript solution that changes the model