Client Script Doctype
This page provides detailed documentation for writing custom javascript code for doctypes.

Event triggers

1. Setup (setup): setup is triggered on page load before any other trigger.
1
/**
2
* @param: {string} [tableField] - child table fieldname
3
* @param: {Object} handlers
4
*/
5
frm.onChange({
6
"setup": (frm)=>{
7
/* your code */
8
}
9
})
Copied!
2. Refresh (refresh): refresh is triggered whenever there is any change on the form.
1
/**
2
* @param: {string} [tableField] - child table fieldname
3
* @param: {Object} handlers
4
*/
5
frm.onChange({
6
"refresh": (frm)=>{
7
/* your code */
8
}
9
})
Copied!
3. Fields: you can subscribe to any field present in Doctype, you must use filed label in camel case eg. "Customer Name" "customerName". Fields of child table can also be subscribed. Check the example below for usage.
Field triggers can be used to perform validation as the value in the field is being changed or change the field properties.
1
/**
2
* Subscribe to field triggers
3
*
4
* @param: {string} [tableField] - child table fieldname
5
* @param: {Object} handlers
6
*/
7
frm.onChange({
8
"fieldname": (doc, value)=>{
9
/* your code */
10
},
11
"childtable.fieldname": (doc, value) => {
12
/* subscribe to triggers inside a table */
13
}
14
})
Copied!
4. On Save (onSave): onSave is executed every time user clicks on the save button.
onSave is the perfect trigger to perform your client-side validation on the entire document before anything hits the server.
1
/**
2
* @param: {string} [tableField] - child table fieldname
3
* @param: {Object} handlers
4
*/
5
frm.onChange({
6
"onSave": (frm)=>{
7
/* your code */
8
}
9
})
Copied!
5. Submit (onSubmit): onSubmit is executed when the user submits the document by clicking on the submit button. onSave trigger is executed before onSubmit.
1
/**
2
* @param: {string} [tableField] - child table fieldname
3
* @param: {Object} handlers
4
*/
5
frm.onChange({
6
"onSubmit": (frm)=>{
7
/* your code */
8
}
9
})
Copied!

Field modification

1. Changing field property (setProp): There are the following field properties that can be changed programmatically using setProp method.
    Is Read Only (readOnly)
    Is Required (reqd)
    Is Hidden (hidden)
    Label (label)
    Options (options)
1
/**
2
* @param: {string} fieldname
3
* @param: {string} property name [readOny, reqd, hidden, ...]
4
* @param: {boolean|string|Array} property value
5
*/
6
frm.setProp('lead_name', 'hidden', true)
7
frm.setProp('lead_name', 'label', "New Label For Lead")
8
frm.setProp('lead_name', 'reqd', true)
9
frm.setProp('lead_name', 'readOnly', false)
10
11
// options can be used on Select and Link field types
12
frm.setProp('lead_source', 'options', ["Website","Blog", "Search"])
13
14
Copied!

2. Changing field value (setValue):

1
/*
2
* @param: {string} fieldname
3
* @param: {boolean|string|Object} value must be as per fieldtype
4
*/
5
6
// data, lookup, select
7
frm.setValue('lead_name','Github Inc.')
8
9
// integer, float, currency
10
frm.setValue('lead_value', 10000)
11
12
// table
13
frm.setValue('fieldname',{fieldname:value})
14
Copied!

Form actions

You can programmatically execute form actions as per your use case. These actions can be executed under any form trigger.
1
frm.save()
2
frm.submit()
3
frm.cancel()
4
frm.delete()
Copied!
Last modified 7mo ago