Dynamic Doctype Document Format Example

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:

image

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.

2 Likes

Hi @mslake,

thank you for sharing! The only thing I did not understand: is there a limitation in using the depands on for the sections (using eval statements)? I.e. section_capa depends on eval:doc.document_type == 'CAPA'

This is how SAP config(in ERPNext term customize) for almost every transaction(in ERPNext term doctype), for example, in SAP the consultant can config the screen layout/ field selection for every PO type like below
SAP%20IMG
Fig 1: Go to the overall configuration(IMG implementation guide) tree, and locate to the config item(PO type)


Fig2: Config PO type: assign number range and field selection for the PO type
NBF
Fig3 Config field selection(in ERPNext term customize form)

Config number range ,screen layout and others by type is common in SAP for almost every transaction(doctype), e.g Purchase order, production order, sales order, material movement, accounting document etc. for more complicated transaction, even the orgnization such as company, plant is used to differentiate the above mentioned configuration.

There are other thread discussing the feasibility of customizing different form per type, especially for payment, stock entry, sales orders etc, per my opinion using Depends On together with custom script to manipulate distinct form layout by type is not a neat solution, maybe we can refer to above mentioned SAP way of config by type.

is it possible to add one child table: field selection with following fields
field
mandatory
optional
display:check field)
and assign this child table to the document type doctype,
instead of hard code the custom script read the content from the above child table , make it more generic which can be used by every standard doctype, merge it into Core, it will make Frappe framework more flexible and end user/consultant friendly(customizable).