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.


Due to the ever-changing nature of writing and maintaining scripts, Infoplus does not provide support in this area. We recommend someone in your IT Department handles scripting requests internally so that you keep all control over the process.


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;" 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.  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:

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");


Reject inbound Order

Shows how to reject an inbound EDI Order from an 850 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");


Fully Custom Outbound EDI Document

Shows how to send a custom outbound EDI 846 document:

utils.log("Creating and sending a custom outbound EDI 846 document.");
var ediDocument = infoplusApi.constructModel("ediDocument");

/////////////////////////////////////////////////////////////////////////////////////
// Initialize our Outbound EDI Document Model using Infoplus EDI Utils passing in: //
// ediDocument - the document we are creating //
// 2 - the ID of the EDI Connection we want to use //
// "846" - The name of the EDI Document Type //
// "PO" - The functional identifier for our document //
// false - Indicating we would like to not request acknowledgment //
/////////////////////////////////////////////////////////////////////////////////////
ediDocument = ediUtils.initializeOutboundDocumentModel(ediDocument, 2, "846", "PO", true);

/////////////////////////////////////////////////////////////////////////////////
// populate the body of the document with the data we want in the EDI Document //
/////////////////////////////////////////////////////////////////////////////////
var body = JSON.parse(ediDocument.jsonBody);

var itemList = infoplusApi.search("Item", "lobId eq 10008", null, null, null);
for(var i=0; i<itemList.size(); i++)
{
var item = itemList.get(i);
body.push(["BIA","00","DD", "000", "20190715"]);
body.push(["DTM","040", "20190715"]);

body.push(["LIN","1", "SK", String(item.sku)]);
body.push(["PID","F","","","", String(item.itemDescription)]);
body.push(["SDQ", "EA", "54", "", String(item.onHandQuantity)]);
}

body.push(["CTT", itemList.size()]);

//////////////////////////////////////////////////////////////////////////////////////////
// note: EDI document footers will be automatically generated and do not need populated //
//////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////
// set the populated body of the EDI Document we have created //
////////////////////////////////////////////////////////////////
utils.log("body: " + body);
ediDocument.jsonBody = JSON.stringify(body);
utils.log("json body: " + ediDocument.jsonBody);

/////////////////////////////////////////////
// store the EDI Document via Infoplus API //
/////////////////////////////////////////////
var ediDocument = infoplusApi.add("eDIDocument", ediDocument);

//////////////////////////////////////////////
// send the document via Infoplus EDI Utils //
//////////////////////////////////////////////
ediUtils.sendOutboundDocument(ediDocument);

utils.log("Done");

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.  

///////////////////////////////////////////////////////////////////////////
// get a value from the PO4 segment - put it in a line item custom field //
///////////////////////////////////////////////////////////////////////////
var po1LoopArray = ediUtils.getPO1ItemDataLoopArray(documentModel);
for(var i=0; i<po1LoopArray.length(); i++)
{
var po1Loop = po1LoopArray.get(i);
for(var j=0; j<po1Loop.length(); j++)
{
var segment = po1Loop.get(j);
if(segment.get(0) == "PO4")
{
utils.log("Found PO4 segment, with PO401 value: " + segment.get(1));
order.lineItems.get(i).customFields.put("po401", segment.get(1));
}
}
}