Infoplus Scripts can be used to allow you to customize the behavior of your EDI Connections. For each Document Type, Infoplus has a built-in translation or mapping that it uses, based on configurations specified in the EDI Connection. However, to support custom business rules or features that aren't available in the core of Infoplus, a script can optionally be used to modify both Inbound and Outbound documents.


All Document Types have some common objects available in the global script context. These include:

  • utils - standard Infoplus script utils object.  See The Introduction to Infoplus Scripts for additional information.  
  • infoplusApi -standard Infoplus script utils object.  See The Introduction to Infoplus Scripts for additional information.   
  • documentModel - JSON representation of incoming EDI document
  • ediUtils - provides utility methods for working with EDI documents.  See below for method reference.  
  • inboundDocumentModel (JSON representation of inbound EDI (855, 856))
  • parcelShipmentList (list of parcelShipment API objects (856))


Inbound 850 Example

  • order - this object is an instance of the Infoplus API's order type.  It will be initially populated with the default mapping that Infoplus has performed on the 850.  You can modify this object to customize this mapping.  For example, to change the carrier used when the order is created, you would say "order.carrierId = 0;" Consult the Infoplus API Documentation for full details on fields available on this object.
  • documentModel - this object is a representation of the inbound EDI document. You can read values from this object, to apply logic for how you want to customize the order object.  Methods on the ediUtils object exist to help processing this object.  Note that for X12 EDI, this object is an array of arrays - where each segment in the document is a top-level array, and each segment is a lower-level array, with each element represented as a string in that array.  For example, a document containing with these segments:
    • ...
      ST*850*1130~

      BEG*00*SA*80X611071**20181001~

      REF*LOB*10008~
      ...

  • Would be represented as this documentModel:

    • [
         ...
         ["ST", "850", "1130"],
         ["BEG", "00", "SA", "80X611071", "", "20181010"],
         ["REF", "LOB", "10008"],
         ...
      ]


To combine the two concepts introduced above, we could imagine:

var carrierFrom850 = ediUtils.getElementValueBySegmentAndElementRef(documentModel, "TD5", 5);
utils.log("Read carrier [" + carrierFrom850 + "] from 850 document carrierAlphaCode");

if(carrierFrom850 == "UPGF")
{
   order.carrierId = 7000;
}
else if(carrierFrom850 == "UP1D")
{
   order.carrierId = 1;
}
else if(carrierFrom850 == "UP2D")
{
   order.carrierId = 2;
}
else
{
   utils.log("Using default carrier 111 because carrier from 850 was not recognized.");
   order.carrierId = 111;
}



Methods on the ediUtils object


getNumberPadded

  • Description:  Format a number with leading-0's for padding, for placement in an outbound EDI Document body.
  • Inputs: 
    • size - number of digits.
    • number - value to pad.
  • Example:
    • ediUtils.getNumberPadded(6, 147);  // returns 000147


getDateFormatted

  • Description:  Format a date/time variable for inclusion in an EDI document, for placement in an outbound EDI Document body.
  • Inputs:
    • date - the date to format
    • format - String, such as "YYYYMMDD" or "HHMMSS"
  • Examples:
    • ediUtils.getDateFormatted(order.orderDate, "YYYYMMDD"); // returns, for example, "20191225"
    • ediUtils.getDateFormatted(order.createDate, "HHMMSS");  // returns, for example, "123059"


getElementValueBySegmentAndElementRef

  • Description:  Get a single value from an EDI segment, specifying the segment name and element index.
  • Inputs:
    • documentModel 
    • segmentName - name of the segment to return a value from (e.g., "BEG", "TD5", "PO1", "LIN").  Note that only the first matching segment will be found.  For repeated segments, see 
    • elementIndex - index of the value from the segment to return, starting from 0 (noting that the segment identifier is in index 0).
  • Examples:
    • ediUtils.getElementValueBySegmentAndElementRef(documentModel, "BEG", 3); 
      // for a document containing the segment "BEG*00*SA*80X611071**20181001~", 
      // returns the string value "80X611071"


getSegmentArrayBySegment

  • Description:  Get an array of values, representing an EDI segment, as a script array.
  • Inputs:
    • documentModel
    • segmentName - name of the segment to return a value from (e.g., "BEG", "TD5", "PO1", "LIN").  Note that only the first matching segment will be found.  For repeated segments, see
  • Examples:
    • ediUtils.getSegmentArrayBySegment(documentModel, "BEG"); 
      // for a document containing the segment "BEG*00*SA*80X611071**20181001~", 
      // 
      returns the the javascript array: ["BEG", "00", "SA", "80X611071", "", "20181001"]  


getElementValueBySegmentAndElementRefForQualifier

  • Description:  Get a value from a REF segment, where REF01 matches the supplied parameter.  
  • Inputs:
    • documentModel
    • segmentName - To find a REF segment, use the value "REF".
    • qualifier - Value to be matched in REF01
    • elementIndex - index of the value from the segment to return, starting from 0 (noting that the segment identifier is in index 0).  To get REF02, use the value of 2.  
  • Examples:
    • //////////////////////////////////////////////////////////////////////

      // Get the REF02 value from the REF segment with the Qualifier "T1" //

      //////////////////////////////////////////////////////////////////////

      var ediParcelLabelRef1 = ediUtils.getElementValueBySegmentAndElementRefForQualifier
         (documentModel, "REF""T1", 2);


getIntegerElementValueBySegmentAndElementRefForQualifier

  • Description:  Get a value from a REF segment, where REF01 matches the supplied parameter, returning the value as a number (Integer).  Note that an exception will be thrown if the value that is found cannot be parsed as an Integer.  
  • Inputs:    
    • documentModel
    • segmentName - To find a REF segment, use the value "REF".
    • qualifier - Value to be matched in REF01
    • elementIndex - index of the value from the segment to return, starting from 0 (noting that the segment identifier is in index 0).  To get REF02, use the value of 2.  
  • Examples:
    • var myInteger = ediUtils.getIntegerElementValueBySegmentAndElementRefForQualifier
         (documentModel, "REF""ZZ", 2);


getN1AddressLoopArrayByIdentificationQualifier

  • Description:  Get an N1 loop from an ediDocument, based on the supplied qualifier (matched against N101).  
  • Inputs:
    • documentModel
    • qualifier - Value to be matched in N101
  • Returns:
    • array of segments (which are arrays of strings), which can be passed into other methods which accept documentModel.  
  • Examples:
    • ////////////////////////////////////////////////////////

      // Get the N1 Address loop for Qualifier ST (Ship To) //

      // then get the N3 Segments 03 Element (Street 3) and //

      // set it as Orders Street 2                          //

      ////////////////////////////////////////////////////////

      var shipToArray = ediUtils.getN1AddressLoopArrayByIdentificationQualifier

         (documentModel, "ST");


      var ediStreet3 = ediUtils.getElementValueBySegmentAndElementRef

         (shipToArray, "N3", 3);


      utils.log("Found street3 value from inbound EDI as[" + ediStreet3 + "]");

      order.shipToStreet2 = ediStreet3;


getPO1ItemDataLoopArray

  • Description:  Get an array representing all PO1 loops from an ediDocument.  
  • Inputs:
    • documentModel
  • Returns:
    • array of array of segments (which are arrays of strings). 
  • Examples:
    • Given this portion of a document:
      • PO1**2*EA*2**SK*BASIC1*BP*123
        SAC*A*C310***1
        PID*F****BASIC1 ITEM
        PO1**3*EA*2**SK*BASIC2*BP*1234
        SAC*A*C310
        PID*F****BASIC2 ITEM
    • This call:
      • ediUtils.getPO1ItemDataLoopArray(ediDocument)
    • Would return this data structure:
      • [
        [
              ["PO1", "", "2", "EA", "2", "", "SK", "BASIC1", "BP", "123"],

              ["SAC", "A", "C310", "", "", "1"],
              ["PID", "F", "", "", "", "BASIC1 ITEM"]
        ],
           [

              ["PO1", "", "3", "EA", "2", "", "SK", "BASIC2", "BP", "1234"],
              ["SAC", "A", "C310"],
              ["PID", "F", "", "", "", "BASIC2 ITEM"]
        ]
        ]


rejectDocument

  • Description:  For an inbound document, tell Infoplus to stop processing the document (i.e., for an inbound 850, causes the order to not be created).  
  • Inputs:
    • message - string to appear in logs, for referencing why the document was rejected.  
  • Examples:
    • ediUtils.rejectDocument("It has invalid SKU: " + testSku);


initializeOutboundDocumentModel

  • Description:  Helper method, for when generating a custom Outbound document, to populate the ISA, GS, and ST segments.  
  • Inputs:
    • ediDocument - an ediDocument Infoplus API object, i.e., created by a call to infoplusApi.constructModel("ediDocument");
    • ediConnectionId - id of the EDI Connection that the document document should be associated with.
    • transactionType - Name of the EDI Document Type, such as "846"
    • functionalIdentifier - Functional Identifier for the document, such as "PO"
    • requestAcknowledgment - boolean to indicate whether or not to request Functional Acknowledgement (997)
  • Examples:
    • ediDocument = ediUtils.initializeOutboundDocumentModel
         (ediDocument, 2, "846", "PO", true);

sendOutboundDocument

  • Description:  Trigger a custom Outbound document to be sent to the partner.  
  • Inputs:
    • ediDocument - an ediDocument Infoplus API object, i.e., created by a call to infoplusApi.constructModel("ediDocument").  
  • Examples:
    • ediUtils.sendOutboundDocument(ediDocument);



Script Examples:

Test Script (multiple examples of getting values from EDI document):

utils.log("Running EDI 850 script for order with CustomerPoNo: " + order.customerPoNo);
 
////////////////////////////////////////////////////////
// Set a order value, just hardcoded in script no EDI //
////////////////////////////////////////////////////////
var customerOrderNo = "SCRIPT-UPDATED-ORDER-NO";
utils.log("Updating Infoplus Order with Customer Order No=[" + customerOrderNo + "]");
order.customerOrderNo = customerOrderNo;
 
//////////////////////////////
// output our documentModel //
//////////////////////////////
// utils.log("documentModel: " + documentModel);
 
//////////////////////////////////////////////////////////////////////////////////////
// Get the REF segment with the Qualifier "T1" and set it to the Parcel Label Ref 1 //
//////////////////////////////////////////////////////////////////////////////////////
var ediParcelLabelRef1 = ediUtils.getElementValueBySegmentAndElementRefForQualifier(documentModel, "REF", "T1", 2);
utils.log("Updating Infoplus Order with ediParcelLabelRef1[" + ediParcelLabelRef1 + "]");
order.parcelLabelRef1 = ediParcelLabelRef1;
 
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Get the REF segment with the Qualifier "C1" and set its value as the orders "rank" custom field //
/////////////////////////////////////////////////////////////////////////////////////////////////////
var customRank = ediUtils.getElementValueBySegmentAndElementRefForQualifier(documentModel, "REF", "C1", 2);
utils.log("Updating Infoplus Order with custom field rank[" + customRank + "]");
order.customFields.put("rank", customRank);
 
//////////////////////////////////////////////////////////////////////////////////
// Get the N1 Address loop for Qualifier ST (Ship To)                           //
// then get the N3 Segments 03 Element (Street 3) and set it as Orders Street 2 //
//////////////////////////////////////////////////////////////////////////////////
var shipToArray = ediUtils.getN1AddressLoopArrayByIdentificationQualifier(documentModel,"ST");
var ediStreet3 = ediUtils.getElementValueBySegmentAndElementRef(shipToArray, "N3", 3);
utils.log("Found street3 value from inbound EDI as[" + ediStreet3 + "]");
order.shipToStreet2 = ediStreet3;
order.shipToStreet3 = "-EDI Script 1";
 
utils.log("Done");


Test Reject Order (Shows how to reject an EDI Order via Script):

utils.log("Running EDI 850 script for order with CustomerPoNo: " + order.customerPoNo);
 
//////////////////////////////
// output our documentModel //
//////////////////////////////
// utils.log("documentModel: " + documentModel);
 
/////////////////////////////////////////////////////////////////////////////////
// Get the REF segment with the Qualifier "T1" and use it was reason to reject //
/////////////////////////////////////////////////////////////////////////////////
var testValue = ediUtils.getElementValueBySegmentAndElementRefForQualifier(documentModel, "REF", "T1", 2);
 
if(testValue != null)
{
   utils.log("Reject order due to testValue[" + testValue + "]");
   ediUtils.rejectDocument("It has invalid value of : " + testValue);
}
 
utils.log("Done");



  (