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:
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.
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:
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.
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.
1Note: 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.)
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
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
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?
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