Page 2 of 2

Re: triggering built-in webhook based on some condition

PostPosted: Mon Dec 12, 2022 6:57 pm
by SBarnes
If you are already calling to get the individual products the following link might be the best option https://docs.servicestack.net/auto-batc ... t-batching to get all the products you need in one go.

Re: triggering built-in webhook based on some condition

PostPosted: Tue Dec 13, 2022 2:14 am
by sameermoin
I think instead of playing with the default webhook, can I create my own webhook for inventory update/create?

can I get a reference for creating a custom webhook?

Re: triggering built-in webhook based on some condition

PostPosted: Tue Dec 13, 2022 7:27 am
by SBarnes
Short answer is yes, I've done it before where I added salesorder.processed below is a link that will give you some guidance, in addition to this have a look in the rest api plugin in the business logic section, you will need to come up with your own actions like inventory.specialcreate don't repeat what is there


viewtopic.php?f=32&t=1329&p=6193&hilit=webhookevents#p6193


The basics are:

  • Hook onto the business logic save end event
  • Create some sort of Json Payload/Body
  • Process the data to WebhooksEventsPOSTRequest thats what the WebHook function in the REST API plugin does
  • Make sure you have fall back code to put an entry in the messages table if the step above fails
  • Make sure your hook is registered to WebHookEvents
  • You will probably need to reference the rest api plugin in plugin references and call your rest api plugin something different to avoid any scoping issues

Mike can you add anything I may have missed?

Re: triggering built-in webhook based on some condition

PostPosted: Tue Dec 13, 2022 4:11 pm
by sameermoin
SBarnes, this is exactly what I want to do:

I added the below line in the REST API plugin inside Configure method

Code: Select all
JiwaApplicationManager.ObjectDictionary.Add("BusinessLogicPlugin", this);


screenshot restplugin.jpg


and in my custom plugin Business logic class I am adding a new webhook event like the below:

Code: Select all
RESTAPIPlugin.WebHookEvents.Add(new WebHookEvent { Name = "specialinventory.updated", Description = "Occurs on inventory update" });


but I am unable to access the Rest API plugin BusinessLogic Class WebHook method

businesslogic.jpg

Re: triggering built-in webhook based on some condition

PostPosted: Wed Dec 14, 2022 10:25 am
by Scott.Pearce
OK, here is a plugin that creates custom webhook events:

Plugin Custom Inventory Webhook.xml
(29.57 KiB) Downloaded 134 times


I developed it against Jiwa 7.2.1.0 SR14 running the REST API plugin v7.2.1.32.

The plugin creates webhook events which fire during inventory item save ONLY if inventory custom field "I'm Special" is true (ticked).

Webhook event names:
"inventory.specialcreated"
"inventory.specialupdated"
"inventory.specialdeleted"

You will see where the webhook is "fired" in the code (line 38):

Code: Select all
JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.Webhook(inventoryItem.Manager, body, (inventoryItem.InsertFlag) ? "inventory.specialcreated" : (inventoryItem.DeleteFlag) ? "inventory.specialdeleted" : "inventory.specialupdated", JiwaFinancials.Jiwa.RESTAPIPlugin.BusinessLogicPlugin.PluginName);   


That is where you could change the body variable. Before making the above call you could go read your custom productIDs via the supplied inventoryItem object. Using those productIDs you could instantiate and read in other inventory objects or entities. You could then deserialise those additional inventory objects/entities, and perhaps append some or all of the resultant json to the existing body variable, which in turn gets sent on to the webhook.

Notes:
1. A plugin for custom webhooks must have a class that implements IJiwaRESTAPIPlugin, so that IIS or the Jiwa API service will pick it up and run the contained "Configure()" function. It is in here that you add the webhook definitions, making calls out the the main Rest API plugin methods and properties (the main REST API plugin is added as a reference on the "Plugin References" tab).

2. Set the "Execution Order" of custom webhook plugins to be a number higher than that of the main REST API plugin. This allows the main REST API plugin to set up it's webhook collection ready for when your plugin comes along to add to it.

3. One trap was the passing of the "correct" plugin name when firing the webhook. You should pass the plugin name of the main REST API plugin - this is because in the Webhook() method the plugin name is used to read in the "WebhooksHostURL" system setting. Thankfully said plugin name was available via a static property in the referenced main REST API plugin.

I hope this sets you on your way.

Re: triggering built-in webhook based on some condition

PostPosted: Wed Dec 14, 2022 3:43 pm
by sameermoin
Hi Pearce,

The plugin you shared also throws the same error. When I am compiling the plugin that you gives me throwing the below error:

webhook error.jpg


The RESP API plugin reference is already defined.

plugin reference.jpg


Did you add something in the REST API plugin or is this a version issue?

I am using Jiwa 7.2.1.0

jiwa version.jpg
jiwa version.jpg (70.2 KiB) Viewed 2771 times

Re: triggering built-in webhook based on some condition

PostPosted: Wed Dec 14, 2022 4:02 pm
by Scott.Pearce
Try using the latest REST API plugin (7.2.1.32):

Plugin REST API.xml
(1.71 MiB) Downloaded 137 times

Re: triggering built-in webhook based on some condition

PostPosted: Wed Dec 14, 2022 4:44 pm
by sameermoin
thanks that's worked

Re: triggering built-in webhook based on some condition

PostPosted: Wed Dec 14, 2022 4:45 pm
by Scott.Pearce
No problem. Thanks for letting me know.