BPM to call POST web call

Has anyone created a BPM to call a POST web call. The USE case is that our customer wants us to send them the tracking information for each order when the tracking information is entered.

Or is this something that needs to be done out of system.

You can do this in a BPM, use the WebRequest and WebResponse classes.

2 Likes

Any examples of doing this?

From the MSDN Article WebRequest Class (System.Net) | Microsoft Learn

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Create a request for the URL. 		
            WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}

Remember, a custom code block is simply a class.

Here is a sample I use for people. Follows the MSDN post that @Aaron_Moreng posted. You can paste this into custom code and add Usings

/*
Add to Usings
using System.Net;
using System.Text;
using System.IO;
*/

WebRequest request = WebRequest.Create ("http://contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
Epicor.Customization.Bpm.InfoMessage.Publish((((HttpWebResponse)response).StatusDescription));

// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
	Epicor.Customization.Bpm.InfoMessage.Publish(responseFromServer);

// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
3 Likes

I actually had this in my queue as a doc story going forward. Any theft err… leveraging issues? :wink:

2 Likes

Thanks all got the web request code started.

Is there a JSON package? Need to create a string like this I could create it, just was checking to see if there was an automagic solution.

string Json = “{“Date”:“06/12/2017”,“PONumber”:“SO -1234”,“Carrier”:“UPS Ground”,“TrackingNumber”:“1234”,“ShipToName” : “Customer”,“ShipToAddr1” : “123 Main St”,“ShipToAddr2” : “Optional”,“ShipToAddr3” : “Optional”,“ShipToCity” : “San Francisco”,“ShipToState”: “CA”,“ShipToCountry”:“USA”,“ShipToZip”: “94103”,“Items”:[{“PartNumber” : “IC2X0GN1S”,“SerialNumber”: “AVP05201519955”},{“PartNumber” : “SomeNumber”,“SerialNumber” : “SN”},{“PartNumber”: “A7-URL-RL”}]}”;

You could use Newtonsoft but its not built in you’ll have to stick it in the externall DLL’s folder to load it into Epicor BPM

Hey @Bart_Elia how about BPM support for nugget?

Install-Package Newtonsoft.Json

that would be swanky! One can dream…

(Sets down his Kool-Aid)

With the movement toward SaaS (Epicor, Microsoft, etc.), I was wondering about the way we (non-SDKers) customize Epicor. Instead of adding DLLs to our servers and clients, what if there was a framework where users/contractors/solution providers could consolidate web services into a single “container?” This server/container would be able to use the entire breadth of the .Net universe (including NuGet). All of your custom DLLs would be placed there. Your custom code woudl be separate from the Epicor application other than the web call. (It’s kinda like the BPMServer on steroids…with less angry outbursts.) One would just use a similar web call as described above to communicate with the customization service.

So now, a product like Bartender wouldn’t be picking up files in a certain folder but it would be called as a web service and with the information needed to print the labels (number, location, etc.). Need a routine to serialize JSON? Encryption? EDI transmission? Proprietary calculations for your configurator? Just call your custom web service.

With the new update cadence, this could make updating less brittle too since the bulk of the custom code is separate. Of course, this doesn’t replace BPMs or customizations but it would make them easier if the most of the code isn’t embedded in the Epicor application. It would also make solutions available to Multi/Dedicated tenant users too.

Anywho, food for thought for the end of the week near quittin’ time… Enjoy the weekend everybody!

Mark W.

3 Likes

Is there a way to import a SSL Certificate? I am getting the following error. Our Customer (we are the supplier sending information to the customer in this situation) is not going to get a cert. :frowning_face:

Thoughts?

Server Side Exception

BPM runtime caught an unexpected exception of 'WebException' type.
See more info in the Inner Exception section of Exception Details.

Exception caught in: Epicor.ServiceModel

Error Detail 
============
Description:  BPM runtime caught an unexpected exception of 'WebException' type.
See more info in the Inner Exception section of Exception Details.
Inner Exception:  The remote certificate is invalid according to the validation procedure.

I can run c# code in visual studio to ignore it, based on the following web page, but it didn’t like being pasted into Epicor.

https://stackoverflow.com/questions/2675133/c-sharp-ignore-certificate-errors

I am sure newtonsoft is included, we are using it in our BPMs. You should be able to add the assemblies and reference them from within the BPM workflow designer. At the bottom right, usings & references.

Open the address in the browser, see what certificate error occurs there.
Usually there are 2 problems

  1. URL address used does not match to what is written in the certificate - like you use https://localhost/… and certificate contains https://some-site/… You should always use same address as in the certficate.
  2. This is self-signed certificate used on the target site, and your client machine (the one were you execute POST) does not trust this self-signed certificate. Then you need to import the certificate on your client machine to the Local Computer certificate storage, and add it to the Trusted People or Trusted Root Certification Authorities storage,

Your discussion of this ‘containerization’ fits the current discussions going on in the industry around this. Docker, Kubernetes, Azure Functions amongst others are all are playing around with this concept of isolated execution of code for a variety of reasons.
The E10 Dedicated Tenancy offering is a start into that approach although hugely coarse at this point - each tenant runs as it’s own Windows Identity so tenants are bound to their own Process Identity which runs a lowest allowed privileges and each Identity only has access to it’s own db. The issue with this is density - the number of duplicative caching and assemblies across processes. That’s an active area of investigation in the industry and a personal fascination.
We have put a few subsystems on a diet so you should see current w3wp sizes shrinking as we optimize a few things for ourselves and you (Anyone complaining if we make your hardware more efficient?).
Stay Tuned, when we can productize some of this research into something you can leverage, I’ll be advertising it around to this kind of audience.

2 Likes