Skip to main content
13-Aquamarine
January 10, 2023
Solved

Set DateTime to null programmatically

  • January 10, 2023
  • 1 reply
  • 3533 views

Hey there,

 

i have an infotable where i set the datetime and at a specific point this datetime need to be set to "null" or just unset programmatically. I tried to set to null, to Zero (still a datetime and not unset).

And then i found this: https://www.ptc.com/en/support/article/CS253788

 

I dont like to delete and insert again, i prefer to update. And it seems like there is no other solution than that.

 

For alot of my problems i see always only a workaround and no real solution and after a workaround is applied then noone cares anymore about this problem and this workaround will exist like for ever.

 

how is it possible that this workaround exists almost 2 years and no real solution got applied?

 

Best answer by wcui

I did below test with latest TW9.3.7.

  1. create a DataShape with Datetime field [DT] (not key field).
  2. create a DataTable with above DataShape
  3. run below service, got below results.

As we dont have plan to accept null or undefined for UpdateDataTableEntry service, I also suggest to set to a special value instead of null.

Please let us know if this workaround does not work for you and share more business background so we can help.

--------------------- service code --------------------

let dt1 = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({

infoTableName: "InfoTable",
dataShapeName: "cuiDS1"
});

 

let newEntry = {
ID: "1", // STRING [Primary Key]
Value: "1", // STRING
DT: *** // DATETIME 
};

dt1.AddRow(newEntry);

Things["cuiDT1"].UpdateDataTableEntry({
sourceType: undefined /* STRING */,
values: dt1 /* INFOTABLE */,
location: undefined /* LOCATION */,
source: undefined /* STRING */,
tags: undefined /* TAGS */
});

-----------result of GetDataTableEntries ----------

"2000-1-2" --->  2000-01-02 08:00:00.000

null              --->  failed to update

undefined  --->  failed to update

0                  --->  1970-01-01 08:00:00.000

1 reply

17-Peridot
January 10, 2023

tried with ThingWorx 9.1.11

let params = {
	infoTableName: undefined /* STRING */
};
let info = Resources["InfoTableFunctions"].CreateInfoTable(params);
let newField = {
 name: 'dt',
 baseType: 'DATETIME'
};
info.AddField(newField);

info.AddRow({"dt": new Date()});
info.AddRow({"dt": new Date()});
info.AddRow({"dt": new Date()});

info.rows[0].dt = null;
info.rows[1].dt = 0;

result = info;

got result as following,

Sathishkumar_C_0-1673352502898.png

 

13-Aquamarine
January 10, 2023

Thanks for your try. I tried this aswell.

Try to save it to an Infotable and then try to use "UpdateDataTableEntry". You will realize the infotable wont accept the "null" parameter and it will keep the old value.

17-Peridot
January 10, 2023

Yeah, that's correct. It's not updating as NULL in the DataTable. Let you know if i got anything related to that, other than delete and insert.