Interactive Document Management: Implementing Actions and Links in Empress

Introduction

Welcome to this comprehensive guide that delves into the powerful feature of Actions and Links (also known as Connections) added in Empress Version 12.1. This feature provides end users with greater interaction capabilities with their documents, maximizing usability and functionality.

Part 1: Actions

A DocType can have DocType Action that will result in the creation of a button on the DocType View. Supported actions include:

  1. Server Action: This triggers a whitelisted server action.
  2. Route: This redirects to a given route.

Let’s look at how to configure these actions:

Configuring an Action

The configuration of an action requires defining the action type and the corresponding action name or route.

Custom App Action Configuration

To call an action in your own app, you will need to define a Python function decorated with frappe.whitelist:

import frappe

@frappe.whitelist()
def execute_function(*args,**kwargs):
    """
    This function will be executed when the Execute Action Button is clicked
    """
    print('Hello World')
    # The data is transmitted via keyword argument
    print(kwargs)

This code should be placed within your app, typically in a file like apps/my_app/my_app/api.py.

After this, you need to configure the corresponding Action path.

Part 2: Connections (Linked Documents)

The Connections section on the DocType view dashboard is a standard navigation aid. It helps identify which document types are connected to this DocType and allows for the quick creation of new related documents. Internal links (links to DocType in child tables) are also supported.

Configuring Connections

Connections can be configured by specifying the document type, the relationship type, and the reference fieldname.

Connection Configuration via Script

To configure connections for a doctype in your app, create a get_data() function inside <doctype>_dashboard.py. Here’s an example from the sales_invoice_dashboard.py of the Sales Invoice doctype from ERPNext:

from frappe import _

def get_data():
    return {
        "fieldname": "sales_invoice",
        "non_standard_fieldnames": {
            "Delivery Note": "against_sales_invoice",
            "Journal Entry": "reference_name",
            "Payment Entry": "reference_name",
            "Payment Request": "reference_name",
            "Sales Invoice": "return_against",
            "Auto Repeat": "reference_document",
            "Purchase Invoice": "inter_company_invoice_reference",
        },
        "internal_links": {
            "Sales Order": ["items", "sales_order"],
            "Timesheet": ["timesheets", "time_sheet"],
        },
        "internal_and_external_links": {
            "Delivery Note": ["items", "delivery_note"],
        },
        "transactions": [
            {
                "label": _("Payment"),
                "items": [
                    "Payment Entry",
                    "Payment Request",
                    "Journal Entry",
                    "Invoice Discounting",
                    "Dunning",
                ],
            },
            {"label": _("Reference"), "items": ["Timesheet", "Delivery Note", "Sales Order"]},
            {"label": _("Returns"), "items": ["Sales Invoice"]},
            {"label": _("Subscription"), "items": ["Auto Repeat"]},
            {"label": _("Internal Transfers"), "items": ["Purchase Invoice"]},
        ],
    }

Customization of Actions and Links

DocType Actions and Links can be extended via the Customize Form utility.

In summary, the Actions and Links feature in Empress Version 12.1 provides a powerful method for developers to enhance the interaction capabilities of their applications. By effectively implementing this feature, developers can effectively design, modify, and debug their software, offering a more interactive, user-friendly experience.