Server Calls (AJAX)

frappe.call

frappe.call(method, args)

Makes an AJAX request to the server, where the method which is the dotted path to a whitelisted Python method, is executed and it's return value is sent as the response.

// call with no parameters
frappe.call('ping')
 .then(r => {
 console.log(r)
 // {message: "pong"}
 })

// call with a single parameter
frappe.call('frappe.core.doctype.user.user.get\_role\_profile', {
 role\_profile: 'Test'
}).then(r => {
 console.log(r.message)
})

// call with all options
frappe.call({
 method: 'frappe.core.doctype.user.user.get\_role\_profile',
 args: {
 role\_profile: 'Test'
 },
 // disable the button until the request is completed
 btn: $('.primary-action'),
 // freeze the screen until the request is completed
 freeze: true,
 callback: (r) => {
 // on success
 },
 error: (r) => {
 // on error
 }
})

frappe.db.get_doc

frappe.db.get\_doc(doctype, name, filters)

Returns the Document object of doctype and name. If name is not provided, gets the first document matched by filters.

// get doc by name
frappe.db.get\_doc('Task', 'TASK00002')
 .then(doc => {
 console.log(doc)
 })

// get doc by filters
frappe.db.get\_doc('Task', null, { status: 'Open' })
 .then(doc => {
 console.log(doc)
 })

frappe.db.get_list

frappe.db.get\_list(doctype, { fields, filters })

Returns a list of records of doctype with fields and filters.

frappe.db.get\_list('Task', {
 fields: ['subject', 'description'],
 filters: {
 status: 'Open'
 }
}).then(records => {
 console.log(records);
})

frappe.db.get_value

frappe.db.get\_value(doctype, name, fieldname)

Returns a document's field value or a list of values.

// single value
frappe.db.get\_value('Task', 'TASK00004', 'status')
 .then(r => {
 console.log(r.message.status) // Open
 })

// multiple values
frappe.db.get\_value('Task', 'TASK00004', ['status', 'subject'])
 .then(r => {
 let values = r.message;
 console.log(values.status, values.subject)
 })

// using filters
frappe.db.get\_value('Task', {status: 'Open'}, 'subject')
 .then(r => {
 let values = r.message;
 console.log(values.subject)
 })

frappe.db.get\single\value

frappe.db.get\_single\_value(doctype, field)

Returns a field value from a Single DocType.

frappe.db.get\_single\_value('System Settings', 'time\_zone')
 .then(time\_zone => {
 console.log(time\_zone);
 })

frappe.db.set_value

frappe.db.set\_value(doctype, docname, fieldname, value, callback)

Sets a document's property using frappe.get\_doc and doc.save on server.

// update a field's value
frappe.db.set\_value('Task', 'TASK00004', 'status', 'Open')
 .then(r => {
 let doc = r.message;
 console.log(doc);
 })

// update multiple fields
frappe.db.set\_value('Task', 'TASK00004', {
 status: 'Working',
 priority: 'Medium'
}).then(r => {
 let doc = r.message;
 console.log(doc);
})

frappe.db.insert

frappe.db.insert(doc)

Insert a new document.

frappe.db.insert({
 doctype: 'Task',
 subject: 'New Task'
}).then(doc => {
 console.log(doc);
})

frappe.db.count

frappe.db.count(doctype, filters)

Returns number of records for a given doctype and filters.

// total number of Task records
frappe.db.count('Task')
 .then(count => {
 console.log(count)
 })

// total number of Open Tasks
frappe.db.count('Task', { status: 'Open' })
 .then(count => {
 console.log(count)
 })

frappe.db.delete_doc

frappe.db.delete\_doc(doctype, name)

Delete a document identified by doctype and name.

frappe.db.delete\_doc('Task', 'TASK00004')

frappe.db.exists

frappe.db.exists(doctype, name)

Returns true if a document record exists.

frappe.db.exists('Task', 'TASK00004')
 .then(exists => {
 console.log(exists) // true
 })
Discard
Save

On this page