Try ERPNext Buy Support Partners Foundation

Updata database value from custom script

Hi All,

Can i use frappe.db.set_value in custom script??

Regards,
Vishakha

yes you can use in this way

frappe.model.set_value(doctype, doc.name, fieldname, value);

Hi ,
This have not worked for me.
I my case i am calculating scores from child table and storing ot in Total KRA Score but the problem is this script runs on refresh trigger and because of this there is cache proble and it is taking one extra click on save to reflect the exact numbers on report bulider.

In above images in the table the TOtal KRA score is showing 58 while in the report it is showing 54 which is old data and when i save this doc one more thim then it reflect the 58 value.
I have tried lots of ways to solve it but did not work. Kindly help me out if you have any solution.

Regards,
Vishakha

when you create reports its take the value from database but you setting that value from front end side so its just for showing purpose… the new calculated value will be reflect in report after saving the doc.
so why you not doing frm.save() from you custom script after setting the value.
so it will not require manual save after setting the value.

This script is working on refresh so, when i use frm.save() after setting the value it will contineously save the doc again and again due to refresh.

are you calculating from the child table field?
if yes the its should be return on trigger of child table instead of refresh.

calculation is running on childtable yes… but total score is on parent table thats why on refresh

No need it should be in child table trigger. i have same scenario.

can you share the code snnipet for better understanding…?
i have tried this but did not work…

set_receipt_type_value: function(frm) {
var total= 0;
for (var i=0, l=(frm.doc.items || []).length; i<l; i++){
var row = this.frm.doc.items[i];
total= total + row.field_name
}
refresh_field(“items”);
frappe.model.set_value(frm.doc, fieldname, total);
frm.refresh_field(“fieldname”)
},

Hi
If you want to set value for child table field then use

frappe.model.set_value(child_table.doctype, child_table_doc.name, ‘field_name_of_child_table’, ‘value_to_assign’);
refresh_field(‘child_table_name’)

Or if you want to set value on form then use

frm.set_value(‘field_name’, ‘value_to_assign’)
refresh_field(‘field_name’)

Try this,

frappe.call({
“method”: “frappe.client.set_value”,
args:{
“doctype”:“DoctypeName”,
“name”: “formname”,
“fieldname”: “field_db_name”,
“value”: value
},
callback: function(data){}
})