Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
Hi,
I have created Thingshape and added Infotable type property, assigned Datashape to Infotable property.
How to set default values to Infotable properties to Thingshape through programmatically?
Thank you in Advance.
Latha
Solved! Go to Solution.
Hi @vi1 ,
I tested in TWX 9.1.3 and 9.3.2 with below code and it seems I could pass default value with Infotable Type data to AddPropertyDefinition service successfully and the property created at ThingShape level with default value set without any issue.
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(WeatherDataShape)
let a = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName: "InfoTable",
dataShapeName: "WeatherDataShape"
});
// WeatherDataShape entry object
let newEntry = {
id: 1,// NUMBER [Primary Key]
date: undefined,// DATETIME
max_temp: 33,// NUMBER
min_temp: 22,// NUMBER
cold: undefined,// BOOLEAN
visibility: undefined,// INTEGER
wind: undefined,// LONG
events: undefined,// STRING
image: undefined,// IMAGELINK
location: undefined,// LOCATION
actions: undefined,// HTML
info: undefined// HYPERLINK
};
a.AddRow(newEntry);
ThingShapes["testThingShape"].AddPropertyDefinition({
defaultValue: a /* STRING */,
remoteBindingAspects: undefined /* JSON */,
description: undefined /* STRING */,
readOnly: undefined /* BOOLEAN */,
type: "INFOTABLE" /* BASETYPENAME */,
remote: undefined /* BOOLEAN */,
remotePropertyName: undefined /* STRING */,
timeout: undefined /* INTEGER */,
pushType: undefined /* STRING */,
dataChangeThreshold: undefined /* NUMBER */,
logged: undefined /* BOOLEAN */,
name: "test2" /* STRING */,
pushThreshold: undefined /* NUMBER */,
dataChangeType: undefined /* STRING */,
category: undefined /* STRING */,
persistent: true /* BOOLEAN */,
dataShape: "WeatherDataShape" /* DATASHAPENAME */
});
The only problem is that the service requires the same property name does not exist in advance. It cannot update existing property and set a default value to it.
Another workaround I guess is to use ThingShapes["xxx"].GetImplementingThings(); to get a list of instantiated things, loop through the entity list and update property values - this should have the same effect as setting default value at ThingShape level.
Hope that helps.
@vi1 : I doubt that setting default property which is an infotable base type is supported as of now. We have a service 'AddPropertyDefinition' through which we can add properties and also set a default value for it. We will have to pass default value as a 'String' data type only even if the property base type is NUMBER,STRING,DATETIME,JSON.
However this will not work with INFOTABLE as property base type .
Thank you for response.
Why do you need to set this programmatically?
If you are assigning the infotable after instantiation, you can use something like 'ThingStart' or some other event to write the values to your property.
Make sure the Property is set to persistent so it will retain its values on a restart.
Hi,
Thank you for response.
I want to provide that adding default values to Infotable through script, not from Thingworx composer. For that, I have created mashup for adding default values to Infotable parameters to Thingshape. I could not able to do to add values.
Thanks&Regards,
Vidyullatha
What is the use case for this though, in what sort of situation is this necessary for you?
I was looking at what you can do with the PropertyDefinition and I don't see a way to add a default infotable value.
Hi,
I am looking for adding default values to Infotable in Thingshape level. Looks like we can not add to infotable using service.
Thanks&Regards,
Latha
That tells me what you want to do, but I'd like to know why you have to do it this way.
Hi @vi1 ,
I tested in TWX 9.1.3 and 9.3.2 with below code and it seems I could pass default value with Infotable Type data to AddPropertyDefinition service successfully and the property created at ThingShape level with default value set without any issue.
// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(WeatherDataShape)
let a = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
infoTableName: "InfoTable",
dataShapeName: "WeatherDataShape"
});
// WeatherDataShape entry object
let newEntry = {
id: 1,// NUMBER [Primary Key]
date: undefined,// DATETIME
max_temp: 33,// NUMBER
min_temp: 22,// NUMBER
cold: undefined,// BOOLEAN
visibility: undefined,// INTEGER
wind: undefined,// LONG
events: undefined,// STRING
image: undefined,// IMAGELINK
location: undefined,// LOCATION
actions: undefined,// HTML
info: undefined// HYPERLINK
};
a.AddRow(newEntry);
ThingShapes["testThingShape"].AddPropertyDefinition({
defaultValue: a /* STRING */,
remoteBindingAspects: undefined /* JSON */,
description: undefined /* STRING */,
readOnly: undefined /* BOOLEAN */,
type: "INFOTABLE" /* BASETYPENAME */,
remote: undefined /* BOOLEAN */,
remotePropertyName: undefined /* STRING */,
timeout: undefined /* INTEGER */,
pushType: undefined /* STRING */,
dataChangeThreshold: undefined /* NUMBER */,
logged: undefined /* BOOLEAN */,
name: "test2" /* STRING */,
pushThreshold: undefined /* NUMBER */,
dataChangeType: undefined /* STRING */,
category: undefined /* STRING */,
persistent: true /* BOOLEAN */,
dataShape: "WeatherDataShape" /* DATASHAPENAME */
});
The only problem is that the service requires the same property name does not exist in advance. It cannot update existing property and set a default value to it.
Another workaround I guess is to use ThingShapes["xxx"].GetImplementingThings(); to get a list of instantiated things, loop through the entity list and update property values - this should have the same effect as setting default value at ThingShape level.
Hope that helps.
Thank you for providing above code. I am able to do both the option.
Regards,
Latha