ERPNext Conference 2019* ERPNext.com Blog

Add hook to function which is not in default list


#1

In the linked topic I was investigating possibility to change Bin DocType bin.py file.

Alternative would be to use hooks in custom app to update Bin Item -s related to Bin document.

I found list of events which can be hooked here https://frappe.io/docs/user/en/guides/basics/hooks but I did not find the explanation when these events are triggered. Can somebody point me to this information?

I would like to hook to https://github.com/frappe/erpnext/blob/develop/erpnext/stock/doctype/bin/bin.py function set_projected_qty(self) to trigger Bin Item generation code located in my custom app.

When I look bin.py I can see that set_projected_qty(self) is triggered by

  1. validate(self) - which can be hooked from custom app
  2. update_stock(self, args, allow_negative_stock=False, via_landed_cost_voucher=False):
    ‘’‘Called from erpnext.stock.utils.update_bin’’’ - this seems to be called from outside
    3.def update_reserved_qty_for_production(self):
    ‘’‘Update qty reserved for production from Production Item tables
    in open production orders’’’ - this seems to be called from outside
  3. update_reserved_qty_for_sub_contracting(self): - this seems to be called from outside

What I want to do is to detect when some document or event changes Bin projected quatity. If I can detect this I can update my Bin Item -s related to this Bin.

If change events are triggered from multiple places from outside how can I know if validate(self) is called always when Bin is changed? Or some other hooks event like on_update etc. is always triggered on Bin change with self.db_set() or self.db_update().

Or there is better way to hook or override function set_projected_qty(self)?


#2

Can anyone share their knowledge?
All comments are welcome!


#3

Hi, I think validate is more suitable for your changes. It works everytime when you change somehow doctype. Please check
https://frappe.io/docs/user/en/tutorial/controllers


#4

@joonas I’m having the same problem. I think that db_update and db_set does not trigger the hooks properly, how do you solve your problem?