big_water
big_water

Reputation: 3204

How to make outgoing request or webhook in Acumatica?

I'm integrating an Asp.NET application with Acumatica that needs to update shipping information (tracking #, carrier, etc.) when it becomes available in Acumatica. Is there a way to have Acumatica call an endpoint on my Asp.NET app when a shipment is created? I've searched through a lot of the docs (available here), but I haven't come across anything to send OUT information from Acumatica to another web service.

Ideally, this outgoing call would send the shipment object in the payload.

Upvotes: 3

Views: 1386

Answers (3)

bbrown
bbrown

Reputation: 6360

If you don't have Acumatica 2017R2, then you have to create your own extension project and then you can call it from your Acumatica code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace MyApp
{
    public static class Utility
    {
        private static WebRequest CreateRequest(string url, Dictionary headers)
        {
            if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
            {
                WebRequest req = WebRequest.Create(url);
                if (headers != null)
                {
                    foreach (var header in headers)
                    {
                        if (!WebHeaderCollection.IsRestricted(header.Key))
                        {
                            req.Headers.Add(header.Key, header.Value);
                        }
                    }
                }
                return req;
            }
            else
            {
                throw(new ArgumentException("Invalid URL provided.", "url"));
            }
        }
        public static string MakeRequest(string url, Dictionary headers = null)
        {
            WebResponse resp = CreateRequest(url, headers).GetResponse();
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            string response = reader.ReadToEnd();
            reader.Close();
            resp.Close();
            return response;
        }
        public static byte[] MakeRequestInBytes(string url, Dictionary headers = null)
        {
            byte[] rb = null;
            WebResponse resp = CreateRequest(url, headers).GetResponse();
            using (BinaryReader br = new BinaryReader(resp.GetResponseStream()))
            {
                rb = br.ReadBytes((int)resp.ContentLength);
                br.Close();
            }
            resp.Close();
            return rb;
        }
    }
}

You can then call it like this:

try
{
  Utility.MakeRequest(theUrl, anyHeadersYouNeed);
}
catch(System.Net.WebException ex)
{
  throw(new PXException("There was an error.", ex));
}

Upvotes: 0

bbrown
bbrown

Reputation: 6360

This wasn't available when you asked the question but push notifications seem to be exactly what you're looking for:

Help - https://help.acumatica.com/(W(9))/Main?ScreenId=ShowWiki&pageid=d8d2835f-5450-4b83-852e-dbadd76a5af8

Presentation - https://adn.acumatica.com/content/uploads/2018/05/Push-Notifications.pdf

Upvotes: 3

Yuriy Zaletskyy
Yuriy Zaletskyy

Reputation: 5151

In my answer I suppose that you know how to call some outside service from C# code, and for your is a challenge how to send notification from Acumatica. I propose you to extend each Persist method in each Acumatica graph, from which you expect to send notification when object is persisted in db. IMHO the best option for this is to override method persist ( btw, it overriding persist method is well described in T300 ). In code of extension class you can do the following:

public void Persist(PersistDelegate baseMethod) 
{ 
   baseMethod(); // calling this method will preserve your changes in db

   //here should go your code, that will send push/pop/delete etc web request into your asp.net application. Or in other words your web hook.
  }

Upvotes: 1

Related Questions