Append IFTSTA messages into same interchange.

Post author
Hayo

We are working for a while with ediFabric and are pleased with it, however now we are running into an issue. We have gotten the request to write multiple IFTSTA messages in 1 file with 1 interchange (UNB, UNH). 
Is there a way to do this, I was working with reading and writing, but somehow couldn't figure it out. 
How to combine the reading with the writing to the same file. In general, the setup will be writing a message every second so I have to consider locking of the file. 

Comments

4 comments

  • Comment author
    Admin

    Hello Hayo,

    You can write any number of messages to a stream. If you know how to do it with one then you know how to do it with more, just write each message to the stream. In the SDK (download from here), you can find multiple examples on writing EDI messages.

    This one in particularWriteMultipleInvoices() example, shows how to write two transactions.

    using (var stream = new MemoryStream())
    {
    using (var writer = new EdifactWriter(stream))
    {
    writer.Write(SegmentBuilders.BuildUnb("1"));

    // 1. Write the first invoice
    writer.Write(EdifactTransactionBuilders.BuildInvoice("1"));

    // 2. Write the second invoice
    writer.Write(EdifactTransactionBuilders.BuildInvoice("2"));

    // 3. Write any subsequent invoices...
    }
    }
    0
  • Comment author
    Hayo

    Hello,

    Thank you, in this situation, you write all of them at the same time to a file. 
    What i am looking at is, that at 8am, we start the file with interchange and 1 message
    at 8:05am we append a message to the file.
    At 8:10am we append a message and close the interchange

    Now based on this i could read the file, empty it and rewrite it. But is there another way? that just adds a message to an existing interchange in a file

    0
  • Comment author
    Admin
    • Edited

    If you need to write messages to the same file at different times, then you can't use the writer as it automatically closes the headers.

    So you'll have to build UNB segment yourself as a string (we can add this feature in the next version) and do:

    1. Create UNB as string and write it to a file, let's say C:\myfile.txt

    string unb = // create it

    File.CreateAllText("C:\myfile.txt", unb);

    2. After some time, create an instance of TSIFTSTA 

    TSIFTSTA iftsta_1 = // create the object

    3. Turn it to a string, set separators, postfixes, etc, if you need to in EdifactWriterSettings

    string edi_1 = iftsta_1.ToEdi(new EdifactWriterSettings());

    4. Append the string to the file

    File.AppendAllText("C:\myfile.txt", edi_1);

    5. After some time create another iftsta and append it to the file:

    TSIFTSTA iftsta_2 = // create it

    string edi_2 = iftsta_2.ToEdi(new EdifactWriterSettings());

    File.AppendAllText("C:\myfile.txt", edi_2);

    6. Append as many IFTSTA as you need...

    7. Manually create a UNZ as a string and write it to the end of the file (either maintain a counter or count the UNH occurrences in the file):

    string unz = // create it

    File.AppendAllText("C:\myfile.txt", unz);
    0
  • Comment author
    Hayo

    Thank you, i got it working.

    0

Please sign in to leave a comment.