Fetch data from another DocType to a child table

Hello, I am new to Custom scripting
I want to fetch data from another DocType to a child table of a DocType.
The Doctype from where I have to fetch data is this one Session History Collection.


Its naming is having name of the customer, date and number
The Doctype to which I want the data fetched is this Session History.

In session history only customer names is shown and when we click on that customer we see a list of all interaction of that customer in Session history collection in this child table.

In this child table each entry should be the a link to session history collection doctype entry containing that customer name.
For example we can see in session history collection there are multiple entries of Customer Devershi Vashistha now in session history all these entries should be present in the child table as a link.
This is what I want to be able to do.
Now what I was thinking is to create a button in Session History which when pressed will check the session history collection doctype and get all the names which has that customer name in it and add it to the child table.
And if possible add in newest first order.
I am trying my best for creating this but I am new so I need help.
Any help is appreciated, Thank you for your time.

@Devershi_Vashistha Considering that the fieldname of the child table in Session History doctype is sessions and the fieldname of the first child table column is session, then the code you want will be:

frappe.ui.form.on('Session History', {
	refresh: function(frm) {
		frappe.db.get_list('Session History Collection', {
            fields: ['name'],
            filters: {name: ['like', frm.doc.name + '%']}
        }).then(records => {
            $.each(records, function(i, r) {
                frm.add_child('sessions', {session: r.name});
            });
        });
	}
});

If it doesn’t work, then please go to Customize Form, select Session History doctype, screenshot, do the same for the child table and then post the screenshots.

@kid1194 Thank you I tried it but it doesn’t seem to work.


@Devershi_Vashistha Based on the screenshots, this code is supposed to work.

frappe.ui.form.on('Session History', {
	refresh: function(frm) {
		frappe.db.get_list('Session History Collection', {
            fields: ['name'],
            filters: {name: ['like', frm.doc.customer + '%']}
        }).then(records => {
            $.each(records, function(i, r) {
                frm.add_child('session', {sessions: r.name});
            });
        });
	}
});

@kid1194 Thank you so much It worked.

@kid1194 It is adding all those fields again and again can we make it so that it doesn’t add what is already there?

@Devershi_Vashistha This code will get executed when you set or change the value of customer and it will also check for duplicates.

frappe.ui.form.on('Session History', {
	customer: function(frm) {
	    var exist = [];
	    if (!frm.is_new()) {
	        $.each(frm.doc.session, function(i, r) { exist.push(r.sessions); });
	    }
		frappe.db.get_list('Session History Collection', {
            fields: ['name'],
            filters: {name: ['like', frm.doc.customer + '%']}
        }).then(records => {
            $.each(records, function(i, r) {
                if (exist.indexOf(r.name) < 0) {
                    exist.push(r.name);
                    frm.add_child('session', {sessions: r.name});
                }
            });
            exist.splice(0, exist.length);
        });
	}
});

Since the code has worked for you, please don’t forget to mark this post as the answer so others can easily find it.

@kid1194 when I am creating a new customer in session history it is working but how can I update it?
because there are no values to change or set

@Devershi_Vashistha This code will get executed when you set or change the value of customer and it will also check for duplicates.
It will also create a button at the top called Update Sessions, when clicked it will update the table.

function updateSession(frm) {
    if (!frm.doc.customer) return;
    let exist = [];
    if (!frm.is_new()) {
        $.each(frm.doc.session, function(i, r) { exist.push(r.sessions); });
    }
	frappe.db.get_list('Session History Collection', {
        fields: ['name'],
        filters: {name: ['like', frm.doc.customer + '%']}
    }).then(records => {
        $.each(records, function(i, r) {
            if (exist.indexOf(r.name) < 0) {
                exist.push(r.name);
                frm.add_child('session', {sessions: r.name});
            }
        });
        exist.splice(0, exist.length);
    });
}
frappe.ui.form.on('Session History', {
	refresh: function(frm) {
	    frm.add_custom_button('Update Sessions', () => { updateSession(frm); });
	},
	customer: function(frm) { updateSession(frm); }
});

@kid1194 Thank you so much for your time it worked perfectly.

1 Like