You are viewing limited content. For full access, please sign in.

Question

Question

Workflow - script to generate XML file with variable number of datasets

asked on December 17, 2024 Show version history

Hello,

I have a situation where a 3rd party software system can accept XML files in order to import data. The file they're looking for looks like below:
 

<NewDataSet>
<Table>
<ID>1</ID>
<RecNumber>123456</RecNumber>
<Status>1</Status>
<Product>2250-265100</Product>
<Vendor>520405</Vendor>
<PO>1224-3</PO>
<DateTimeIn>2024-12-06T12:08:00-05:00</DateTimeIn>
<DateTimeOut>2024-12-06T13:01:00-05:00</DateTimeOut>
<VehicleID>6057</VehicleID>
<Complete>true</Complete>
<Operator>TestName</Operator>
<WeightIn>75080</WeightIn>
<WeightOut>28120</WeightOut>
<Custom1>274981</Custom1>
<Void>false</Void>
<POSequence>1</POSequence>
</Table>
</NewDataSet>

The idea is we'll populate these tags with document metadata. However, there is a multi-value field group that will dictate how many of these <NewDataSet> tags we'll have, and that's where I'm running into issues. So for example, the multi-value field group looks like this at the moment:


For each Product ID on a given document, I would need a whole set of the tags provided above, with the <Product> tag containing the "current" Product ID. I'm guessing they're going to add additional tags for <Quantity> and <Price> or something like that since those values are specific to each product, but they're not at that point yet. Most of the tags will contain static info that comes from single-value metadata fields.

So far, I can't get my script to iterate the way I want. I either get an empty <Product> tag or it contains all my values one after the other.

Snippet of my code so far. Has to be something with how I'm creating this list class? The file gets created, and I can get the single-value variables to appear properly, but I only ever get 1 <NewDataSet> and it either contains an empty <Product> tag or it lists the Product IDs one after the other.

protected override void Execute()

                {
                    // Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session

                    //Get the session object
                    Session lfsess = this.Connection;
                    //Get the current entry
                    EntryInfo ei = this.BoundEntryInfo;


                    //Get the field data for the entry
                    FieldValueCollection fvc = ei.GetFieldValues();

                    //List of products ot be used in the file
                        List<object> products = new List<object>
                        {
                            fvc["Product ID"]
                        };

                        // Create the root <NewDataSet> element
                        XElement newDataSet = new XElement("NewDataSet");

                        // Iterate over each product in the list
                        int id = 1;
                        foreach (var product in products)
                        {
                            // Create the <Table> element
                            XElement table = new XElement("Table");

                            // Add child elements for each <table> entry
                            table.Add(new XElement("ID", id));
                            table.Add(new XElement("RecNumber", GetTokenValue("Entry ID")));
                            table.Add(new XElement("Status", "1"));
                            table.Add(new XElement("Product", product));
                            table.Add(new XElement("Vendor", fvc["Vendor ID"]));
                            table.Add(new XElement("PO", "1224-3"));
                            table.Add(new XElement("DateTimeIn", "2024-12-06T12:08:00-05:00"));
                            table.Add(new XElement("DateTimeOut", "2024-12-06T13:01:00-05:00"));
                            table.Add(new XElement("VehicleID", "6057"));
                            table.Add(new XElement("Complete", "true"));
                            table.Add(new XElement("Operator", "OperatorName"));
                            table.Add(new XElement("WeightIn", "75080"));
                            table.Add(new XElement("WeightOut", "28120"));
                            table.Add(new XElement("Custom1", fvc["BOL Number"]));
                            table.Add(new XElement("Void", "false"));
                            table.Add(new XElement("POSequence", "1"));

                         // Add the <table> to <NewDataSet>
                            newDataSet.Add(table);

                            //Increment the ID for the next iteration
                            id++;
                        }

                        // Create an XDocument to wrap the root element
                        XDocument xmlDocument = new XDocument(newDataSet);

                        // Write the XMl to a file
                        string filePath = "C:\\output.xml";
                        xmlDocument.Save(filePath);

                        Console.WriteLine("XML file created at: " + Path.GetFullPath(filePath));
                    }

As you can probably tell, this kind of scripting is not my specialty. This is what I've been able to put together by looking at other code people have posted as well as a little help from ChatGPT. I know I'm asking a lot here, so any help is appreciated!

Thanks!
 

1 0

Replies

replied on December 18, 2024 Show version history

Ended up using the below code to make this work. It's possible I'll have more multi-value tokens that need to be included in each dataset, but I should be able to figure that out. I created a token in the Workflow that contains the multivalue token with all values separated by a comma, and then this script uses that token and splits the values on the comma. This resulted in a new set of <Table> tags for each Product ID I had in the document metadata. Most of the data I'm putting in this file with this code right now is static, explicitly defined data, but it won't be difficult to map some of those to field values instead like the <Custom1> tag or the <RecNumber> already do.
 

//List of products to be used in the file
                     string productIDs = GetTokenValue("ProductIDFlattened").ToString();
                     List<string> products = new List<string>(productIDs.Split(','));

                     // Create the root <NewDataSet> element
                     XElement newDataSet = new XElement("NewDataSet");

                     // Iterate over each product in the list
                     int id = 1;
                     foreach (object product in products)
                     {
                         // Create the <Table> element
                         XElement table = new XElement("Table");

                         // Add child elements for each <table> entry
                         table.Add(new XElement("ID", id));
                         table.Add(new XElement("RecNumber", GetTokenValue("Entry ID")));
                         table.Add(new XElement("Status", "1"));
                         table.Add(new XElement("Product", product));
                         table.Add(new XElement("Vendor", fvc["Vendor ID"]));
                         table.Add(new XElement("PO", "1224-3"));
                         table.Add(new XElement("DateTimeIn", "2024-12-06T12:08:00-05:00"));
                         table.Add(new XElement("DateTimeOut", "2024-12-06T13:01:00-05:00"));
                         table.Add(new XElement("VehicleID", "6057"));
                         table.Add(new XElement("Complete", "true"));
                         table.Add(new XElement("Operator", "OperatorName"));
                         table.Add(new XElement("WeightIn", "75080"));
                         table.Add(new XElement("WeightOut", "28120"));
                         table.Add(new XElement("Custom1", fvc["BOL Number"]));
                         table.Add(new XElement("Void", "false"));
                         table.Add(new XElement("POSequence", "1"));


 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.