Page 1 of 1

StockLevelChanged event running more than once

PostPosted: Fri Dec 30, 2022 4:28 pm
by sameermoin
Hi Everyone,

I am using the StockLevelChanged event on ApplicationManagerPlugin class. The problem is when I activate a Stock Take from Inventory > Stock Take and put the debugger in the code, the debugger hit multiple times for a single inventory.

I checked the REST API plugin, and their inventory.stocklevel webhook was implemented using the same event and because of this behavior the webhook also triggered multiple time

Is this normal behavior? If so how can I make it run only one time for each inventory? It exhausts the other application endpoint.

Re: StockLevelChanged event running more than once

PostPosted: Fri Dec 30, 2022 4:48 pm
by SBarnes
Whilst I am not sure about it running twice for this particular hook my experience with this hook is that you usually end up inundating an external system with events to do with SOH.

What I found as a better system was to put in place a set of SQL triggers that would update a table with the SOH value and a date and time, then what simply became the easiest solution was to then use a scheduled plugin to run at an interval of something like five minutes and send the data that way and then simply keep the last time the schedule ran in a system setting.

It also meant I was able to avoid two issues which were

  • Sending details for products that did not have their web enabled flag set or weren't related to the relevant warehouse
  • Things getting out of order in terms of hook events and you ending up with the wrong SOH value

That being said try lengthening the retry interval you might be experiencing the behavior of hooks being sent multiple times and lengthening the hook interval usually fixes this.

Re: StockLevelChanged event running more than once

PostPosted: Fri Dec 30, 2022 4:58 pm
by sameermoin
SBarnes wrote:
  • Sending details for products that did not have their web enabled flag set or weren't related to the relevant warehouse
  • Things getting out of order in terms of hook events and you ending up with the wrong SOH value



I am filtering inventory based on the warehouse and then querying for stocks. the only problem I am facing is that it's running more than once

Re: StockLevelChanged event running more than once

PostPosted: Fri Dec 30, 2022 5:01 pm
by SBarnes
Then see if lengthening the retry interval solves the issue.

Re: StockLevelChanged event running more than once

PostPosted: Tue Jan 03, 2023 11:04 am
by Mike.Sheen
sameermoin wrote:when I activate a Stock Take from Inventory > Stock Take and put the debugger in the code, the debugger hit multiple times for a single inventory.


I'll test that scenario and if I can repro it, I will see what work-arounds are possible.

It may be by design - depending on how the stock take is adjusting the stock level - for example, if the stock take is reducing the stock level of an item it may fire the webhook event for each line detail - not just the item itself.

Re: StockLevelChanged event running more than once  Topic is solved

PostPosted: Tue Jan 03, 2023 11:36 am
by Mike.Sheen
Mike.Sheen wrote:It may be by design - depending on how the stock take is adjusting the stock level - for example, if the stock take is reducing the stock level of an item it may fire the webhook event for each line detail - not just the item itself.


This is exactly what it is. The StockLevelChanged webhook is invoked whenever the WarehouseSOHUpdateEvent is raised by the JiwaApplication.Manager. The stock take will cause that event to be raised for each line detail when saving a stock take that is being activated.

If you are not familiar with Jiwa's Line Detail concept - to give you a brief explanation it means stock levels are comprised of multiple line details which represent a discrete inwards movement of the stock. If you have 10 left of an item in stock in a warehouse, there may be anywhere between 1 and 10 line details representing that quantity - each with potentially different costs, bin locations, expiry dates and serial numbers.

If you need to only have one stock level changed event, then you will need to create an intermediate receiver for the webhook which aggregates and then transmits a single webhook.

Re: StockLevelChanged event running more than once

PostPosted: Tue Jan 03, 2023 6:35 pm
by sameermoin
Mike.Sheen wrote:If you need to only have one stock level changed event, then you will need to create an intermediate receiver for the webhook which aggregates and then transmits a single webhook.


Hi Mike, thanks for your response.

I didn't get this. I am interested in only the last updated quantity but the problem is how do I know that the event for x Inventory is the last updated?

for example, let's say 1001 inventory and it's updating four times due to multiple lines so its stocklevel event will also run four times, but how do I know from this event that this is the last run?

Re: StockLevelChanged event running more than once

PostPosted: Fri Feb 10, 2023 1:51 pm
by Mike.Sheen
sameermoin wrote:I am interested in only the last updated quantity but the problem is how do I know that the event for x Inventory is the last updated?

for example, let's say 1001 inventory and it's updating four times due to multiple lines so its stocklevel event will also run four times, but how do I know from this event that this is the last run?


Because the most recent one is the last one?

If you have multiple events signaling a change to the stock level, then the most recent one is the one you are interested in.