5 Replies Latest reply on Apr 28, 2017 10:59 AM by polinao RSS
    albertoz Explorer

    Create template by snippet

    Hi, i need to create a template by snippet.

    I figured out this method but i can't see on the template list my new template and if i try to recreate it i receive the message "can't create template 'MyCustomNameTemplate' template already exist", i had this problem when i tryed to create a thing but i didn't restart it and enable it. Is there any similar method for templates ?

     

    var paramsRemoteThing = {

        name: "MyCustomNameTemplate",

        description: "my personal description",

        thingTemplateName: 'RemoteThing',

        tags: 'MyApp:Template'

    };

     

    Resources["EntityServices"].CreateThingTemplate(paramsRemoteThing);

     

    //here i need some code for enable template and see it in the template's list.

     

    thank you !!!

      • Re: Create template by snippet
        aanjan Heavyweight Champ

        Alberto, was that snippet part of a service that failed? If it says 'template already exists' and you can't see it, you've essentially created a ghost entity; that's an entity that exists in memory but is not committed to the database. You would always need to use a try-catch block around create scripts so that you avoid creating ghost entities.

         

        If you have ThingWorx 7.2.0+, please take a look at our Ghost Entities Cleaner extension in our Marketplace. Here is a KCS article that expands on the topic of detection, prevention and removal of said entities. With that being said, the create code you provided should work (on its own).

          • Re: Create template by snippet
            albertoz Explorer

            No the script did'nt failed, and for solving ghost entities i stopped tomcat and restart.

            But also with try catch i can't see the template on the template list so i imagine there is some code to enable it like creating things...

            • Re: Create template by snippet
              albertoz Explorer

              Anyone give me some help please?

                • Re: Create template by snippet
                  supandey Collaborator

                  Alberto i just tried reproducing this but it's working just fine (tested on 7.4.0), which TWX version are you on and with what user are you logged in to execute this?

                   

                  var params = {

                    name: "DemoThingTemplate2" /* STRING */,

                    description: "demoThing" /* STRING */,

                    thingTemplateName: "RemoteThing" /* THINGTEMPLATENAME */,

                    tags: undefined /* TAGS */

                  };

                   

                   

                  // no return

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

                  • Re: Create template by snippet
                    polinao Collaborator

                    I was able to create it using your exact code/snippet in a service. You don't need to enable it like when creating things:

                    Do you see any errors in the Application log?

                     

                    What version of Thingworx are you using? Versions of Tomcat and Java? Have you tried in a different browser?