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

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

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(){"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++) {
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");"ProductTemplates", { id: product_template_id });
if (!_.isEmpty(product_templates_bean))
{"Before FETCH : "+product_bundles_item.models[j].get("list_price"));
success:function (product_template)
{"New price will be: "+product_template.get("list_price")+" replacing old value: "+product_bundles_item.models[j].get("list_price"));
error:function ()
app.logger.fatal("Error retrieving product template bean resetQuotedLineItemPrices");
});"AFTER FETCH : "+product_bundles_item.models[j].get("list_price"));
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
Parents Reply Children
  • 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