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 an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Programmatically add Things

fgrondin
5-Regular Member

Programmatically add Things

I'd like to programmatically add new Things, but I want to have some validation to see if the Thing is already Created.

The validation I want to do is something like this

if(Things["ThingName"] == null)

{

    //Then Create the thing

}

but by doing that, I receive the error which tells me : "Cannot read Property "ThingName" from undefined"

Then seeing that, I Created another service that will only retrieve the Thing name / project Name

But both of them didn't work telling me the same error as before (Cannot read Property "ThingName" from undefined)

By the way, the thing is already created.

This is the code I used :

var result = Things["ThingName"].GetProjectName();

returning result as output

Then I tried with another Entity (tried ThingTemplate) and it worked.

So first, is the Things[""] bugged for me ?

Second, how could I add the validation I'm looking for?

Message was edited by: Frederik Grondin

1 ACCEPTED SOLUTION

Accepted Solutions
posipova
20-Turquoise
(To:fgrondin)

You may look into try/catch. For example, the following will create a thing based on the already existing template (for this particular case), then if error occurs, it will delete to ensure no ghost entities were created. EDIT:  or, since you are looking to create an entity if doesn't exist already, you may replace the deleteThing with CreateThing with the new name instead. The example is just a reference.

try {

var thing1 = {

              thingTemplateName: "TestingTemplate"/* THINGTEMPLATENAME */,

              name: InputName /* STRING */

};

// no return

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

  

} catch(err) {

var params = {

name: InputName /* THINGNAME */

};

// no return

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

   logger.error (err);

}

View solution in original post

2 REPLIES 2
posipova
20-Turquoise
(To:fgrondin)

You may look into try/catch. For example, the following will create a thing based on the already existing template (for this particular case), then if error occurs, it will delete to ensure no ghost entities were created. EDIT:  or, since you are looking to create an entity if doesn't exist already, you may replace the deleteThing with CreateThing with the new name instead. The example is just a reference.

try {

var thing1 = {

              thingTemplateName: "TestingTemplate"/* THINGTEMPLATENAME */,

              name: InputName /* STRING */

};

// no return

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

  

} catch(err) {

var params = {

name: InputName /* THINGNAME */

};

// no return

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

   logger.error (err);

}

fgrondin
5-Regular Member
(To:posipova)

I guess it would've worked, but I managed to find why my Things["Name"].XX wouldn't work.

The issue was, I had an Input called Things as well.

I guess it would take the input over the Entity Things Collection.

Top Tags