How to fetch table from master?

Hi,

On selecting custom field document.
I need to fetch table data from master.

cur_frm.add_fetch('documents','documents_required','documents_required');

field type is table, and field is linked with document master

Thanks,
Sambhaji

add_fetch won’t work with table type fields.

If you want to add new rows to a Table field (i.e. create documents of a Child type), you could do it via frappe.model.add_child

var newrow = frappe.model.add_child(parent_doc, child_doctype_name, table_fieldname);

and then populate the row with your data and refresh

newrow.field1 = 'foo'
newrow.field2 = 'bar'
# etc...
cur_frm.refresh()
3 Likes

Now, I can successfully fetch table field from master using following code.
But, I need this with custom script.
My code is.

python code:

def apply_documents_required(self):
        if self.documents_required_master:
            shipping_rule = frappe.get_doc("Documents Required Master", self.documents_required_master)
            for condition in shipping_rule.get("documents_required_master"):
                frappe.msgprint(condition.name_of_document)
                doc_req = {
                    "doctype": "Documents Required",
                    "name_of_document": condition.name_of_document,
                    "dispatch_address": condition.dispatch_address
                };
                self.append("documents_required", doc_req)

js code:

documents_required_master: function() {
        var me = this;
        return this.frm.call({
                doc: this.frm.doc,
                method: "apply_documents_required",
                callback: function(r) {
                    if(!r.exc) {
                    }
                }
            })
        }

self.append() does not work in python file in custom app.

Any idea, How to assign table data from py file.

My custom script is,

cur_frm.cscript.docs_required = function(doc, cdt, cdn) {
        var child = locals[cdt][cdn];
    cur_frm.call({
    "method": 'ef_fruits.ef_doc.ef_doc.apply_documents_required',
     "args": {
           "docs_required": doc.docs_required
        },
     callback: function(r) {
           if(!r.exc) {
           }
           }
}) 
};

python code is

@frappe.whitelist(allow_guest=True)
def apply_documents_required(self, docs_required):
        doc_req = []
        if (5==5):
        shipping_rule = frappe.get_doc("Documents Required Master", docs_required)
        frappe.msgprint("Hi @3")
        for condition in shipping_rule.get("documents_required_master"):
            doc_req = {
                "doctype": "Documents Required",
                "name_of_document": condition.name_of_document,
                "dispatch_address": condition.dispatch_address
            };
           self.append("documents_required", doc_req)
                        frappe.msgprint(condition.name_of_document)

I can get master table data in doc_req.
How to assign doc_req to “documents_required”?
“documents_required” is table field.

try this

self.documents_required.extend(doc_req)

This is not working.
I am trying from my csutom app.
I think, problem is, In custom app, I don’t have access to doc object.
I am trying to change custom table field in Sales Order.

opps, dint realise that you are trying to add dict object by iterating. my bad.

try this

self.documents_required.append(doc_req)

The problem is I didn’t get scope of doc object in custom app.
I am trying to call python function from custom script.

Now, I have solved this using hooks.
I have write doc_events on_validate.
The table field are populated when we save document, but I want immediate
effect.
Any suggestion will be helpful for me.

Thanks,
Sambhaji

No reason why it should not work. Maybe you are clearing it elsewhere?

Try running this from your python console. Should work.

Also the “doctype” property is not required if you are using self.append

got this error name 'self' is not defined
Any example to call method with current doctype.
ipython console output:

In [24]:        for value in

doc_master.get(“documents_required_master”):
…: doc_req = {
…: “doctype”: “Documents Required”,
…: “name_of_document”:
value.name_of_document,
…: “dispatch_address”:
value.dispatch_address
…: }
…: print doc_req
…:
{u’name_of_document’: u’PAN’, u’doctype’: u’Documents Required’,
u’dispatch_address’: u’ANAGAr’}
{u’name_of_document’: u’UID’, u’doctype’: u’Documents Required’,
u’dispatch_address’: u’PuNE’}
{u’name_of_document’: u’VID’, u’doctype’: u’Documents Required’,
u’dispatch_address’: u’MUM’}

In [25]: self.append("documents_required", doc_req)

Hi,
did u figure this out? am having similar issue

I got the following working, Dont know if its the right way

quote = frappe.get_doc({"doctype":"Quotation", "customer": self.customer, "territory":"Rest Of The World"})
quote.insert()
quote_item = frappe.get_doc({"doctype":"Quotation Item", "item_code":"Nvidia Tablet", "item_name": "Tablet", "description": "test", "qty":10 })
doc = quote.append("quotation_details", quote_item)
quote.save()

Here is my working code:

def apply_documents_required(self):
if self.documents_required_master:
shipping_rule = frappe.get_doc(“Documents Required Master”, self.documents_required_master)
frappe.msgprint(“from python function”)
for condition in shipping_rule.get(“documents_required_master”):
doc_req = {
“doctype”: “Documents Required”,
“name_of_document”: condition.name_of_document,
“dispatch_address”: condition.dispatch_address
}
if (5==5):
self.append(“documents_required”, doc_req)

You are going good, just one more suggesion use, you can use quote.get("quotation_details") to fetch quotation item data.

1 Like