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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Update a value in an infotable

Tomellache2B
15-Moonstone

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.

3 REPLIES 3

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,

Tomellache2B
15-Moonstone
(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;
Top Tags