Determine incoming message format with Azure Function

When receiving an AS2 message from a third party, you are not always sure what format you are receiving (X12, EDIFACT, XML, Flat File, ...) Specially if you are working with an external EDI service for X400 (like GXS, EDICOM, ...)

BizTalk

When you use the EDI Disassembler component in a receive location, it determines if you are receiving an EDIFACT or an X12 message. The way it does that is quite easy actually.
It looks at the 3 first characters of the message body.
  • ISA = X12
  • UNA or UNB = EDIFACT
In any other case it will throw an error.

Logic Apps

In Logic Apps, the components for EDIFACT and X12 are not combined in common "EDI" compontens. So we need a way to determine this ourselves in order to use the correct components.
To make it fully generic, I also included the option to receive XML and custom Flat File messages.
using System.Net;
using System.Text.RegularExpressions;

public static async Task Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // get the message content
    dynamic data = await req.Content.ReadAsAsync<object>();
    string messagecontent=data?.messagecontent;

    log.Info(string.Format("Message content : {0}",messagecontent));
    //Default to TEXT
    string format="TEXT";
    
    //Check for X12
    //X12Pattern=@"^ISA\*";
    Regex rgx = new Regex(@"^ISA\*");
    if(rgx.IsMatch(messagecontent))
    {
        format="X12";
    }
    else
    {
        //Check for EDIFACT
        //EDIFACTPattern=@"^UN[AB]";
        rgx=new Regex(@"^UN[AB]");
        if(rgx.IsMatch(messagecontent))
        {
            format="EDIFACT";
        }
        else
        {
            //Check for XML
            //xmlPattern=@"^<";
            rgx=new Regex(@"^<");
            if(rgx.IsMatch(messagecontent))
            {
                format="XML";
            }
        }
    }

    return req.CreateResponse(HttpStatusCode.OK, format);
}

This function returns 4 possible values
  • X21
  • EDIFACT
  • XML
  • TEXT
I hope this helps when you want to create a generic AS2 receive Logic App.

Comments

Popular posts from this blog

Query SAP HANA from BizTalk

Receive invalid xml through WCF adapter

Azure API Management : Exposing backend SOAP service as REST