Need to add a custom field with calculations

177 views
Skip to first unread message

HF

unread,
Oct 10, 2012, 4:58:27 AM10/10/12
to erpnext-dev...@googlegroups.com
How can I add a custom field to the sales order item document that can hold a mathematical equation of another fields ?

ex.:  custom field 1 = fld1  and custom field 2 = fld2  and custom field 3 = fld3     I need the fld3 to hold this value   ==  fld1*fld2/1000  just after entering the values in fld1 and fld2

is that possible?  How ?

Nabin Hait

unread,
Oct 10, 2012, 5:21:28 AM10/10/12
to erpnext-dev...@googlegroups.com
Dear HF,

You can do that via Document --> New --> Custom Script.

In custom script try the following:

Script Type: Server

Script:

def custom_validate(self):
  from webnotes.utils import flt
  self.doc.custom_field3 = flt(self.doc.custom_field1)*flt(self.doc.custom_field2)/1000



--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To post to this group, send email to erpnext-dev...@googlegroups.com.
To unsubscribe from this group, send email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/erpnext-developer-forum/-/OZD91z8Qy9YJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Regards,
Nabin Hait

Nabin Hait

unread,
Oct 10, 2012, 5:30:47 AM10/10/12
to erpnext-dev...@googlegroups.com
Dear HF,

If you want instant calculation on entering the value of field1 and field2, the try the following:

Script Type: Client

Script:

cur_frm.cscript.custom_field1 = function(doc, cdt, cdn) {
  if (doc.custom_field1 && doc.custom_field2) {
    doc.custom_field3 = doc.custom_field1*doc.custom_field2/1000;
    refresh_field('custom_field3');
  }
}

cur_frm.cscript.custom_field2 = cur_frm.cscript.custom_field1;
--
Regards,
Nabin Hait

Nabin Hait

unread,
Oct 10, 2012, 5:38:38 AM10/10/12
to erpnext-dev...@googlegroups.com
Slight modification in client side code:


cur_frm.cscript.custom_field1 = function(doc, cdt, cdn) {
  doc.custom_field3 = flt(doc.custom_field1)*flt(doc.custom_field2)/1000;
  refresh_field('custom_field3');
}
cur_frm.cscript.custom_field2 = cur_frm.cscript.custom_field1;


--
Regards,
Nabin Hait

HF

unread,
Oct 10, 2012, 6:40:25 AM10/10/12
to erpnext-dev...@googlegroups.com
Hi Nabin;

It doesn't work !

Anand Doshi

unread,
Oct 10, 2012, 7:22:20 AM10/10/12
to erpnext-dev...@googlegroups.com
Hi HF,

You will need to do a Tools > Clear Cache and Refresh after saving custom script for it to reflect.

Also, please keep the browser's console open to keep track of any errors in the script.

Thanks,
Anand.

On 10-Oct-2012, at 4:10 PM, HF <h.far...@gmail.com> wrote:

Hi Nabin;

It doesn't work !


--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To post to this group, send email to erpnext-dev...@googlegroups.com.
To unsubscribe from this group, send email to erpnext-developer...@googlegroups.com.

HF

unread,
Oct 10, 2012, 8:58:29 AM10/10/12
to erpnext-dev...@googlegroups.com
Hi Nabin;

I did all that, but nothing happened, and no errors.

Rushabh Mehta

unread,
Oct 10, 2012, 12:21:12 PM10/10/12
to erpnext-dev...@googlegroups.com
Hi HF,

The script looks okay.

Here is a slightly more commented version


Please check the field names.

- Rushabh


W: https://erpnext.com
T: @rushabh_mehta

On 10-Oct-2012, at 6:28 PM, HF <h.far...@gmail.com> wrote:

Hi Nabin;

I did all that, but nothing happened, and no errors.

--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To post to this group, send email to erpnext-dev...@googlegroups.com.
To unsubscribe from this group, send email to erpnext-developer...@googlegroups.com.

HF

unread,
Oct 11, 2012, 11:00:00 AM10/11/12
to erpnext-dev...@googlegroups.com

Hi Rushabh;

It still did not work, may be because this is a child table (sales order item) ?

Rushabh Mehta

unread,
Oct 11, 2012, 1:01:38 PM10/11/12
to erpnext-dev...@googlegroups.com
Could be the issue. You need to refresh the table field in that case. Sorry its confusing. Ideally there should be a set_value method.

Just use

refresh_field("[table_field]")


On Thu, Oct 11, 2012 at 8:30 PM, HF <h.far...@gmail.com> wrote:

Hi Rushabh;

It still did not work, may be because this is a child table (sales order item) ?

--
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To post to this group, send email to erpnext-dev...@googlegroups.com.
To unsubscribe from this group, send email to erpnext-developer...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages