| name | frappe-syntax-clientscripts |
| description | Use when writing client-side JavaScript for ERPNext/Frappe form events, field manipulation, server calls, or child table handling in v14/v15/v16. Covers exact syntax for frappe.ui.form.on, frm methods, frappe.call, and browser-side validation. Keywords: client script, form event, frm, frappe.call, frappe.ui.form.on, JavaScript, UI interaction, field validation, form event syntax, how to write client script, frm example, frappe.call example.
|
| license | MIT |
| compatibility | Claude Code, Claude.ai Projects, Claude API. Frappe v14-v16. |
| metadata | {"author":"OpenAEC-Foundation","version":"2.0"} |
Frappe Client Scripts Syntax
Client Scripts run in the browser and control all UI interactions in Frappe/ERPNext. Create them via Setup > Client Script or in custom apps under public/js/.
CRITICAL: Client Script validations ONLY apply in the browser form view. API calls and System Console bypass them. ALWAYS pair with Server Scripts for security-critical validation.
Quick Reference
| Action | Code |
|---|
| Set value | frm.set_value('field', value) |
| Get value | frm.doc.fieldname |
| Hide field | frm.toggle_display('field', false) |
| Make mandatory | frm.toggle_reqd('field', true) |
| Make read-only | frm.toggle_enable('field', false) |
| Set field property | frm.set_df_property('field', 'options', [...]) |
| Filter Link field | frm.set_query('field', () => ({filters: {}})) |
| Call server | frappe.call({method: 'path.to.fn', args: {}}) |
| Call doc method | frm.call('method_name', {args}) |
| Prevent save | frappe.throw(__('Error message')) |
| Add button | frm.add_custom_button(__('Label'), callback, group) |
| Add child row | frm.add_child('table', {values}); frm.refresh_field('table') |
| Show alert | frappe.show_alert({message: __('Done'), indicator: 'green'}) |
| Translate string | __('Text') or __('Hello {0}', [name]) |
Event Decision Tree
What do you need to do?
│
├─ One-time setup (queries, formatters)?
│ └─ ALWAYS use setup — runs once per form instance
│
├─ Show/hide fields, add buttons, update UI?
│ └─ ALWAYS use refresh — fires after every load/reload
│
├─ Validate data before save?
│ └─ ALWAYS use validate — use frappe.throw() to block save
│
├─ Modify data right before server save?
│ └─ Use before_save — last chance to change values
│
├─ Run logic after successful save?
│ └─ Use after_save — document is persisted
│
├─ React to a field value change?
│ └─ Use the fieldname as the event name
│
├─ Intercept workflow state change?
│ └─ Use before_workflow_action / after_workflow_action
│
└─ Manipulate DOM after full render?
└─ Use onload_post_render — NEVER use jQuery selectors directly
See references/events.md for complete event list and execution order.
Form Event Registration
frappe.ui.form.on('Sales Order', {
setup(frm) { },
refresh(frm) { },
validate(frm) { },
fieldname(frm) { }
});
frappe.ui.form.on('Sales Order Item', {
qty(frm, cdt, cdn) {
let row = frappe.get_doc(cdt, cdn);
frappe.model.set_value(cdt, cdn, 'amount', row.qty * row.rate);
},
items_add(frm, cdt, cdn) { },
items_remove(frm) { },
items_move(frm) { }
});
Value Manipulation
frm.set_value('status', 'Approved');
frm.set_value({status: 'Approved', priority: 'High'});
let val = frm.doc.fieldname;
let items = frm.doc.items;
Field Properties
frm.toggle_display(['priority', 'due_date'], frm.doc.status === 'Open');
frm.toggle_reqd('due_date', true);
frm.toggle_enable('amount', false);
frm.set_df_property('status', 'options', ['New', 'Open', 'Closed']);
frm.set_df_property('amount', 'read_only', 1);
frm.set_df_property('notes', 'label', 'Internal Notes');
frm.set_intro('This document is pending review', 'orange');
Link Field Filters
frappe.ui.form.on('Sales Order', {
setup(frm) {
frm.set_query('customer', () => ({
filters: { disabled: 0 }
}));
frm.set_query('item_code', 'items', (doc, cdt, cdn) => {
let row = locals[cdt][cdn];
return { filters: { is_sales_item: 1 } };
});
frm.set_query('customer', () => ({
query: 'myapp.queries.get_filtered_customers',
filters: { region: frm.doc.region }
}));
}
});
Server Communication
let r = await frappe.call({
method: 'myapp.api.process_data',
args: { customer: frm.doc.customer },
freeze: true,
freeze_message: __('Processing...')
});
if (r.message) { }
let result = await frm.call('calculate_taxes', { include_shipping: true });
let val = await frappe.db.get_value('Customer', name, 'credit_limit');
let list = await frappe.db.get_list('Sales Order', {
filters: { customer: frm.doc.customer },
fields: ['name', 'grand_total'],
order_by: 'creation desc',
limit: 10
});
Child Table Operations
let row = frm.add_child('items', { item_code: 'ITEM-001', qty: 5 });
frm.refresh_field('items');
frm.clear_table('items');
frm.refresh_field('items');
frm.doc.items.forEach(row => {
row.discount = row.qty > 10 ? 5 : 0;
});
frm.refresh_field('items');
frappe.model.set_value(cdt, cdn, 'amount', row.qty * row.rate);
frm.dirty();
Custom Buttons
refresh(frm) {
if (frm.doc.docstatus === 1) {
frm.add_custom_button(__('Invoice'), () => {
frappe.model.open_mapped_doc({
method: 'erpnext.selling.doctype.sales_order.sales_order.make_sales_invoice',
frm: frm
});
}, __('Create'));
frm.page.set_primary_action(__('Process'), () => {
frm.call('process').then(() => frm.reload_doc());
});
}
if (!frm.is_new() && frm.doc.docstatus === 0) {
frm.add_custom_button(__('Validate'), () => { });
}
}
List View Customization
frappe.listview_settings['Task'] = {
add_fields: ['status', 'priority'],
filters: [['status', '!=', 'Cancelled']],
hide_name_column: true,
get_indicator(doc) {
if (doc.status === 'Open') return [__('Open'), 'orange', 'status,=,Open'];
if (doc.status === 'Closed') return [__('Closed'), 'green', 'status,=,Closed'];
},
button: {
show(doc) { return doc.status === 'Open'; },
get_label() { return __('Close'); },
action(doc) { frappe.call({method: 'myapp.api.close', args: {name: doc.name}}); }
},
formatters: {
priority() { val === ? : val; }
},
() { },
() { }
};
Dialogs and Prompts
frappe.prompt({label: 'Reason', fieldname: 'reason', fieldtype: 'Data'},
(values) => { console.log(values.reason); },
__('Enter Reason')
);
let d = new frappe.ui.Dialog({
title: __('Enter Details'),
fields: [
{label: 'Name', fieldname: 'name', fieldtype: 'Data', reqd: 1},
{label: 'Date', fieldname: 'date', fieldtype: 'Date'}
],
size: 'small',
primary_action_label: __('Submit'),
primary_action(values) { d.hide(); }
});
d.show();
frappe.show_progress(__('Importing'), 45, , ());
Critical Rules
- ALWAYS call
frm.refresh_field('table') after ANY child table modification
- NEVER assign
frm.doc.field = value — ALWAYS use frm.set_value()
- ALWAYS use
__('text') for every user-facing string
- ALWAYS place
set_query in setup — NEVER in refresh
- NEVER use
async: false — it freezes the browser
- ALWAYS check
frm.is_new() before adding action buttons
- NEVER use direct jQuery selectors for field manipulation — use Frappe API
- NEVER store state in global variables — attach to
frm object instead
- ALWAYS check
r.message before using server call responses
- ALWAYS use
frappe.throw() inside validate to block save — NEVER return false in async handlers
See references/methods.md for complete API reference.
See references/examples.md for real-world patterns.
See references/anti-patterns.md for common mistakes.
Related Skills
frappe-impl-clientscripts — Implementation workflows and decision trees
frappe-errors-clientscripts — Error handling and debugging patterns
frappe-syntax-whitelisted — Server-side methods called from client scripts
frappe-syntax-doctypes — DocType field definitions referenced in scripts