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

How do I programmatically apply a Model Tag when using Resources["EntityServices"].CreateThing()?

SOLVED
Bedrock

How do I programmatically apply a Model Tag when using Resources["EntityServices"].CreateThing()?

I created a service to create Things programmatically from unbound remote Things. I modeled it upon the service created in the PTC University Learning Exchange tutorial, Programmatically Add Things and Automatically Assign an Identifier.

I extended the example a little bit, with the intent of adding model tags to the Things as they were created. Here's the code for my service:

var identifiers = {   maxItems: undefined /* NUMBER */ };  // result: INFOTABLE dataShape: EntityList var result = Resources["DeviceFunctions"].GetUnboundIdentifiers(identifiers);  for (var i = 0; i < result.getRowCount(); i++) {   var sensorName = result.getRow(i).name;   var params = {     tags: [   {       vocabulary: "Applications",       vocabularyTerm: "ThingWorxTraining"    }   ] /* TAGS */ ,     thingTemplateName: "RemoteThing" /* THINGTEMPLATENAME */ ,     description: undefined /* STRING */ ,     name: "SteamSensor" + sensorName /* STRING */   };   // no return   Resources["EntityServices"].CreateThing(params);   Things[params.name].EnableThing();   Things[params.name].RestartThing();    var ident = {     identifier: result.getRow(i).name /* STRING */   };    Things[params.name].SetIdentifier(ident);   Things[params.name].RestartThing(); }

My service creates the Things just fine. It doesn't tag my Things, though.

Here's what my, "tags," element looks like in the JSON param above:

 tags: [   {       vocabulary: "Applications",       vocabularyTerm: "ThingWorxTraining"   } ] /* TAGS */ 

To see what the ThingWorx Platform does, I opened up my developer tools, created a Thing, and watched what went across the wire. The request the Composer made to create my Thing contained the following JSON pertaining to the model tag:

"tags": [{     "vocabulary": "Applications",     "vocabularyTerm": "ThingWorxTraining",     "_magicPickerId": "d19dc787-cd85-4271-9d04-40719202e561" }]

The only differences I see are:

  • The keys are surrounded by double-quotes. This shouldn't matter; unquoted key names should be valid JSON.
  • The system includes a _magicPickerId element. I'm guessing that's because it knew that Id already, and that I'm not required to supply it.

Is there a problem with the syntax of my params variable that's preventing application of the model tag to my new Thing?

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: How do I programmatically apply a Model Tag when using Resources["EntityServices"].CreateThing()?

It seems this may be a possible bug. For now, after the thing is created, enabled, and restarted, you will need to use the SetTags() snippet.

View solution in original post

2 REPLIES 2
Highlighted

Re: How do I programmatically apply a Model Tag when using Resources["EntityServices"].CreateThing()?

It seems this may be a possible bug. For now, after the thing is created, enabled, and restarted, you will need to use the SetTags() snippet.

View solution in original post

Highlighted

Re: How do I programmatically apply a Model Tag when using Resources["EntityServices"].CreateThing()?

I need to get my testing infrastructure set back up, but this is the code I plan to test. I've moved the application of the tags from object creation to post-creation steps:


var identifiers = {


  maxItems: undefined /* NUMBER */


};



// result: INFOTABLE dataShape: EntityList


var result = Resources["DeviceFunctions"].GetUnboundIdentifiers(identifiers);



for (var i = 0; i < result.getRowCount(); i++) {


  var sensorName = result.getRow(i).name;


  var params = {


    thingTemplateName: "RemoteThing" /* THINGTEMPLATENAME */ ,


    description: undefined /* STRING */ ,


    name: "SteamSensor" + sensorName /* STRING */


  };


  // no return


  Resources["EntityServices"].CreateThing(params);


  Things[params.name].EnableThing();


  Things[params.name].RestartThing();



  var ident = {


    identifier: result.getRow(i).name /* STRING */


  };



  var params = {


    tags: [{


      vocabulary: "Applications",


      vocabularyTerm: "ThingWorxTraining"


    }] /* TAGS */


  };



  Things[params.name].SetTags(params);



  Things[params.name].SetIdentifier(ident);


  Things[params.name].RestartThing();


}


Announcements

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum!