The following code is best practice when creating any "entity" in Thingworx service script. When a new entity is created (like a Thing) it will be loaded into the JVM memory immediately, but is not committed to disk until a transaction (service) successfully completes. For this reason ALL code in a service must be in a try/catch block to handle exceptions. In order to rollback the create call the catch must call a delete for any entity created. In line comments give further detail.
try {
var params = {
name: "NewThingName",
description: "This Is A New Thing",
thingTemplateName: "GenericThing"
};
Resources["EntityServices"].CreateThing(params);
// Always enable and restart a new thing to make it active on the Platform
Things["NewThingName"].Enable();
Things["NewThingName"].Restart();
//Now Create an Organization for the new Thing
var params = {
topOUName: "NewOrgName",
name: "NewOrgName",
description: "New Orgianization for new Thing",
topOUDescription: "New Org Main"
};
Resources["EntityServices"].CreateOrganization(params);
// Any code that could potentially cause an exception should
// also be included in the try-catch block.
}
catch (err) {
// If an exception is caught, we need to attempt to delete everything
// that was created to roll back the entire transaction.
// If we do not do this a "ghost" entity will remain in memory
// We must do this in reverse order of creation so there are no dependency conflicts
// We also do not know where it failed so we must attempt to remove all of them,
// but also handle exceptions in case they were not created
try {
var params = {name: "NewOrgName"};
Resources["EntityServices"].DeleteOrganization(params);
}
catch(ex2) {//Org was not created
}
try {
var params = {name: "NewThingName"};
Resources["EntityServices"].DeleteThing(params);
}
catch(ex2) {//Thing was not created
}
}
Learning Paths combine guides into one experience to teach related skills efficiently from start to finish. Begin your learning journey to reduce time to proficiency to get up and running with ThingWorx quickly. Featured Learning Utilizing ThingWorx to Secure Your Aerospace and Defense Systems Connect and Monitor Industrial Plant Equipment Vehicle Predictive Pre-Failure Detection with ThingWorx Platform Industry Solutions Complex and Automatic Food and Beverage Systems Connect and Configure Industrial Devices and Systems Medical Device Service Monitor Factory Supplies and Consumables Using an Allen-Bradley PLC with ThingWorx Learn ThingWorx Getting Started on the ThingWorx Platform Design and Implement Data Models to Enable Predictive Analytics Customize UI and Display Options to Deploy Applications Azure MXChip Development Kit
Getting Started on the ThingWorx Platform Learning Path Learn hands-on how ThingWorx simplifies the end-to-end process of implementing IoT solutions. NOTE: Complete the following guides in sequential order. The estimated time to complete this learning path is 210 minutes. Get Started with ThingWorx for IoT Part 1 Part 2 Part 3 Part 4 Part 5 Data Model Introduction Configure Permissions Part 1 Part 2 Build a Predictive Analytics Model Part 1 Part 2