Server side script run on save of record

I have a usecase where i need create instance of the table on save of another table.
Two tables are hopsital_appointment and appointment.
hospital appointment table has 4 column name,date,gender,type.
appointment table has 2 colums name,date.
Now I need write server side script which should run on save of hospital appointment table and it has to create instance of appointment table. name and date column of hospital values should be copied from the hospital appointment table.

@praajna

Please try the below code in custom script, after making necessary changes.

frappe.ui.form.on('YourDoctype', {
    validate: function(frm) {
        if (frm.doc.hospital_appointment) {
            frm.doc.appointment = []; 

            $.each(frm.doc.hospital_appointment, function(i, hospital_appointment) {
                var appointment = frappe.model.add_child(frm.doc, 'Appointment', 'appointment');
                appointment.name = hospital_appointment.name;
                appointment.date = hospital_appointment.date;
            });

            frm.refresh_field('appointment');
        }
    }
});

Hi @Rahul-R

tried with the code which you sent Iā€™m getting error

Syntax Error

Script: Invalid python code on line 30

validate: function(frm) {

Hello,

This is a client script, not a server script.

Please put the code in the client script doctype.
and set the field name and doctype according.

Thank You!

1 Like