triggering built-in webhook based on some condition  Topic is solved

Discussions relating to plugin development, and the Jiwa API.

Re: triggering built-in webhook based on some condition

Postby SBarnes » Mon Dec 12, 2022 6:57 pm

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.
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: triggering built-in webhook based on some condition

Postby sameermoin » Tue Dec 13, 2022 2:14 am

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?
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

Re: triggering built-in webhook based on some condition

Postby SBarnes » Tue Dec 13, 2022 7:27 am

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?
Regards
Stuart Barnes
SBarnes
Shihan
Shihan
 
Posts: 1619
Joined: Fri Aug 15, 2008 3:27 pm
Topics Solved: 175

Re: triggering built-in webhook based on some condition

Postby sameermoin » Tue Dec 13, 2022 4:11 pm

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
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

Re: triggering built-in webhook based on some condition

Postby Scott.Pearce » Wed Dec 14, 2022 10:25 am

OK, here is a plugin that creates custom webhook events:

Plugin Custom Inventory Webhook.xml
(29.57 KiB) Downloaded 78 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.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Re: triggering built-in webhook based on some condition

Postby sameermoin » Wed Dec 14, 2022 3:43 pm

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 2698 times
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

Re: triggering built-in webhook based on some condition

Postby Scott.Pearce » Wed Dec 14, 2022 4:02 pm

Try using the latest REST API plugin (7.2.1.32):

Plugin REST API.xml
(1.71 MiB) Downloaded 81 times
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Re: triggering built-in webhook based on some condition

Postby sameermoin » Wed Dec 14, 2022 4:44 pm

thanks that's worked
sameermoin
Regular Contributor
Regular Contributor
 
Posts: 75
Joined: Wed Jun 15, 2022 4:02 am
Topics Solved: 1

Re: triggering built-in webhook based on some condition

Postby Scott.Pearce » Wed Dec 14, 2022 4:45 pm

No problem. Thanks for letting me know.
Scott Pearce
Senior Analyst/Programmer
Jiwa Financials
User avatar
Scott.Pearce
Senpai
Senpai
 
Posts: 742
Joined: Tue Feb 12, 2008 11:27 am
Location: New South Wales, Australia
Topics Solved: 221

Previous

Return to Technical and or Programming

Who is online

Users browsing this forum: No registered users and 29 guests

cron