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

Unable to create Thing via Java Extension

Raccone
10-Marble

Unable to create Thing via Java Extension

Dear experts,

 

I am trying to create a Thing from a Service developed in an extension. I could easily do it in another extension by using: 

 

 

es.CreateThing(thingName, null, null, thingTemplateName);

 

 

Where es is an instance of EntityServices. I am developing another extension now and for some reason it always throws an Exception, saying that the ThingTemplate does not exist. The template is defined into the Extension itself and it is correctly seen by the composer. Moreover, instantiating a Thing from that Template from the Composer works.

 

Thanks and Best Regards 

1 ACCEPTED SOLUTION

Accepted Solutions

Hi Vladmir,

 

I have identified the error. The point is that I think I was breaking the transaction internal to ThingWorx, as in the next row I was assigning the newly created Thing itself in the try block:

 

es.CreateThing(thingName, null, null, thingTemplateName);
userThing = ThingUtilities.findThing(thingName);

 

At least this is my explanation, and the only difference with the working pieces of code that I mentioned previously.  

View solution in original post

10 REPLIES 10

Low chance this may be a Visibility issue created by the User you're using to execute the Service?

Hi Vladmir,

 

It got even creepier after that, when I created a new extension with a simple service that created a Thing, and it worked, on the Template I was referencing in the post before and also others.

 

Could the previous extension be corrupted or something like that?

Hmm, I would not bet on the corruption. Maybe it's a crazy thing like the fact you passed an empty space in your templateName, or something like that. It happens a lot when you copy from Composer entity names. I always to do a trim for this reason. Other thing to keep in mind, if you have the chance, please connect your instance via JDWP to Eclipse so you can do proper debugging. Hard to do if the instance is not yours, but if you can, it's a life savior, especially seeing you're in the Extension teritory.

Hi Vladmir,

 

I have identified the error. The point is that I think I was breaking the transaction internal to ThingWorx, as in the next row I was assigning the newly created Thing itself in the try block:

 

es.CreateThing(thingName, null, null, thingTemplateName);
userThing = ThingUtilities.findThing(thingName);

 

At least this is my explanation, and the only difference with the working pieces of code that I mentioned previously.  

Ah, this is even more interesting. Typically you should do also in the extension code an EnableThing and then RestartThing, before using that entity - but for that you still need to get access to the Thing.

Can you try with my way of accessing the Thing, which is this one?

 

Thing rsc = (Thing) EntityUtilities.findEntity(Const.str_UtilityThingName,ThingworxRelationshipTypes.Thing); 

 

Then call EnableThing, and RestartThing, they are both Java methods.

Hi Vladmir,

 

Yes I both did Enable and Restart, because I have seen elsewhere that it's recommended before setting properties, and that worked indeed!

 

Anyway, I also tried the EntityUtilities.findEntity (I use it to retrieve Networks, for example). Is there any difference between these two? I thought that:

 

ThingUtilities.findThing(thingName);

 

Could have been better since it looks specifically for Things. But that's my wild guess.

 

Thanks!

Being frank, I don't know if they are different and I wasn't aware of the ThingUtilities before this thread

Coming back to a closure of this thread, what was the cause of your issue?

1. You tried to access the Thing immediately after creating it (regardless if you did Enable and Restart)

2. You used ThingUtilities instead of EntityUtilities

3. You did not do Enable and Restart after creating it

Thanks!

I'd stick with the guess from before, adding some context for you:

 

I tried to access the Thing in the same try/catch block where I called the CreateThing method. It resulted in the Service call returning a generic error, the Application Logs to only log a row with "null". I think it was breaking some kind of transaction because trying to recreate the Thing resulted in a "Thing with Name xxx already exists", even though it couldn't be seen from the Composer or any generic API. Upon instance restart, the transactions would most probably be aborted and you had no "Thing with Name xxx already exists" anymore.

slangley
23-Emerald II
(To:Raccone)

Hi @Raccone.

 

If one of the previous posts allowed you to find a solution, please mark the appropriate one as the Accepted Solution for the benefit of others in the community.

 

Regards.

 

--Sharon

I forgot, I fixed it now.

 

Thanks,

Federico

Top Tags