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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Dynamically create, enable, and restart Things in a webservice

TricoSupport
7-Bedrock

Dynamically create, enable, and restart Things in a webservice

I am trying to create a service that pulls a list of properties from a webservice to create multiple Things of the same type.  I am able to create multiple things in the webservice but cannot find a way to refer to the newly created things in the service so that I can enable and restart.

 

Here is the code I am trying to run:

///  Loops through a JSON object to extract properties for each thing

while (i < json_obj.devices.length)
{
var params = {
name: json_obj.devices[i].deviceId/* STRING */,
description: json_obj.devices[i].name /* STRING */,
thingTemplateName: 'XXXXXX' /* THINGTEMPLATENAME */
};

// no return
Resources["EntityServices"].CreateThing(params);
i++;

//

//Need to enable and restart newly created thing.  Not sure how to reference it 


}

ACCEPTED SOLUTION

Accepted Solutions

The handle is the name you assigned the thing when you created it:

 

    var params = {

        name: "NewThingName",

        thingTemplateName: "GenericThing"

    };

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

    Things["NewThingName"].Enable();

    Things["NewThingName"].Restart();

 

Here is some helpful info:

https://community.ptc.com/t5/ThingWorx-Code-Examples-Platform/Creating-a-Thing-in-Service-Script/td-p/533427

 

 

View solution in original post

3 REPLIES 3

I can think of a couple of different ways:

1. Enable and restart the Things in the same loop where you created them.

2. Create another loop with the same loop parameters you used to create them and enable/restart in that loop.

3. Save the name of the newly created things in an array and pass that array to a function that enables/restarts them  

I would like to do it in the loop.  Once I create them how do I call the service to enable and restart.  I'm not sure how to get the handle to the newly created thing.

The handle is the name you assigned the thing when you created it:

 

    var params = {

        name: "NewThingName",

        thingTemplateName: "GenericThing"

    };

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

    Things["NewThingName"].Enable();

    Things["NewThingName"].Restart();

 

Here is some helpful info:

https://community.ptc.com/t5/ThingWorx-Code-Examples-Platform/Creating-a-Thing-in-Service-Script/td-p/533427

 

 

Announcements


Top Tags