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

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

JavaScript CreateThing - Thing is not running

mateuszboc
1-Newbie

JavaScript CreateThing - Thing is not running

Hi,

I encounter problem when implementing service for creating Things based on my template.


Part of the script with changed strings:

var params = {

        thingTemplateName: 'XXXTemplate' /* THINGTEMPLATENAME /,</div><div>        description: 'description' / STRING /,</div><div>        name: 'xxx-11' / STRING */

    };

// no return

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

// no return

Things[newNodeName].EnableThing();


When I called another service via REST API I get an error:

Wrapped com.thingworx.common.exceptions.InvalidRequestException: Thing /xxx-11 is not running Cause: Thing /xxx-11 is not running

When I open this newly created Thing in Composer and save without changing anything - it works.

When I go to .../Thingworx/Things/xxx-11 it also exists. Webpage says it is enabled.

Where is the problem? How to run this new Thing?

3 REPLIES 3
adam11
5-Regular Member
(To:mateuszboc)

Hi Mateusz,

After programmatically creating a

Thing

(or any entity), you need to invoke two

Services

: (1)

EnableThing()

and (2)

RestartThing()

. Once you enable and restart the

Thing

, you are able to use it just as you would any

Thing

 created manually in Composer.


By the way, you'll want to make sure you are using proper error handling in

 

the

Service

 creating

Things

. For example, if your

Service

 throws an exception before the entire transaction is committed to the database, you'll wind up with "ghost"

Things

 that can only be removed by restarting Tomcat.


Regards,

Adam



Hi, Thanks. It works. I'm pretty sure I've tried RestartThing() yesterday... 


What do you mean by proper error handling as this functions (Create/Enable/RestartThing) have no return? Some small example? 



adam11
5-Regular Member
(To:mateuszboc)

Mateusz,


You'll want to use try-catch blocks to handle exceptions. Typically, you'll wrap your entire

Service

 in a try-catch, as uncaught exceptions from any code in your

Service

 will introduce "ghost"

Things

that only exist in memory.



Top Tags