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
Solved! Go to Solution.
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.
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?
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.
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