trigger recalculation after customfield is changed

1,001 views
Skip to first unread message

Robert Becht

unread,
Feb 18, 2014, 2:51:34 AM2/18/14
to erpnext-dev...@googlegroups.com


Description of functionality we try to implement.
Copy start en end data custom fields from invoice to Invoice _items (works)
Calc number of days in invoice item (works) 

Fetch a number from a customfield in Item database (works)

Multiply the calculated number of days with the constant from database. (works)

hat does not work??

recalculation if the user changes the inputs. How do we triggger the recalc after input is changed.



How can I add an "onchange" trigger to a form element like a text input field?
In the cookbook (http://wnframework.readthedocs.org/en/latest/client_side_cookbook.html#form-events-triggers) I have found that one can add general form triggers for "refresh",  "onload" and "setup", e.g. "cur_frm.cscript.refresh = function()". However I need an "onchange" trigger for a specific text input field, as described here: http://www.w3schools.com/jsref/event_onchange.asp

I tried the following:
cur_frm.cscript.amount_allocated_on_permit.onchange = function() {
    cur_frm.set_value("total_amount_lawfully_used", 999);
    refresh_field("total_amount_lawfully_used");
}
but nothing happens, the onchange isn't added to the "amount_allocated_on_permit" text field.

I need this, because I want to recompute parts of the form when the user changes some elements. I am trying to avoid adding a "recompute" button, because that spoils the user's interactive experience. Therefore I need a trigger that is called when the user made some changes in the form input.

Thank you,

Reply via mail:

For onchange trigger you should write the function with a little chnage:

 cur_frm.cscript.amount_allocated_on_permit = function() {

cur_frm.set_value("total_amount_lawfully_used", 999);

}

 You do not need to refresh field, as set_value function automatically refresh after setting the value.

Reply back::
Thank you for this suggestion, however this does not trigger when the user has typed new text in the text field called "amount_allocated_on_permit", not even when the user leaves the text field by clicking on another field.
It does trigger once when the form loads, but I can't get it to trigger again.


Nabin Hait

unread,
Feb 18, 2014, 4:52:18 AM2/18/14
to erpnext-dev...@googlegroups.com
Use prefix "custom_"  with the fieldname to call the trigger, it should work.

cur_frm.cscript.custom_amount_allocated_on_permit = function() {

    cur_frm.set_value("total_amount_lawfully_used", 999);

}

--
Note:
 
If you are posting an issue,
1. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
2. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
3. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/37a95255-0c6c-4e07-b4ff-237520a82815%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Robert Becht

unread,
Feb 18, 2014, 9:04:23 AM2/18/14
to erpnext-dev...@googlegroups.com


Hi,

We are somehow stuck with something that seems relatively easy.

Say we have 3 custom fields: AAA, BBB, and CCC, where CCC=AAA*BBB

We fail to trigger the recalculation if the user changes AAA or BBB

Robert

Description of Bas below:


The following is what we try to achieve.
The two numbers circled with red are multiplied in order to get the number in green.
This works when the form loads (the 90 and the 7 are nicely fetched from elsewhere, and the 630 is computed).
When the user changes the 90 to e.g. 100, we wish the 630 to be recomputed. As this computation is "lightweight" (it is only a simple multiplication), the nicest is to have this as the user types. In the worst case, the recomputation can be done when the user leaves the "Amount Allocated on Permit" text field by clicking on another field.
Note that the number recomputes if the user first clicks the "close" arrow ( ^ ) and then re-selects the item to re-open it. But this is not good enough because it causes misunderstandings as to which numbers are actually used when creating the invoice. We want it to be interactive, recomputing this simple multiplication on-the-fly, preferrably as the user types.
We have just tried the _custom option, but that also doesn't have the effect, the number 630 stays as-is.

Thank you in advance for your kind support.





On 18/2/2014 14:04, Becht, R. (Robert, ITC) wrote:

Nabin Hait 

https://groups.google.com/forum/clear.cache.gif

10:52 AM (3 hours ago)

Use prefix "custom_"  with the fieldname to call the trigger, it should work.

 

cur_frm.cscript.custom_amount_allocated_on_permit = function() {

    cur_frm.set_value("total_amount_lawfully_used", 999);

}

- show quoted text -

Robert Becht

unread,
Feb 18, 2014, 9:07:59 AM2/18/14
to erpnext-dev...@googlegroups.com
the screen shot that did not get thru::



On Tuesday, February 18, 2014 8:51:34 AM UTC+1, Robert Becht wrote:

Maxwell Morais

unread,
Feb 18, 2014, 9:09:11 AM2/18/14
to erpnext-dev...@googlegroups.com
You can define a common function for this:

function on_change_aaa_bbb(doc, cdt, cdn){

}


--
Note:
 
If you are posting an issue,
1. We should be able to replicate it at our end. So please give us as much information as you can. Please see it from the point of view of the person receiving the communication.
2. Paste your code at http://pastebin.com or http://gist.github.com and send only the URL via email
3. For sending images, use http://imgur.com or other similar services. Do not send images as attachments. Links are good. Same goes for any file you are going to send.
 
End of Note
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--

Best Regards.

Maxwell Morais
Python Developer powered by Webnotes framework <3

Maxwell Morais

unread,
Feb 18, 2014, 9:11:50 AM2/18/14
to erpnext-dev...@googlegroups.com
Sorry, the last response is wrong!

function on_change_aaa_bbb(doc, cdt, cdn){
    if (doc.aaa && doc.bbb){ 
        doc.ccc = doc.aaa*doc.bbb;
        cur_frm.refresh()
    }
}

cur_frm.custom_aaa = on_change_aaa_bbb;
cur_frm.custom_bbb = on_change_aaa_bbb;

Robert Becht

unread,
Feb 19, 2014, 10:49:45 AM2/19/14
to erpnext-dev...@googlegroups.com


On Tuesday, February 18, 2014 8:51:34 AM UTC+1, Robert Becht wrote:

Robert Becht

unread,
Feb 19, 2014, 10:50:39 AM2/19/14
to erpnext-dev...@googlegroups.com
Hi Maxwell, thanks a lot, we have it up and running, rgds robert


On Tuesday, February 18, 2014 8:51:34 AM UTC+1, Robert Becht wrote:

Javier Wong

unread,
Sep 23, 2014, 10:54:34 PM9/23/14
to erpnext-dev...@googlegroups.com
Dear Experts,

Am trying to achieve a similar result and had a look at this article.

Basically for my scenario, I have a custom field called "discount_amount" in the Sales Invoice Item document. I would like to achieve the following:
- When the field "rate" or "price_list_rate" is changed I want to calculate price_list_rate - rate and put the result in the discount_amount field

I made the following custom script in the Sales Invoice Item DocType just to test if changes in rate would call the function:
cur_frm.cscript.custom_rate = function(doc) {
    result_value = doc.price_list_rate - doc.rate;
    cur_frm.set_value("discount_amount", result_value);
}

Unfortunately nothing seemed to work. I tried a msgprint as well and it did not call the necessary. Would anyone be able to shed some light on this?

Thank you very very much.


Javier

Nabin Hait

unread,
Sep 24, 2014, 1:07:21 AM9/24/14
to erpnext-dev...@googlegroups.com
Try this one:

cur_frm.cscript.custom_rate = function(doc, cdt, cdn) {
    var d = locals[cdt][cdn];
    result_value = d.price_list_rate - d.rate;
    frappe.model.set_value(cdt, cdn, "discount_amount", result_value);
}

--
1. Please be kind and don't send large attachments
2. For issues, features requests use https://github.com/frappe/erpnext/issues

---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.

Rushabh Mehta

unread,
Sep 24, 2014, 1:07:43 AM9/24/14
to erpnext-dev...@googlegroups.com
Are you setting the trigger on the right field?

is it "discount_rate" or "custom_rate" ?



@rushabh_mehta

Javier Wong

unread,
Sep 24, 2014, 2:23:17 AM9/24/14
to erpnext-dev...@googlegroups.com
Dear Nabin / Rushabh,

My mistake earlier, the trigger was mean to be for the "rate" field, so whenever a change is detected on the "rate" field it will calculate the value to be set on the custom "discount_amount" field. 

I tried to put the following custom script in the Sales Invoice Item Doc but it didn't work. It is still not triggering when the Sales Invoice Item rate field value is changed.

cur_frm.cscript.rate = function(doc, cdt, cdn) {
    var d = locals[cdt][cdn];
    result_value = d.price_list_rate - d.rate;
    frappe.model.set_value(cdt, cdn, "discount_amount", result_value);
}

In fact I tried an msgprint within the function as well and it doesn't trigger so I suspect that it is not even hitting this function.

Any ideas? Thanks again.


Javier
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Nabin Hait

unread,
Sep 24, 2014, 2:28:19 AM9/24/14
to erpnext-dev...@googlegroups.com

Put the custom script for Sales Invoice doctype, not for Sales Invoice Item.

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/4d051527-a81d-4c9f-bb78-ecef9731e5b3%40googlegroups.com.

Javier Wong

unread,
Sep 24, 2014, 10:29:34 PM9/24/14
to erpnext-dev...@googlegroups.com
Dear Nabin,

That worked wonders and the function triggers now. Just for sharing, I decided to put the trigger on the Pricing Rule field at the end of the day as I noticed very odd behaviour when attaching the trigger to fields like qty, price_list_rate and rate --> when using pricing rule it seems to override some default functionality which seem to block the system from writing into the rate field when a pricing rule triggers.

I do have one question though. Similar to the "price_list_rate" field, I would like to disable my custom field from being editable by a user. Can I do that programmatically or from the Customize form?

Thanks so much guys. You are superb.


Javier
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsubscr...@googlegroups.com.

--
1. Please be kind and don't send large attachments
2. For issues, features requests use https://github.com/frappe/erpnext/issues
---
You received this message because you are subscribed to the Google Groups "ERPNext Developer Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer-forum+unsub...@googlegroups.com.

Anand Doshi

unread,
Sep 25, 2014, 11:32:08 AM9/25/14
to erpnext-dev...@googlegroups.com
Well. If you always want a field to be non editable, you can mark it as read only from the custom field form.  

Sent from my phone
To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/6f34b9cd-2244-4fc5-a57e-c3a0ca066b47%40googlegroups.com.

Javier Wong

unread,
Sep 25, 2014, 10:20:32 PM9/25/14
to erpnext-dev...@googlegroups.com
Dear Anand,

Is it possible to set the field to read only programmatically? I have a custom field that is of Currency type called "discount_amount" and I want to make it read only after say the pricing rule is triggered. Can I run something like "frappe.model.set_df_property(cdt, cdn, "discount_amount", "read_only", 0);"? It currently doesn't work - perhaps my syntax is wrong....

Appreciate any help. Thank you very much....

The current customer script that I am using "all works except setting the field to read only"

cur_frm.cscript.pricing_rule = function(doc, cdt, cdn) {
    var d = locals[cdt][cdn];
    result_value = d.price_list_rate - d.rate;
    frappe.model.set_value(cdt, cdn, "discount_amount", result_value);
    frappe.model.set_df_property(cdt, cdn, "discount_amount", "read_only", 0);
}


Javier

Rushabh Mehta

unread,
Sep 26, 2014, 1:55:37 AM9/26/14
to erpnext-dev...@googlegroups.com
On 26-Sep-2014, at 7:50 am, Javier Wong <boh...@gmail.com> wrote:

Dear Anand,

Is it possible to set the field to read only programmatically? I have a custom field that is of Currency type called "discount_amount" and I want to make it read only after say the pricing rule is triggered. Can I run something like "frappe.model.set_df_property(cdt, cdn, "discount_amount", "read_only", 0);"? It currently doesn't work - perhaps my syntax is wrong....

Appreciate any help. Thank you very much....

The current customer script that I am using "all works except setting the field to read only"

cur_frm.cscript.pricing_rule = function(doc, cdt, cdn) {
    var d = locals[cdt][cdn];
    result_value = d.price_list_rate - d.rate;
    frappe.model.set_value(cdt, cdn, "discount_amount", result_value);
    frappe.model.set_df_property(cdt, cdn, "discount_amount", "read_only", 0);

Should be cur_frm.set_df_property ...

}

To unsubscribe from this group and stop receiving emails from it, send an email to erpnext-developer...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/erpnext-developer-forum/e4ac88b1-784a-4483-8899-5324dbaf98e9%40googlegroups.com.

Javier Wong

unread,
Sep 29, 2014, 5:26:06 AM9/29/14
to erpnext-dev...@googlegroups.com
Dear Rushabh,

Thanks for the recommendation. This would only work if the field is in the main form.

In my scenario I am running the script on the Sales Order DocType but the field I want to make read_only is within the Sales Order Item form.

Just as an example, once there is a change in the qty within the Sales Order Item form I want to disable the qty field.

The following script was created in the Sales Order form.

cur_frm.cscript.qty = function(doc, cdt, cdn) {
    cur_frm.set_df_property("qty", "read_only", true);
}

Appreciate any help. Thank you very much.


Javier
Reply all
Reply to author
Forward
0 new messages