Webhooks and sending requests

Hi there. Today I'm gonna tell you about webhooks and will show one small example of its use.
By using the webhook configuration we can easily send requests to the Acumatica instance. Today I'll show you a POST request.

First of all, we need to create a class which will handle methods for POST requests. In my way it will be “MapperWebhookHandler” which will take text from the console and will send this text to my Acumatica instance.
Please do not download any nuget packages. Acumatica already has everything that you need for development.

Let’s create our Webhook class and we need to inherit IWebhookHandler interface:

public class MapperWebhookHandler : IWebhookHandler
{
    public async Task<System.Web.Http.IHttpActionResult> ProcessRequestAsync(
                HttpRequestMessage request, CancellationToken cancellationToken)
    {
        using (var scope = GetAdminScope())
        {
            if (request.Method == HttpMethod.Post)
            {
                var result = request.Content.ReadAsStringAsync().Result;
                var customer = JsonConvert.DeserializeObject<CustomerMapper>(result);
                var graph = PXGraph.CreateInstance<MapperMaint>();
                graph.OrderCustomerMapperView.Insert(new OrderCustomerMapper()
                {
                    CustomerName = customer.Name,
                    CustomerEmail = customer.Email,
                    CustomerPassword = customer.Password
                });
                graph.Actions.PressSave();
            }
        }
        return new OkResult(request);
    }
    private IDisposable GetAdminScope()
    {
        var userName = "admin";
        if (PXDatabase.Companies.Length > 0)
        {
            var company = PXAccess.GetCompanyName();
            if (string.IsNullOrEmpty(company))
            {
                company = PXDatabase.Companies[0];
            }
            userName = userName + "@" + company;
        }
        return new PXLoginScope(userName);
    }
}

Now, you can build your project and after that you need to add your webhook class to the customization project under the webhook section.

Then you need to register your webhook on the SM304000 screen, make it active also.

You will be able to see a link(this like hide on my screenshot by red color) which you can take and you where you want.

Let’s test now. For this moment I have my screen where I send data by webhook like this.

We are going to make a request to see that everything is working as it should.

I've sent this object:

And on a screen for this webhook we can see this

We can say that everything works fine and now you can create webhook for your needs.

Summary

Here I've described webhooks and provided an example of how to use them in Acumatica. Also I've created a class called "MapperWebhookHandler" that handles POST requests and sends data to an Acumatica instance. The code for the class and explains how to register it on the SM304000 screen. The author also shows how to test the webhook by sending data and checking if it appears on the screen. Finally, the article concludes by suggesting that readers can create their own webhooks based on their needs.

 

 

Add comment

Loading