Using EDI Scripts

An overview of how scripting for EDI works within Infoplus.

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.


Infoplus Support can help with general questions about how scripting works. For help with a specific script or its outputs, you will need to submit a Pro Services request for paid support. Pro Service request form can be found here.


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 or outgoing EDI document.  Note that this object is an instance of a org.json.JSONArray, as documented here.
  • 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;"
    • Note that when an 850 script is running, this order has not yet been saved in Infoplus - i.e., it will not yet have an orderNo assigned to it.  After your script finishes running, Infoplus will store the order object.  
    • If you want to set a value in a custom field on this object, add a line to your script like this:  order.customFields.put("yourCustomFieldName", yourCustomValue);
      There is no need to call any methods on the infoplusApi to store custom values in this case.
    • 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:

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.  Note, you may need to wrap the value returned by this function in a JavaScript String() call, to use the returned value in an outbound EDI document.  
  • Inputs:
    • date - the date to format.  To use the current date & time, pass null. 
    • format - String, such as "YYYYMMdd" or "HHMMSS"
  • Examples:
    • var orderDate = String(ediUtils.getDateFormatted(order.orderDate, "YYYYMMdd")); // returns, for example, "20191225"
    • var orderCreateTime = String(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 EDI documentModel, 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 EDI documentModel.  
  • 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(documentModel)
    • 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 EDI documentModel 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);


insertSegmentBeforeFirstOccurrence

  • Description:  When producing an outbound document, insert a new Segment into the documentModel before the first segment found in the document with the specified segment identifier.  
  • Inputs:
    • documentModel - The outbound document, represented as an org.json.JSONArray object 
    • newSegment - The segment to add to the document. Can be a javascript array of strings.
    • searchSegmentId - The identifier of the segment to search for, and insert the newSegment in front of.
  • Returns:
    • Boolean true if the searchSegmentId is found, and the newSegment is inserted into the documentModel.
  • Examples:
    • // inserts a ITD*01 segment before the first IT1 segment in an 810

    • ediUtils.insertSegmentBeforeFirstOccurrence(documentModel, ["ITD", "01"], "IT1");


insertSegmentAfterFirstOccurrence

  • Description:  When producing an outbound document, insert a new Segment into the documentModel after the first segment found in the document with the specified segment identifier.  
  • Inputs:
    • documentModel - The outbound document, represented as an org.json.JSONArray object 
    • newSegment - The segment to add to the document. Can be a javascript array of strings.
    • searchSegmentId - The identifier of the segment to search for, and insert the newSegment in back of.
  • Returns:
    • Boolean true if the searchSegmentId is found, and the newSegment is inserted into the documentModel.
  • Examples:
    • // inserts an N1*RI segment after the (first) BIG segment in an 810

    • ediUtils.insertSegmentAfterFirstOccurrence(documentModel, 
         ["N1", "RI", "Infoplus"], "BIG");

insertSegmentAtIndex

  • Description:  When producing an outbound document, insert a new Segment into the documentModel at the specified 0-based index within the document.   Care must be taken, if you are calling this method within a loop (i.e., iterating over the document), to not keep re-inserting the same segment (for example, if you insert when you find a given segment - you may keep re-inserting if your loop finds the same segment again).
  • Inputs:
    • documentModel - The outbound document, represented as an org.json.JSONArray object 
    • newSegment - The segment to add to the document. Can be a javascript array of strings.
    • index - 0-based index of the position in the documentModel where the newSegment should be inserted. If the index is greater than documentModel.length() - 1, the segment will not be inserted.
  • Examples:
    • ediUtils.insertSegmentAtIndex(documentModel, itdSegment, indexForItdSegment);


removeSegmentFirstOccurrence

  • Description:  When producing an outbound document, remove the first occurrence of a given Segment from the documentModel.
  • Inputs:
    • documentModel - The outbound document, represented as an org.json.JSONArray object 
    • segmentName - The name segment to be removed from the document.  e.g., "ISS" or "TD5".
  • Returns:
    • A modified documentModel after removing a segment - should be re-assigned to the documentModel variable.
  • Examples:
    • documentModel = ediUtils.removeSegmentFirstOccurrence(documentModel, "ISS");


removeSegmentByIndex

  • Description:  When producing an outbound document, remove the segment from the document that exists at the specified 0-based index within the documentModel. 
  • Inputs:
    • documentModel - The outbound document, represented as an org.json.JSONArray object 
    • segmentIndex - The 0-based index of the segment to remove from the document. 
  • Returns:
    • A modified documentModel after removing a segment - should be re-assigned to the documentModel variable.
  • Examples:
    • documentModel = ediUtils.removeSegmentByIndex(documentModel, 12);

Full Script Examples

Demo Script 

Multiple examples of getting values from EDI document:

Reject inbound Order

Shows how to reject an inbound EDI Order from an 850 via Script

Fully Custom Outbound EDI Document

Shows how to send a custom outbound EDI 846 document:

Capture Custom Fields on Line Items

This script shows how you can populate Infoplus custom fields on order line items, based on values from an 850.  Note that you need to have pre-defined a custom field on the Order Line table for this script to work.  See Create / Edit Custom Fields for more information.