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

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Update a value in an infotable

Janicen
16-Pearl

Update a value in an infotable

Good day all,

 

I have a table that contains a timestamp field.

I want to replace the values in this field with a specific dataDifference calculation that I make. 

The two dates that I use to find the difference are inputs.

I have written the code below. It seems like the issue occurs when I assign the new timestamp value to the table. 

 

if (testMode) {
    // Get data directly from the property for testing purposes
    searchTable = Things["ThingName"].ChartData[0].Data;
} else {
    searchTable = data;
}
 
 
let params = {
t1: searchTable /* INFOTABLE */
};
let clonedTable = Resources["InfoTableFunctions"].Clone(params);
 
 
// Find the row in searchTable with the matching timestamp
let searchForTimestampRow = clonedTable.Find({timestamp: currentTimestamp});
if (searchForTimestampRow) {
    // Calculate duration
    let duration = dateDifference(currentTimestamp, otherTime);
 
    // Update the duration field; assuming you want to add a 'duration' field
   
    searchForTimestampRow.timestamp = duration;
}
 
// Update searchTable with the modified cloned data
searchTable = clonedTable;
result = searchTable;

 

 

Has anyone tried doing this before?

 

Your response is much appreciated.

4 REPLIES 4

You posted your code, but can you describe what "the issue" is? Do you get an error?

 

Also, when you have two timestamps, and you compute the difference, note that the result is not a timestamp, but a number.

// dateDifference(date1:DATETIME, date2:DATETIME):NUMBER
let difference = dateDifference(date1, date2);

So you probably can't assign that to a timestamp property,

Janicen
16-Pearl
(To:Rocko)

I do not get an error but my timestamp column does not update.

Well, as I said, you can't assign a NUMBER value to a TIMESTAMP column.

Your code doesn't look too bad, but you can put in loggings to see what is happening. Not sure what is cloning is for. Try this:

 

// setting up some sample data
let currentTimestamp=new Date();
let otherTime=dateAddMonths(new Date(),-2);
var searchTable = Resources["InfoTableFunctions"].CreateInfoTable();
searchTable.AddField({name: 'token', baseType: 'STRING'});
searchTable.AddField({name: 'duration', baseType: 'NUMBER'});
searchTable.AddField({name: 'timestamp', baseType: 'DATETIME'});
searchTable.AddRow({ token:"test",duration:0, "timestamp":currentTimestamp});

// your version would be:
//searchTable = testMode?Things["ThingName"].ChartData[0].Data:data;

let searchForTimestampRow = searchTable.Find({timestamp: currentTimestamp});
if (searchForTimestampRow) {
logger.warn("Found Row with timestamp "+currentTimestamp);
let duration = dateDifference(currentTimestamp, otherTime);
logger.warn("duration="+duration);
searchForTimestampRow.timestamp = dateAddYears(new Date(),1); // just to show it's updating
searchForTimestampRow.duration = duration; // duration is a number
}

result=searchTable;
Janicen
16-Pearl
(To:Rocko)

I found that using the row below did not work. 

searchTable.Find({timestamp: currentTimestamp});

 So instead I used an if statement to find the correct row using the timestamp field: if(searchTable.timestamp - currentTimestamp == 0){ 

// to calculate duration, instead of using the dateDifference snippet, I used:

let duration = 

currentTimestamp -  otherTime;

}

This worked well and my timestamp column gets updated by the duration values.

Announcements


Top Tags