Creating EDI data using NodeJS/JavaScript

Creating EDI data is simple using the EDILightWeightDocument component. Simply create an instance, set its loops and segments, and call its generateEDIData() method. The EDILightWeightDocument structure is based on a loop hierarchy. Loops contain segments and other loops. Segments contain elements. Elements contains data and composite elements.

This example creates an EDILightWeightDocument instance, set its formation and call its generateEDIData() method. We will create a simple X12 document with the following loop structure:

 

The Code

//Delimiters constants
const STAR = 42; //’*’
const COLON = 58 //’:’
const TILDA = 126 //’~’
 
const edi = require(‘rdpcrystal-edi-library’);
 
//Create a new instance
let doc = new edi.EDILightWeightDocument();
 
//Set its delimiters
doc.Delimiters.ElementTerminatorCharacter = STAR;
doc.Delimiters.CompositeTerminatorCharacter = COLON;
doc.Delimiters.SegmentTerminatorCharacter = TILDA;
 
//Automatically set current segment count in SE01
doc.AutoPlaceCorrectNumOfSegments = true;
 
//Write each segment in a new line
doc.EachSegmentInNewLine = true;
 
let interchangeLoop = doc.createLoop(“Interchange header”);
let isa = interchangeLoop.createSegment(“ISA”);
isa.addElement(“00”);
isa.addElement(” “);
isa.addElement(“00”);
isa.addElement(” “);
isa.addElement(“ZZ”);
isa.addElement(“InterchangeSenderID”);
isa.addElement(“ZZ”);
isa.addElement(“InterchangeReceiverID”);
isa.addElement(“070303”);
isa.addElement(“1804”);
isa.addElement(“U”);
isa.addElement(“00401”);
isa.addElement(“1”);
isa.addElement(“1”);
isa.addElement(“T”);
isa.addElement(“:”);
 
let functionalGroup = interchangeLoop.createLoop(“FunctionalGroup”);
let gs = functionalGroup.createSegment(“GS”);
 
gs.addElement(“T”);
gs.addElement(“SH”);
gs.addElement(“ApplicationSenderCode”);
gs.addElement(“ApplicationReceiverCode”);
gs.addElement(“2005”);
gs.addElement(“132334”);
gs.addElement(“1”);
gs.addElement(“X”);
gs.addElement(“004010”);
 
let transaction = functionalGroup.createLoop(“Transaction Header”);
let st = transaction.createSegment(“ST”);
st.addElement(“837”);
st.addElement(“123”);
st.addElement(“005010X222A1”);
 
let se = transaction.createSegment(“SE”);
se.addElement(“0”);
se.addElement(“123”);
 
let endfunctionalGroup = functionalGroup.createLoop(“EndFunctionalGroup”);
let ge = endfunctionalGroup.createSegment(“GE”);
ge.addElement(“0”);
ge.addElement(“1”);
 
let endInterchange = interchangeLoop.createLoop(“EndInterchange”);
let iea = endInterchange.createSegment(“IEA”);
iea.addElement(“0”);
iea.addElement(“1”);
 
//Display the new EDI document
console.log(doc.generateEDIData());

Output

ISA*00* *00*
*ZZ*InterchangeSenderID*ZZ*InterchangeReceiverID*070303*1804*U*00401*1*1*T*:~
GS*T*SH*ApplicationSenderCode*ApplicationReceiverCode*2005*132334*1*X*004010~
ST*837*123*005010X222A1~
SE*2*123~
GE*0*1~
IEA*0*1~

Now that was easy!

 

Take Charge Of EDI

RDPCrystal EDI Library

trial2