Reading list Switch to dark mode

    How to create Doctypes in ERPNext

    Updated 15 September 2023

    In this blog, we will learn how to create new doctypes in ERPNext.

    DocType is similar to a Model in other frameworks (like odoo).

    When we create doctypes in ERPNext, then database table with the same name gets created (with tab prefix)

    e.g – If the doctype name is Library -> the db table name will be tabLibrary

    The directory structure for doctypes:-

    Start your headless eCommerce
    now.
    Read More
    library_management/library_management/doctype/__init__.py
    
        library_management/library_management/doctype/library/__init__.py
    
        library_management/library_management/doctype/library/library.js
    
        library_management/library_management/doctype/library/library.json
    
        library_management/library_management/doctype/library/library.py
    
        library_management/library_management/doctype/library/test_library.py

    library.json:- JSON file that defines the doctype attributes and fields(table columns)

    library.js:- Client-side controller for the Form view

    library.py:- Python controller (server side) for library doctype

    test_library.py:- Python Unit Test boilerplate for writing tests

    library.json

    ** (This file creates the doctype) **

    {
      "autoname": "format: PS-{####}",
      "doctype": "DocType",
      "fields": [
        {
          "fieldname": "section1",
          "fieldtype": "Section Break",
          "label": "Section 1"
      },
      {
       "fieldname": "field_1",
       "fieldtype": "Data",
       "in_list_view": 1,
       "label": "Field 1 Label",
       "reqd": 1
      },
      {
       "fieldname": "column_break1",
       "fieldtype": "Column Break",
       "label": ""
      },
      {
       "fieldname": "field_2",
       "fieldtype": "Select",
       "in_list_view": 1,
       "label": "Field 2 Label",
       "options": "option1\option2",
       "reqd": 1
      }
     ],
     "module": "library_management",
     "name": "Doctype Name",
     "owner": "Administrator",
     "permissions": [
      {
       "amend": 0,
       "cancel": 0,
       "create": 1,
       "delete": 1,
       "email": 1,
       "export": 1,
       "if_owner": 0,
       "import": 0,
       "permlevel": 0,
       "print": 1,
       "read": 1,
       "report": 1,
       "role": "System Manager",
       "set_user_permissions": 0,
       "share": 1,
       "submit": 0,
       "write": 1
      },
      {
        "amend": 0,
        "cancel": 0,
        "create": 1,
        "delete": 0,
        "email": 0,
        "export": 0,
        "if_owner": 0,
        "import": 0,
        "permlevel": 0,
        "print": 0,
        "read": 1,
        "report": 0,
        "role": "All",
        "set_user_permissions": 0,
        "share": 0,
        "submit": 0,
        "write": 1
       }
     ],
     "quick_entry": 1,
     "show_name_in_global_search": 1,
     "sort_field": "field1",
     "sort_order": "DESC",
     "title_field": "field1",
     "track_changes": 1,
     "track_seen": 1,
     "track_views": 1
    }

    library.py

    ** (This file contains business logic for particular doctype) **

    import frappe
    
    from frappe.model.document import Document
    
    class Library(Document):
    
        def before_save(self):
    
            # code to execute before saving a library record #

    library.js

    ** (This file contains the js code for the form view) **

    frappe.ui.form.on('Library', {
    
        refresh: function(frm) {                 
    
            // refresh method will run every time a form is refreshed
    
            // frm if the form object
    
        }
    
    });

    Read how to create a new app in ERPNext- App Creation and App installation process in ERPNext

    Single doctypes in ERPNext

    When a DocType has Is Single enabled, it becomes a Single DocType.

    It does not create a new database table. All single values stored in the tabSingles table. You can use it for storing global settings.

    We will use `frappe.db.get_single_value(doctype_name, field_name)` method to get the value of a field from the single doctype.

    NEED HELP?

    Hope you find the guide helpful! Please feel free to share your feedback in the comments below.

    If you still have any issues/queries regarding the same, please raise a ticket at https://webkul.uvdesk.com/en/customer/create-ticket/.

    Also, please explore our Odoo development services & an extensive range of quality Odoo Apps.

    For any doubt, contact us at [email protected].

    Thanks for paying attention!!

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home