Error: Document has been modified after you have opened it for multiple time save

//doctypename.js

data = [{"productId":123,"qty":5},{"productId":124,"qty":10}]

for(let i=0;i<data.length;i++){
    reduce_stock(data[i].productId,data[i].qty)
}

function reduce_stock(productId,qty){
    frappe.call({
        method: "path/to/reduce_stock",
        args:{
            productId:productId,
            qty:qty
        },
        callback: (r)=>{

            if(r.message){
                console.log("success",r.message)
            }else{
                console.log("error -> no doc found")
            }
        }
    })
}
   
-----------------------------------------------------------------------------------
//doctypename.js

def reduce_stock(productId,qty):
    docs = frappe.get_all("Doctype name", filters={"id":productId})
    if docs:
        doc = frappe.get_doc("Doctype name", docs[0].name)
        doc.qty = doc.qty - qty
        doc.save()
        frappe.db.commit()
        return true
    else:
        return false

i want multiple time open the diff doc and change the values then save but i got error like
Error: Document has been modified after you have opened it

Please use frappe.db.set_value()

1 Like