Skip to main content
1-Visitor
Oct 15, 2018 7:07 PM UTC
Solved

Programmatically add a property to a Thing Template

Adding a property definition to a Thing programmatically requires a Thing restart in order for the property to come up (https://community.ptc.com/t5/ThingWorx-Developers/Unable-to-add-new-property-programatically-using-service/m-p/553678#M29081). Is there a similar process for a Thing Template? There is no "RestartThing" service on the template and it doesn't look like my properties that I add programmatically work.

1-Visitor
Oct 17, 2018 1:16 PM UTC

Yes you will have to.

 

If you manually add a property to a Thing Template you have to save the changes. When a Thing Template is saved, all the Things with that Thing Template are automatically restarted. In this case, you are not saving so the Things are not restarting. Here is the improved generalized code snippet:

try{
 Things["SimpleTest"].property = 5;
 
 }catch(err){
 var error=err.message;
 if(error.match(/not found on SimpleTest/)!=null){
 var params = {
 defaultValue: undefined /* STRING */,
 remoteBindingAspects: undefined /* JSON */,
 description: undefined /* STRING */,
 readOnly: undefined /* BOOLEAN */,
 type: "NUMBER" /* BASETYPENAME */,
 remote: undefined /* BOOLEAN */,
 remotePropertyName: undefined /* STRING */,
 timeout: undefined /* INTEGER */,
 pushType: undefined /* STRING */,
 dataChangeThreshold: undefined /* NUMBER */,
 logged: undefined /* BOOLEAN */,
 name: "property" /* STRING */,
 pushThreshold: undefined /* NUMBER */,
 dataChangeType: undefined /* STRING */,
 category: undefined /* STRING */,
 persistent: true /* BOOLEAN */,
 dataShape: undefined /* DATASHAPENAME */
 };

 // no return
 ThingTemplates["SimpleTestTemplate"].AddPropertyDefinition(params);
 
 // no return
 Things["SimpleTest"].RestartThing();

	}
}

 Kind regards,

Ezanne

4 replies

1-Visitor
Oct 16, 2018 7:19 AM UTC

Hi agondek

 

I have just tried to add a property to a Thing Template without restarting it and it was successful. I used the ADDPropertyDefinition service under the Thing Template that I wanted to add the property to. Could you please attach the script you are using or any error messages that you are receiving.

What version of ThingWorx are you using?

 

Kind regards,

Ezanne 

Translate
agondek1-VisitorAuthor
1-Visitor
Oct 16, 2018 8:28 PM UTC
 try{
 Things["ImplementedThing"].property = value;
 }
 catch(err){
 var error=err.message;
 if(error.match(/not found on ImplementedThing/)!=null){
 var params = {
 defaultValue: undefined /* STRING */,
 remoteBindingAspects: undefined /* JSON */,
 description: undefined /* STRING */,
 readOnly: undefined /* BOOLEAN */,
 type: "NUMBER" /* BASETYPENAME */,
 remote: undefined /* BOOLEAN */,
 remotePropertyName: undefined /* STRING */,
 timeout: undefined /* INTEGER */,
 pushType: undefined /* STRING */,
 dataChangeThreshold: undefined /* NUMBER */,
 logged: undefined /* BOOLEAN */,
 name: property /* STRING */,
 pushThreshold: undefined /* NUMBER */,
 dataChangeType: undefined /* STRING */,
 category: undefined /* STRING */,
 persistent: undefined /* BOOLEAN */,
 dataShape: undefined /* DATASHAPENAME */
 };

 // no return
 ThingTemplates["Template"].AddPropertyDefinition(params);
}

Generalized code snippet above. I'm finding that on the next iteration of this service, it attempts to add the property again (ie: the property is still not found on the implemented thing after adding it to the template) Do I need to restart the Implemented Thing?

Translate
1-Visitor
Oct 17, 2018 1:16 PM UTC

Yes you will have to.

 

If you manually add a property to a Thing Template you have to save the changes. When a Thing Template is saved, all the Things with that Thing Template are automatically restarted. In this case, you are not saving so the Things are not restarting. Here is the improved generalized code snippet:

try{
 Things["SimpleTest"].property = 5;
 
 }catch(err){
 var error=err.message;
 if(error.match(/not found on SimpleTest/)!=null){
 var params = {
 defaultValue: undefined /* STRING */,
 remoteBindingAspects: undefined /* JSON */,
 description: undefined /* STRING */,
 readOnly: undefined /* BOOLEAN */,
 type: "NUMBER" /* BASETYPENAME */,
 remote: undefined /* BOOLEAN */,
 remotePropertyName: undefined /* STRING */,
 timeout: undefined /* INTEGER */,
 pushType: undefined /* STRING */,
 dataChangeThreshold: undefined /* NUMBER */,
 logged: undefined /* BOOLEAN */,
 name: "property" /* STRING */,
 pushThreshold: undefined /* NUMBER */,
 dataChangeType: undefined /* STRING */,
 category: undefined /* STRING */,
 persistent: true /* BOOLEAN */,
 dataShape: undefined /* DATASHAPENAME */
 };

 // no return
 ThingTemplates["SimpleTestTemplate"].AddPropertyDefinition(params);
 
 // no return
 Things["SimpleTest"].RestartThing();

	}
}

 Kind regards,

Ezanne

Translate
1-Visitor
Oct 16, 2018 9:14 AM UTC

The template does not need a restart, as it does not store the values of the parameters, only their description

Translate