Can we write update query to update table fields in JavaScript custom script code .
Never write an UPDATE query, always use the API
You can change what you want in the validate
event of your controller.
Hi rmehta
i am trying to update a custom field with salesorder no in Quotation Document when Sales Order is submitted.
Can i use frappe REST API to update this field using “PUT” method. But i am trying to access API through browser then i am not able to find this “Custom Field”.
Can i update custom field of another document using REST API ?
i am executing this code in sales Order Document
quotations = this.cur_frm.doc.sales_order_details;
sales_no = this.cur_frm.doc.name;
$.ajax({
url: ‘api/resource/Quotation/’ + quotations[0].prevdoc_docname,
type: ‘PUT’,
data: ‘name=’ + quotations[0].prevdoc_docname + ‘&sales_order_no=’ + this.cur_frm.doc.name ,
success: function(res) { alert('PUT completed with status ’ + res.status); }
});
data has to be a JSON object string not a query string
Thanks Pratik
But custom filed is not displayed using API. So i am getting this Error.
“NetworkError: 500 INTERNAL SERVER ERROR - http://localhost:8080/api/resource/Quotation/QTN-00004”
Request Error
site: erpnext.erpnext-vm
form: {‘sales_order_no’: u’SO-00026’}
Traceback (innermost last):
File “/home/erpnext/frappe-bench/apps/frappe/frappe/app.py”, line 52, in application
response = frappe.api.handle()
File “/home/erpnext/frappe-bench/apps/frappe/frappe/api.py”, line 71, in handle
data = json.loads(frappe.local.form_dict.data)
File “/usr/lib/python2.7/json/init.py”, line 326, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
All this error is saying is that system is unable to decode data as json object. Encode data as json string before sending it from client
Hi Bhupesh
i m using this code, yes i understood error but not able to indentify solution.
here “sales_order_no” is a custom field.
$.ajax({
url: ‘api/resource/Quotation/QTN-00004’,
method: ‘PUT’,
data: { sales_order_no: “SO-00001”},
dataType: “json”,
success: function(res) {
alert('PUT completed with status ’ + res.status);
}
}).done(function( msg ) {
alert( “Data :” + msg );
});
Assuming you are using any modrrn browser, replace your data param with following value.
“”"
darta: JSON.stringify({ sales_order_no: “SO-00001”});
“”"
** sorry for brevity, using handheld.
Hi Bhupesh
Still getting same Error.
Request Error
site: erpnext.erpnext-vm
form: {‘{“sales_order_no”:“SO-00001”}’: u’'}
Traceback (innermost last):
File “/home/erpnext/frappe-bench/apps/frappe/frappe/app.py”, line 52, in application
response = frappe.api.handle()
File “/home/erpnext/frappe-bench/apps/frappe/frappe/api.py”, line 71, in handle
data = json.loads(frappe.local.form_dict.data)
File “/usr/lib/python2.7/json/init.py”, line 326, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer