Update Value using db.set_value

how can i Update Field Value with Filters using db.set_value

I use it all the time. Here are some examples.

frappe.db.set_value("Payment Order",{"name":doc.name},{"status_message":str(response.text)})
frappe.db.set_value("Payment Order",{"name":doc.name},{
            "payout_id":str(jsonResponse["id"]),
            "payout_status":str(jsonResponse["status"]),
            "utr":str(jsonResponse["utr"]),
            "status_message":str(jsonResponse["status_details"]["description"]),
            })
frappe.db.set_value("Item",{"name":items.item_code},{"disabled":1})
frappe.db.set_value("Item Reorder",{"parent":item.name,"warehouse":frappe.db.get_value("Warehouse",{"warehouse_name":warehouse.warehouse_name})},{
                    "warehouse_reorder_level":0,
                    "warehouse_reorder_qty":1,
                    "material_request_type":str(request_type),
                })
frappe.db.set_value("Item Reorder",{"parent":df_eoq["item_code"][row],"warehouse":df_eoq["warehouse"][row]},{"warehouse_reorder_level":df_eoq["reorder_point"][row], "warehouse_reorder_qty":df_eoq["eoq"][row]})
1 Like

db.set_value maps 1-1 with a single SQL update query. To start with there isn’t much else to it apart from what signature of this method says.

frappe.db.set_value(doctype, document_name, field, value)

Is same as

update doctype set field = value where name = document_name;

that’s it.

It also supports multiple fields as dict instead of field-value pair and filters instead of a single name but that’s separate story.

1 Like

Thanks For your Replay, But My Requirement is that I want to Filter by Field name Which is going to be updated from old to new Value. so basically in the where condition I want to add the field name not the document_name

Okay Thanks for Your Efforts.

What if I want to Update in Child Tabel Field using db.set_value

This is how I use it. First fetch the document using frappe.get_doc() and then use doc.append()

Here is an example:

def create_payment_orders_hourly():
    for party_list in frappe.db.get_list("Payment Request",fields=["party"],filters=[["Status","=","Initiated"],["transaction_date","=",frappe.utils.nowdate],["payment_request_type","=","Outward"], ["reference_doctype","=","Purchase Order"],["hold","=",0]],group_by="party"):
        doc = frappe.get_doc({
            "doctype":"Payment Order",
            "payment_order_type":"Payment Request",
            "party":party_list.party,
            "company_bank_account":frappe.db.get_value("Bank Account",{"is_automatic_payout_account":1},"name"),
            })
        for payment_request in frappe.db.get_list("Payment Request",fields=["name","party","reference_doctype","reference_name","grand_total","bank_account"],filters=[["Status","=","Initiated"],["transaction_date","=",frappe.utils.nowdate],["payment_request_type","=","Outward"],["party","=",party_list.party],["reference_doctype","=","Purchase Order"]]):
            doc.append("references",{
                "reference_doctype":payment_request.reference_doctype,
                "reference_name":payment_request.reference_name,
                "amount":payment_request.grand_total,
                "supplier": frappe.db.get_value("Supplier",{"name":payment_request.party}),
                "payment_request":payment_request.name,
                "bank_account":payment_request.bank_account
                })
        bank_details = frappe.db.get_value ("Supplier",{"name":doc.party},["account_name","bank_account_number","ifsc", "contact_name", "mobile", "email"],as_dict=True)
        doc.account_name = bank_details.account_name
        doc.account_number = bank_details.bank_account_number
        doc.branch_code = bank_details.ifsc
        doc.contact_name = bank_details.contact_name
        doc.email = bank_details.email
        doc.mobile = bank_details.mobile
        doc.total_amount = 0
        amount = 0
        for line in doc.get("references"):
            amount = line.amount
            doc.total_amount = amount + doc.total_amount
        doc.save()
        doc.submit()