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

Code Snippet to check if any property already exist in thing

ranjank
15-Moonstone

Code Snippet to check if any property already exist in thing

Hi,

 

I want to check whether any property already exists in thing or not, if not then I will create a new property using AddPropertyDefinition.

 

Please provide the code snippet for this property existence check.

 

Thanks in advance.

ACCEPTED SOLUTION

Accepted Solutions

Some options :

 

1. Just set the property value without checking if it exists and handle the exception to create it if it does not exist.

 

2. Otherwise you have the JS service GetPropertyDefinition. Are you looking at properties defined on the Thing itself or also properties inherited from Thing Shape and Template ?

 

3. Finally, not al the public ThingWorx APIs are exposed as JS Service. The Java extension SDK exposes methods such as hasProperty, hasPropertyDefinition, .... in Thing.

View solution in original post

8 REPLIES 8

One approach would be to use GetPropertyDefinitions and then loop through the infotable returned by it.

var propertyTable = me.GetPropertyDefinitions({
category: undefined /* STRING */,
type: undefined /* BASETYPENAME */,
dataShape: undefined /* DATASHAPENAME */
});

var result = false;
var tableLength = propertyTable.rows.length;
for (var x=0; x < tableLength; x++) {
var row = propertyTable.rows[x];
if(row.name === 'temperature'){
result = true;
break;
}
}

 

 

ranjank
15-Moonstone
(To:rjanardan)

Hi @rjanardan ,

 

In my opinion, this is not a good approach because there are already many default properties and for my use-case, if the property encountered is not present then I have to create a new property (and if present just update its value). The no of properties that I have to create is more than 30.

 

Unnecessary looping will make the service slower. It would be better if any API is present that would take "propertyName" as input and just return "true or false" after checking its existence.

 

Pls guide.

Some options :

 

1. Just set the property value without checking if it exists and handle the exception to create it if it does not exist.

 

2. Otherwise you have the JS service GetPropertyDefinition. Are you looking at properties defined on the Thing itself or also properties inherited from Thing Shape and Template ?

 

3. Finally, not al the public ThingWorx APIs are exposed as JS Service. The Java extension SDK exposes methods such as hasProperty, hasPropertyDefinition, .... in Thing.

ranjank
15-Moonstone
(To:smainente)

@smainente 

 

Thank you for your suggestion, I already went with the 2nd option you provided. For now, the property required only on the thing.

 

But in 2nd option, I do have the desire to know what will be the approach for inherited properties from TS and Template?

ranjank
15-Moonstone
(To:smainente)

@smainente 

 

I tried option 3 i.e. hasProperty API also.

 

Code:-

 

// result: BOOLEAN
var result = me.hasProperty({
propertyName: propertyName /* STRING */
});

 

Output Type BOOLEAN

 

But the API is showing error:-

Error executing service testPropertyNew. Message :: TypeError: Cannot find function hasProperty in object com.thingworx.things.ConfiguredThing@21b2a2fe. - See Script Error Log for more details.

 

Pls, let me know If anything above is wrong.

1. hasProperty() is JAVA API not exposed as a JS Service. Only a subset of the ThingWorx APIs are exposed as JS service.

2. I tested GetPropertyDefinition( ) and it working with "inherited" properties

3. I would personally go for option 1 : assign the value and create the property in case of exception

 

You could also use GetNamedProperties and handle its exception when property is not found. 

Fastest way:

 

if (me["myPropertyName"]===undefined) {

  // -- Not defined, create the property.

}
Announcements


Top Tags