Create a new record on another doctype after save

Hello Everyone,
I am trying to automatically create Interview Feedback records once an Interview record is created. I have seen topics on the subject but the difference is that I would like to create the Interview Feedback records based on the Interviewers child table as shown below:

So if there are 5 Interviewers, it would create 5 Interview Feedback records, based on the Interviewers listed on the child doctype

Thank you in advance

Following steps:
create a custom app.
in the hooks.py, add a doc_events hook for the interview record. probably better on “submit” than on create…
add a custom method in you app that is called from the hook where the feedback docs are created.

while thinking it through: you probably could also handle it as a front end JS script… gut feeling still says python backend.

The easiest way would be creating a server script.

Hi Syd,

I believe your structure is something like this?

  • Parent Document
    • A child Document named 'Interviewer'
    • A child Document named 'Interview Feedback'

This is a somewhat tricky exercise. The problem is that a Child Document’s controller methods are never triggered automatically by the Frappe framework.1

So. If you want to automatically create one child Interview Feedback for each child Interviewer, you have to perform some additional work:

  1. On the -parent- Document, you must modify a controller method. You have several options. My advice would be using 'before_validate’. Why do I recommend this?

    First, because ‘before_validate’ is called both when a Parent inserts, but also when a Parent updates. This is helpful. You only need to make one modification.

    • If you create a new Parent document with new Interviewers, your code will run.
    • If you add “Interviewers” to an existing Parent document, your code will run.

    Second, ‘before_validate’ is called prior to ‘validate’. So you’ll be adding new data to your document -before- validation happens. This is a good thing. You wouldn’t want to insert data that isn’t examined and validated.

  2. In your “before_validate”, you must detect whether the Interviewer row is new, or not. You can achieve that with something like this:

class MyParentDocument():

    before_validate(self):
        for each_row in self.get("interviewer"):
            if each_row.get("__islocal"):  # if true, then the child row is New
                print("Add your code here, to append a new 'Interviewer Feedback' to the parent.")
            else:
               pass  # do nothing, because the Interviewer is not new.

Hopefully this helps. As Moe and Türker mentioned, you can patch the code using “hooks.py”, a Server Script, or a few other ways.

1 Note: There actually is one exception: child controllers are called if you call their APIs with an external REST client. But that’s not important in this thread.)

1 Like

Thank you Bran and Moe,
However, I am very new to programming and Frappe. I would have to do a lot of reading and practicing before I could implement these ideas. I thought the solution existed somewhere and I would just contextualize the code for my use

Hello @brian_pond, @TurkerTunali, @moe01325

I have been reading up on and working with ERPNext and I think I want to tackle this now. I have however simplified it. I no longer want to create an interview feedback per the number of interviewers on the child table. Instead, I have designated only one interviewer to give the feedback using the field “main_interviewer”. I have also created a Server Script on the Interview doctype (on After Save) but it is throwing an error

Please find below the code"

interview_feedback = frappe.get_doc(dict(
	doctype = 'Interview Feedback',
	interview = doc.name,
	interview_round = doc.interview_round,
	job_applicant = doc.job_applicant,
	job_cadre = doc.job_position,
	interviewer = doc.main_interviewer,
	result = 'Pending'
)).insert()

Any help will be highly appreciated

Just a guess, but it appears the DocType 'Interviewer' has a DocField 'Interview Feedback' that is mandatory?

When you save() the parent 'Interview', the children 'Interviewer' documents are also saved. The software examines mandatory fields, ensuring a value exists for each.

Perhaps after you insert() your interview_feedback, you need to update the 'Interviewer', and assign its name?

Yes you’re right, that’s why I am not using the interview feedback child table at all. I’m using the link field “main_interviewer” instead

I think the problem may be coming from doc.name on the interview doctype. On Interview Feedback, there is a mandatory link field to interview. I do not know if doc.name is the best way to pass that field to the interview feedback doctype