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

Creating properties inside Thing by service

ZbigniewK
12-Amethyst

Creating properties inside Thing by service

Needed to copy some properties from one Thing to other. To avoid manual work, created Service with AddPropertyDefinition (later wanted to add service to copy values). It creates no error when running first time, but properties are not generated. So, started script - and it answer "Property [name] already exists".

Manual refreshing of Thing in web service does not help. Restart Thing by code also makes no difference.

 

Here is sample code:

 

myList = me.GetPropertyDefinitions({
category: "MyCategory" /* STRING */,
type: undefined /* BASETYPENAME */,
dataShape: undefined /* DATASHAPENAME */
});
 
for each (var x in myList.rows) {
Things["MyThing"].AddPropertyDefinition({
indexed: undefined /* BOOLEAN {"defaultValue":false} */,
defaultValue: undefined /* STRING */,
remoteBindingAspects: undefined /* JSON */,
description: "my desc"/* STRING {"defaultValue":""} */,
readOnly: undefined /* BOOLEAN {"defaultValue":false} */,
type: 'INFOTABLE' /* BASETYPENAME */,
remote: undefined /* BOOLEAN {"defaultValue":false} */,
remotePropertyName: undefined /* STRING */,
timeout: undefined /* INTEGER {"defaultValue":0} */,
pushType: undefined /* STRING */,
accessModifier: undefined /* JSON */,
dataChangeThreshold: undefined /* NUMBER */,
logged: undefined /* BOOLEAN {"defaultValue":false} */,
name: x.name /* STRING */,
pushThreshold: undefined /* NUMBER */,
dataChangeType: undefined /* STRING */,
category: "MyCategory" /* STRING */,
persistent: true /* BOOLEAN {"defaultValue":false} */,
dataShape: 'myDataShape' /* DATASHAPENAME */
});
Things["MyThing"].RestartThing();
Things["MyThing"].EnableThing();
 
4 REPLIES 4

The line

category: MyCategory" /* STRING */,

misses the opening quote?

Also the line

description: ''my desc"/* STRING {"defaultValue":""} */,

uses two different types of quote, single and double.

ZbigniewK
12-Amethyst
(To:Rocko)

That happens during anonymising code. In original code, it's done correctly, if not - it would generate error. 

Velkumar
19-Tanzanite
(To:ZbigniewK)

Hi @ZbigniewK 

 

If you want to copy all the property, you can use below code

// result: INFOTABLE dataShape: "PropertyDefinition"
let thing1PropertyInfoTable = Things["T"].GetPropertyDefinitions({
	category: undefined /* STRING */,
	type: undefined /* BASETYPENAME */,
	dataShape: undefined /* DATASHAPENAME */
});

// status: INFOTABLE dataShape: "BulkProcessingReport"
let result = me.AddPropertyDefinitions({
	ignoreInvalidDefinitions: true /* BOOLEAN */,
	values: thing1PropertyInfoTable /* INFOTABLE */
});

 

To add specific properties

// result: INFOTABLE dataShape: "PropertyDefinition"
let result = Things["T"].GetPropertyDefinitions({
	category: undefined /* STRING */ ,
	type: undefined /* BASETYPENAME */ ,
	dataShape: undefined /* DATASHAPENAME */
});

let propertyList = ["prop1", "prop2", "G"];

result.rows.toArray().forEach(jsonValue => {
	if (propertyList.indexOf(jsonValue.name) !== -1) {
		me.AddPropertyDefinition({
			defaultValue: undefined /* STRING */ ,
			remoteBindingAspects: undefined /* JSON */ ,
			description: jsonValue.description /* STRING */ ,
			readOnly: jsonValue.isReadOnly /* BOOLEAN */ ,
			remote: undefined /* BOOLEAN */ ,
			type: jsonValue.baseType /* BASETYPENAME */ ,
			remotePropertyName: undefined /* STRING */ ,
			timeout: undefined /* INTEGER */ ,
			pushType: undefined /* STRING */ ,
			dataChangeThreshold: undefined /* NUMBER */ ,
			logged: jsonValue.isLogged /* BOOLEAN */ ,
			name: jsonValue.name /* STRING */ ,
			pushThreshold: undefined /* NUMBER */ ,
			dataChangeType: undefined /* STRING */ ,
			category: undefined /* STRING */ ,
			persistent: jsonValue.isPersistent  /* BOOLEAN */ ,
			dataShape: undefined /* DATASHAPENAME */
		});
	}
});

 

Note : Run above script in View Only mode

 

/VR

Hi @ZbigniewK,


I wanted to see if you got the help you needed.


If so, please mark the appropriate reply as the Accepted Solution. It will help other members who may have the same question.
Please note that industry experts also review the replies and may eventually accept one of them as solution on your behalf.
Of course, if you have more to share on your issue, please pursue the conversation.

Thanks,

Catalina
PTC Community Moderator
Announcements


Top Tags