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