Eben van Deventer
Eben van Deventer

Reputation: 1

In Frappe, in a DocType, how can I add a field that lists all the related Documents of the same DocType that share the same Employee

So I am in the process of building a custom doctype "Disciplinary Action" in that relies on other DocTypes quite heavily to complete (Linked and some custom scripting, but that is not the issue).

My current challenge is that, when the user creates a new Document of the "Disciplinary Action" Doctype, once he has completed the "accused" field, I would like the system to populate a list of all previous "Disciplinary Action" documents that have the same "accused" field, listing the "ID" "Document State" and "Final Charges" from those documents in a table. How would I do this, as a Child Table doesn't work because I can't pass the "accused" field to it at runtime?

I have tried a lot, but really can't wrap my head around how this should be done.

Upvotes: 0

Views: 687

Answers (1)

Eben van Deventer
Eben van Deventer

Reputation: 1

So the solution was to use the frappe.model.with_doc function:

if (frm.doc.accused) {
        frappe.call({
            method: 'frappe.client.get_list',
            args: {
                doctype: 'Disciplinary Action',
                filters: {
                    accused: frm.doc.accused,
                    outcome: ['!=',''], //Exclude documents without outcomes
                    name: ['!=', frm.doc.name]  // Exclude the current document
                },
                fields: ['name', 'outcome_date', 'outcome']
            },
            callback: function(e) {
                if (e.message) {
                    // Clear existing child table entries
                    frm.clear_table('previous_disciplinary_outcomes');
                    let action_count = e.message.length;
                    let completed_actions = 0;
                    e.message.forEach(function(row) {
                        frappe.model.with_doc('Disciplinary Action', row.name, function() {
                            let action_doc = frappe.get_doc('Disciplinary Action', row.name);
                            let charges = [];
                            action_doc.final_charges.forEach(function(charge_row) {
                                charges.push(`(${charge_row.code_item}) ${charge_row.charge}`);
                            });
                            // Add the row to previous_disciplinary_outcomes
                            let child = frm.add_child('previous_disciplinary_outcomes');
                            child.disc_action = action_doc.name;
                            child.date = action_doc.outcome_date;
                            child.sanction = action_doc.outcome;
                            child.charges = charges.join('\n');  // Each charge on a new line
                            completed_actions++;
                            // Refresh the form to show the updated child table when all calls are completed
                            if (completed_actions === action_count) {
                                frm.refresh_field('previous_disciplinary_outcomes');
                            }
                        });
                    });
                }
            }
        });
    }

Upvotes: -1

Related Questions