Struggling to Get Child Table Data to Client Script

Hello,

I have a client script for the Batch DocType which on pressing a custom button on the form, gets values from the DocType and prints them to a label printer. Works well and quite happy.

However, I am struggling to find a way to use the same approach on Purchase Receipt DocType, to get information from the Purchase Receipt Items child table, and send them to the client script to print.

I have looked at the docs but it isn’t clear, I’ve tried following some YT videos on child tables but they don’t seem to work in v14.

Can anyone tell me how this can be achieved, or a good source of info on how to achieve this?

Thanks,
Paul

hello, you can use frappe.call to call server custom function or the core pre made api’s for example:

frappe.call(
    {
        method: "frappe.client.get",
        args: {doctype: "Purchase Receipt", name: "MAT-PRE-2023-00001"},
        callback: function(r) {
            if(r.message){
                let purchase_receipt = r.message;

                console.log(purchase_receipt.items)
            }
        }
    }
)

you can check client.py for different api types

Thanks, I have very little Python knowledge at the moment but hopefully find something. Is it easy enough to transfer the data from the server script to client script?

Welcome, you dont need to write any python code, i just mentioned client.py so that you can get more api options examples for now you can use the script i gave to fetch any doctype with its child tables data, JS will receive data inside r.message

I’m trying to save the script but get the following error
image

Any idea what it relates to?

Sorry, didn’t realise it was for code to save in the client script, I thought it was a server script.

1 Like

@PyJumper I have got the code working but not sure of how to filter it and keep it to the info associated to the parent DocType only. It’s a bit beyond my current understanding, but I’ll keep trying.

In the meantine I have modified the following code from a YT video by D-codE, to try and display the information I want to get from the child table, but is does not seem to work.

frappe.ui.form.on('Purchase Receipt',  "refresh", function(frm) {
    frm.add_custom_button(__("Print Label"), function(){ 
        // When this button is clicked, do this
        frappe.msgprint(__("Print labels from '{0}'",
    	[frm.doc.naming_series]))
    	for (let row of from.doc.purchase_receipt_item) {
	        frappe.msgprint(__("{0}. Item '{1}' Labels:'{2}'",
            	[row.idx,row.item_code,row.qty]))
    	}
	}
)});

It displays a popup as expected but only with the parent DocType info, the Child table info doesn’t appear.

Does anyone know where I may be going wrong?

Got it, I had for (let row of from.doc.purchase_receipt_item) when it should have been for (let row of frm.doc.items)
So one misspelling for frm and I needed to name the link field in the Parent Doc, not the Child table name.

1 Like

glad it worked for you, you can also ask if you got any other problem we will be glad to help

1 Like