@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); }
});