I needed an all purpose document Doctype that reformats format based on which type of document the user was creating. Example: A drawing, test method, standard operating procedure, report or specification may all have different fields that may or may not be needed in the document. I accomplished this by creating a Doctype “Document” and a Doctype “Document Type” and then scripting to hide/show fields based on the Document Type selection.
I used a link field to add Document Type to Document as shown below:
In my document.js file, I used this code on refresh to call a function that shows / hides sections that were or were not needed in the document based on the Document Type selection:
frappe.ui.form.on('Document', {
refresh: function(frm) {
setup_document(frm);
}
});
Where Document is the Doctype and setup_document is the function I am calling to set up the document.
The setup_document function I used is below:
//Add the section break names here//
var setup_document = function(frm) {
frm.toggle_display("com_source_break", frm.doc.document_des == "CAPA");
Where com_source_break is the name of a section break in Doctype Document that I want to show when document type “CAPA” is selected from the Document Type link. In this case, document_des is the name field of Document Type in my Document Doctype. Add as many frm.toggle_display sections here as needed based on how many sections and Document Types you need to handle. As you get more complex with shared sections you may need some conditional operators.
After that, I added this section that is called on when document_des (the name of my Document Type field within Document) is changed :
//add the content of the section breaks here
frappe.ui.form.on("Document", "document_des", function(frm) {
if(!cint(frm.doc.document_des)) {
frm.set_value("complaint_sources", '');
}
setup_document(frm);
});
Add as many frm.set_value lines as needed per your application.
I then created a script report that filters and links these documents by type as my document manager.
I set up the naming script to name the document based on the document type prefix. Each prefix handles its own unique sequence.
I hope this helps if anyone has a similar application. I have found the detailed posts made by the community helpful as I have been getting up to speed developing with the Frappe framework.