cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Persisting an Infotable property

jremb
14-Alexandrite

Persisting an Infotable property

I had a use case where I needed to add to an Infotable property by parsing the contents of a CSV file. For the rest of the community, I thought it might be useful to share the code that I used to make sure the changes persisted in the Infotable property.

 

//// Begin example

// Read in the CSV file using the standard snippet:

var params = {

               longitudeField: undefined /* NUMBER /,

               fieldDelimiter: undefined / STRING /,

               path: CSVName / CSV filename -- STRING /,

               dateFormat: "MM/dd/yyyy HH:mm:ss" / STRING /,

               stringDelimiter: undefined / STRING /,

               dataShape: "ResultsDS" / DATASHAPENAME /,

               columnMappings: undefined / STRING /,

               hasHeader: true / BOOLEAN /,

               latitudeField: undefined / NUMBER /,

               fileRepository: "DemoRepository" / THINGNAME /

};

// result: INFOTABLE

var result = Resources["CSVParserFunctions"].ReadCSVFile(params);


// Now loop through and record to Infotable property:

var tablelength = result.getRowCount();


for (var x = 0; x < tablelength; x++) {

    var row = result.getRow(x);

    // ResultsDS entry object

    var newEntry = new Object();

    newEntry.TimeStamp = row.TimeStamp; // DATETIME

    newEntry.Parameter1 = row.Parameter1; // NUMBER

    newEntry.Parameter2 = row.Parameter2; // NUMBER

    newEntry.Parameter3 = row.Parameter3; // NUMBER

    newEntry.Index = row.Index; // STRING - isPrimaryKey = true

    me.MyInfoTableProperty.AddRow(newEntry);

};


// The following code will allow the additions to the infotable property to persist across a shutdown.

var TABLE = me.MyInfoTableProperty;

/ CORRECTION: The following code is not needed when doing it this way:

    TABLE.AddRow({

    TimeStamp : TimeStamp,

    Parameter1 : Parameter1,

    Parameter2 : Parameter2,

    Parameter3 : Parameter3,

    Index: Index

});

*/

me.MyInfoTableProperty = TABLE;

 

//// End example

 

It works, but if anyone has suggestions to improve it let me know.

2 REPLIES 2
paic
1-Newbie
(To:jremb)

Thanks Jeffrey,

Indeed similar to our training example, the only way to persist an InfoTable property is to write it out to a variable and then write it back to the property, since the system optimizes InfoTables by generally treating them as rapid access in memory tables.
Note that this also applies to JSON properties.

jremb
14-Alexandrite
(To:jremb)

I just edited my original post with a correction (see comment toward the end of the script). If those lines of code are used at that point and in that manner in the script, the infotable will not persist. One would use that technique only if using .AddRow() directly to the temporary table in-line with the main script body, not after as I originally had it. 




Top Tags