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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

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

vi1
15-Moonstone
15-Moonstone

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

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

1 ACCEPTED SOLUTION

Accepted Solutions
TonyZhang
13-Aquamarine
(To:vi1)

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.

View solution in original post

9 REPLIES 9
abjain
13-Aquamarine
(To:vi1)

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

vi1
15-Moonstone
15-Moonstone
(To:abjain)

Thank you for response.

PaiChung
22-Sapphire I
(To:vi1)

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.

vi1
15-Moonstone
15-Moonstone
(To:PaiChung)

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

PaiChung
22-Sapphire I
(To:vi1)

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.

vi1
15-Moonstone
15-Moonstone
(To:PaiChung)

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

PaiChung
22-Sapphire I
(To:vi1)

That tells me what you want to do, but I'd like to know why you have to do it this way.

TonyZhang
13-Aquamarine
(To:vi1)

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
15-Moonstone
15-Moonstone
(To:TonyZhang)

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

 

Regards,

Latha

Top Tags