Skip to main content
16-Pearl
June 16, 2022
Solved

How to set Default Values to Infotable properties to Thing shape through Programatically?

  • June 16, 2022
  • 3 replies
  • 3333 views

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

Best answer by TonyZhang

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.

3 replies

15-Moonstone
June 16, 2022

@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 .

vi116-PearlAuthor
16-Pearl
June 17, 2022

Thank you for response.

22-Sapphire I
June 16, 2022

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.

vi116-PearlAuthor
16-Pearl
June 17, 2022

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

22-Sapphire I
June 17, 2022

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.

TonyZhangCommunity ManagerAnswer
Support
June 28, 2022

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.

vi116-PearlAuthor
16-Pearl
July 7, 2022

Thank you for providing above code. I am able to do both the option.

 

Regards,

Latha