How to assign value to custom field

Dear Team,

How to assign value to doc.total_value with doc.domestic_sales|sum(attribute=‘total_insured’) ?

Please help…

use cur_frm.set_value(field, value)

2 Likes

Script Type: Client

Script:

cur_frm.set_value(total_value, doc.domestic_sales|sum(attribute=‘total_insured’)) ?


Here in my case “Field” is doc.total_value & “value” is doc.domestic_sales|sum(attribute=‘total_insured’)

its not working…

@Ram_Gopal_Rao
you can try this

cur_frm.set_value(field, value_one || value_two);

@Sangram
IN my case Value is total of table field

@Ram_Gopal_Rao
where is the issue you getting Value or not
or problem in setting value to field.

@Ram_Gopal_Rao can you explain what is done by this script doc.domestic_sales|sum(attribute='total_insured')

If you want to set value in form then you can use use cur_frm.set_value(field, value)

Also note that, field name total_value will be in double quote.
You can test this script

cur_frm.set_value("total_value",100);

IN doc.domestic_sales|sum(attribute=‘total_insured’) domestic_sales is table, this command will calculate sum of “total_insured”. This command works in jinja {{ doc.domestic_sales|sum(attribute=‘total_insured’) }}

@Ram_Gopal_Rao
Jinjha code will not work in Javascript, You need to write JavaScript code.
e.g

var total=0;
   for(i=0; i < frm.doc.domestic_sales.length;i++){
       total = total + frm.doc.domestic_sales[i].total_insured 
    } 
cur_frm.set_value("total_value",total);

Do I need to add at the top of custom script

**Script Type: Client

Script:

?**

@kolate_sambhaji

      Do I need to add at the top of custom script

**Script Type: Client

Script:

?**

frappe.ui.form.on(‘Your Doctype Name’, ‘validate’, function(frm){
var total=0;
for(i=0; i < frm.doc.domestic_sales.length;i++){
total = total + frm.doc.domestic_sales[i].total_insured
}
cur_frm.set_value(“total_value”,total);
});

Note: Please refer this link to understand how to add custom script if you are not familiar with Frappe Framework
http://frappe.github.io/erpnext/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/

@kolate_sambhaji

Thank you soo much. It worked. :relaxed:

1 Like