$npx -y skills add Impertio-Studio/Frappe_Claude_Skill_Package --skill frappe-impl-reportsUse when building Script Reports, Query Reports, dashboard charts, or Number Cards in ERPNext. Prevents empty report output from wrong column definitions, broken filters, and unoptimized SQL in large datasets. Covers Report Builder, Script Report (Python + JS), Query Report, Repo
| 1 | # Frappe Report Building |
| 2 | |
| 3 | ## Quick Reference |
| 4 | |
| 5 | | Report Type | Best For | Access | Files | |
| 6 | |---|---|---|---| |
| 7 | | Query Report | Simple SQL queries | System Manager only | SQL in DocType or `.py` | |
| 8 | | Script Report | Complex logic, charts | Administrator + Dev Mode | `.py` + `.js` | |
| 9 | | Report Builder | End-user ad-hoc reports | Any permitted user | UI only | |
| 10 | | Prepared Report | Large datasets (>100k rows) | Same as source report | Background job | |
| 11 | |
| 12 | ## Decision Tree: Which Report Type? |
| 13 | |
| 14 | ``` |
| 15 | Need a report? |
| 16 | ├─ End user builds it themselves? → Report Builder |
| 17 | ├─ Simple SQL with no Python logic? → Query Report |
| 18 | ├─ Complex logic / charts / summary? → Script Report |
| 19 | │ └─ Dataset > 100k rows or timeout? → Add prepared_report = True |
| 20 | └─ Real-time KPI on workspace? → Number Card or Dashboard Chart |
| 21 | ``` |
| 22 | |
| 23 | ## 1. Creating a Script Report |
| 24 | |
| 25 | ### File Structure |
| 26 | |
| 27 | ``` |
| 28 | my_app/my_module/report/sales_summary/ |
| 29 | ├── sales_summary.json # Report DocType definition |
| 30 | ├── sales_summary.py # Python: execute() function |
| 31 | └── sales_summary.js # JavaScript: filters + config |
| 32 | ``` |
| 33 | |
| 34 | ALWAYS create via Desk: Report > New > Script Report > set "Is Standard = Yes" in Developer Mode. |
| 35 | |
| 36 | ### Python: The execute() Function |
| 37 | |
| 38 | ```python |
| 39 | # sales_summary.py |
| 40 | import frappe |
| 41 | from frappe import _ |
| 42 | |
| 43 | def execute(filters=None): |
| 44 | columns = get_columns() |
| 45 | data = get_data(filters) |
| 46 | chart = get_chart(data) |
| 47 | report_summary = get_summary(data) |
| 48 | return columns, data, None, chart, report_summary |
| 49 | |
| 50 | def get_columns(): |
| 51 | return [ |
| 52 | {"fieldname": "customer", "label": _("Customer"), "fieldtype": "Link", |
| 53 | "options": "Customer", "width": 200}, |
| 54 | {"fieldname": "total", "label": _("Total"), "fieldtype": "Currency", |
| 55 | "options": "currency", "width": 120}, |
| 56 | {"fieldname": "qty", "label": _("Qty"), "fieldtype": "Int", "width": 80}, |
| 57 | {"fieldname": "posting_date", "label": _("Date"), "fieldtype": "Date", "width": 100}, |
| 58 | ] |
| 59 | |
| 60 | def get_data(filters): |
| 61 | conditions = get_conditions(filters) |
| 62 | return frappe.db.sql(""" |
| 63 | SELECT |
| 64 | si.customer, SUM(si.grand_total) as total, |
| 65 | SUM(si.total_qty) as qty, si.posting_date |
| 66 | FROM `tabSales Invoice` si |
| 67 | WHERE si.docstatus = 1 {conditions} |
| 68 | GROUP BY si.customer |
| 69 | ORDER BY total DESC |
| 70 | """.format(conditions=conditions), filters, as_dict=True) |
| 71 | |
| 72 | def get_conditions(filters): |
| 73 | conditions = "" |
| 74 | if filters.get("from_date"): |
| 75 | conditions += " AND si.posting_date >= %(from_date)s" |
| 76 | if filters.get("to_date"): |
| 77 | conditions += " AND si.posting_date <= %(to_date)s" |
| 78 | if filters.get("company"): |
| 79 | conditions += " AND si.company = %(company)s" |
| 80 | return conditions |
| 81 | ``` |
| 82 | |
| 83 | **Return value order** (positional — ALWAYS maintain this order): |
| 84 | |
| 85 | | Position | Name | Type | Required | |
| 86 | |---|---|---|---| |
| 87 | | 1 | `columns` | list[dict] | YES | |
| 88 | | 2 | `data` | list[dict] or list[list] | YES | |
| 89 | | 3 | `message` | str or None | NO | |
| 90 | | 4 | `chart` | dict or None | NO | |
| 91 | | 5 | `report_summary` | list[dict] or None | NO | |
| 92 | | 6 | `skip_total_rows` | bool | NO | |
| 93 | |
| 94 | ### JavaScript: Filters |
| 95 | |
| 96 | ```javascript |
| 97 | // sales_summary.js |
| 98 | frappe.query_reports["Sales Summary"] = { |
| 99 | filters: [ |
| 100 | { |
| 101 | fieldname: "company", |
| 102 | label: __("Company"), |
| 103 | fieldtype: "Link", |
| 104 | options: "Company", |
| 105 | default: frappe.defaults.get_user_default("company"), |
| 106 | reqd: 1 |
| 107 | }, |
| 108 | { |
| 109 | fieldname: "from_date", |
| 110 | label: __("From Date"), |
| 111 | fieldtype: "Date", |
| 112 | default: frappe.datetime.add_months(frappe.datetime.get_today(), -1), |
| 113 | reqd: 1 |
| 114 | }, |
| 115 | { |
| 116 | fieldname: "to_date", |
| 117 | label: __("To Date"), |
| 118 | fieldtype: "Date", |
| 119 | default: frappe.datetime.get_today(), |
| 120 | reqd: 1 |
| 121 | }, |
| 122 | { |
| 123 | fieldname: "customer_group", |
| 124 | label: __("Customer Group"), |
| 125 | fieldtype: "Link", |
| 126 | options: "Customer Group", |
| 127 | depends_on: "eval:doc.company" |
| 128 | } |
| 129 | ], |
| 130 | formatter: function(value, row, column, data, default_formatter) { |
| 131 | value = default_formatter(value, row, column, data); |
| 132 | if (column.fieldname === "total" && data.total > 100 |