How to Fetch missing Table values on Server

hello, i was wondering how i can fetch the missing table values or there must be extra fields in the child table for it to work

example SO items, for example:

@frappe.whitelist(allow_guest=True)
def make_so():
    so = frappe.get_doc(
        {
            "doctype": "Sales Order",
            "company": "Company 1",
            "customer": "Customer 1"
        }
    )

    so.append("items",
              {
                  "item_code": "iPhone 13",
                  "qty": 1,
                  "rate": 5,
                  "uom": "Box"
              }
              )

    so.run_method("set_missing_values")
    # this work perfectly
    print(so.customer_name)

    # not working for child tables. amount is 0
    print(so.items[0].amount)


i mean something like js frappe.model.set_value() work on tables, we set item code and other fields fetched automatically

1 Like

Hi @PyJumper,

I think, Please try it.

@frappe.whitelist(allow_guest=True)
def make_so():
    so = frappe.get_doc(
        {
            "doctype": "Sales Order",
            "company": "Company 1",
            "customer": "Customer 1"
        }
    )

    so.append("items",
              {
                  "item_code": "iPhone 13",
                  "qty": 1,
                  "rate": 5,
                  "uom": "Box"
              }
              )

    # set missing values for both the parent and child tables
    so.set_missing_values()
    for item in so.items:
        item.set_missing_values()

    # now the amount should be populated
    print(so.items[0].amount)

Thank You!

1 Like

Hi @NCP thanks for the tip but sadly that did not work nor the run_method style work

AttributeError: ‘SalesOrderItem’ object has no attribute ‘set_missing_values’

1 Like

Hmm :thinking:,

remove it

try it, maybe works.

    for item in so.items:
        item.rate = 5
        frappe.model.meta.get_field("Sales Order Item", "rate").set(item, item.rate)
        item.qty = 1
        frappe.model.meta.get_field("Sales Order Item", "qty").set(item, item.qty)
        item.uom = "Box"
        frappe.model.meta.get_field("Sales Order Item", "uom").set(item, item.uom)

    # now the amount should be populated
    print(so.items[0].amount)

Thanks!

AttributeError: module ‘frappe.model.meta’ has no attribute ‘get_field’

thanks but i found that running the following methods did the trick:

so.run_method(“set_missing_values”)
so.run_method(“calculate_taxes_and_totals”)

Note: there are also other methods which im not sure i need to run or not like:
so.run_before_save_methods()

1 Like