<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Webscript not creating nodes in Alfresco Forum</title>
    <link>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131650#M35512</link>
    <description>&lt;P&gt;Finally! it worked, thank you very much,&amp;nbsp;&lt;A class="" href="https://hub.alfresco.com/t5/user/viewprofilepage/user-id/94701" target="_self" rel="nofollow noopener noreferrer"&gt;&lt;SPAN class=""&gt;prorobin&lt;/SPAN&gt;&lt;/A&gt;&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;))&lt;/P&gt;</description>
    <pubDate>Mon, 16 Jan 2023 10:11:16 GMT</pubDate>
    <dc:creator>tcuser</dc:creator>
    <dc:date>2023-01-16T10:11:16Z</dc:date>
    <item>
      <title>Webscript not creating nodes</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131646#M35508</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;We're working in a webscript that creates custom type nodes inside a folder. Alfresco 7.1 installed.&lt;/P&gt;&lt;P&gt;The problem appears when trying to create a node with the same name. This can happen, since the names are read from a list that can have repeated records. The webscript should avoid this behaviour by cheking if the node just created exists, but alfresco doesn't find it and if we try to catch de duplicatedNodeNameException to handle the error, when the webscript ends the structure is not created.&lt;/P&gt;&lt;P&gt;If the list does not contain repeated records the webscript works like charm and the webscript creates the nodes correctly. It seems that, even handling the exception, Alfresco makes a rollback somehow.&lt;/P&gt;&lt;P&gt;The custom type is therefore well defined in our model and looks like this:&lt;/P&gt;&lt;PRE&gt;&amp;lt;type name="tc:caja"&amp;gt;
    &amp;lt;title&amp;gt;Caja&amp;lt;/title&amp;gt;
    &amp;lt;parent&amp;gt;cm:cmobject&amp;lt;/parent&amp;gt;
    &amp;lt;properties&amp;gt;
        &amp;lt;property name="tc:uiAgtcCaja"&amp;gt;
            &amp;lt;type&amp;gt;d:int&amp;lt;/type&amp;gt;
            &amp;lt;index enabled="true"&amp;gt;
                &amp;lt;atomic&amp;gt;false&amp;lt;/atomic&amp;gt;
                &amp;lt;stored&amp;gt;false&amp;lt;/stored&amp;gt;
                &amp;lt;tokenised&amp;gt;both&amp;lt;/tokenised&amp;gt;
            &amp;lt;/index&amp;gt;
        &amp;lt;/property&amp;gt;
        &amp;lt;property name="tc:cajaLibre"&amp;gt;
            &amp;lt;title&amp;gt;Caja libre&amp;lt;/title&amp;gt;
            &amp;lt;type&amp;gt;d:boolean&amp;lt;/type&amp;gt;
            &amp;lt;mandatory&amp;gt;true&amp;lt;/mandatory&amp;gt;
            &amp;lt;default&amp;gt;true&amp;lt;/default&amp;gt;
        &amp;lt;/property&amp;gt;
    &amp;lt;/properties&amp;gt;
&amp;lt;/type&amp;gt;&lt;/PRE&gt;&lt;P&gt;I've developed a dummy ws to recreate the case, with this descriptor:&lt;/P&gt;&lt;PRE&gt;&amp;lt;webscript&amp;gt;
    &amp;lt;shortname&amp;gt;test&amp;lt;/shortname&amp;gt;
    &amp;lt;description&amp;gt;test&amp;lt;/description&amp;gt;
    &amp;lt;url&amp;gt;es/mycompany/transfer/test&amp;lt;/url&amp;gt;
    &amp;lt;authentication runas="admin"&amp;gt;user&amp;lt;/authentication&amp;gt;
&amp;lt;/webscript&amp;gt;&lt;/PRE&gt;&lt;P&gt;The js code:&lt;/P&gt;&lt;PRE&gt;function main() {

    //Search for the boxes root folder (CAJAS) and create if it does not exist
    var boxesFolderQuery = {
        query: "PATH:\"/app:company_home/cm:CAJAS\"",
        language: "fts-alfresco",
        onerror: "no-results",
    };      
    var boxesFolder = search.query(boxesFolderQuery)[0];
    if (boxesFolder == null) {
        boxesFolder = companyhome.createNode('CAJAS', 'cm:folder');
    }
    logger.debug("Boxes Folder created: " + boxesFolder.nodeRef);
    
    //Create a box inside boxes root folder
    var tmpBox = boxesFolder.createNode(1000, "tc:caja");
    tmpBox.properties["tc:uiAgtcCaja"] = 1000;
    tmpBox.properties["tc:cajaLibre"] = false;
    tmpBox.save();
    logger.debug("Box created: " + tmpBox.nodeRef);
    
    //Search for the box just created
    var boxQuery = {
        query: "TYPE:\"tc:caja\" AND @cm:name:1000",
        language: "fts-alfresco",
        onerror: "no-results",
    };    
    var box = search.query(boxQuery)[0]; //This should return at least 1 result, but it turns out that is null
    
    //Alfresco attempts to create the node again and the duplicatedChildNodeNameException is managed so the webscript does not fail
    if (box == null) {
        try {
            boxesFolder.createNode(1000, "tc:caja");
        } catch (error) {
            logger.warn(error);
        }
    }
        
}

main();&lt;/PRE&gt;&lt;P&gt;Caja means box in Spanish&amp;nbsp;&lt;img id="smileylol" class="emoticon emoticon-smileylol" src="https://connect.hyland.com/i/smilies/16x16_smiley-lol.png" alt="Smiley LOL" title="Smiley LOL" /&gt;&lt;/P&gt;&lt;P&gt;The view simply prints "ok" if the webscript doesn't fail. And it prints "ok" always, by the way, even if the folder and every node inside it are not created.&lt;/P&gt;&lt;P&gt;if I run the webscript in debug mode, the log shows:&lt;/P&gt;&lt;PRE&gt;2023-01-13 11:55:55,703 DEBUG [org.alfresco.repo.jscript.ScriptLogger] [http-nio-8080-exec-11] Boxes Folder created: workspace://SpacesStore/59d9436f-92c3-4a1e-b409-e44d4aeda3ba
2023-01-13 11:55:55,738 DEBUG [org.alfresco.repo.jscript.ScriptLogger] [http-nio-8080-exec-11] Box created: workspace://SpacesStore/5e9076c6-d716-415a-9c43-833f0f2deb18
2023-01-13 11:55:55,806 WARN  [org.alfresco.repo.jscript.ScriptLogger] [http-nio-8080-exec-11] JavaException: org.alfresco.service.cmr.repository.DuplicateChildNodeNameException: Duplicate child name not allowed: 1000&lt;/PRE&gt;&lt;P&gt;Hope someone can help me, thank you very much in advance &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 14:01:12 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131646#M35508</guid>
      <dc:creator>tcuser</dc:creator>
      <dc:date>2023-01-13T14:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: Webscript not creating nodes</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131647#M35509</link>
      <description>&lt;P&gt;The error is handled in the catch block by just logging it as a warn message&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;try {
            boxesFolder.createNode(1000, "tc:caja");
        } catch (error) {
            &lt;U&gt;logger.warn(error);&lt;/U&gt;
        }&lt;/PRE&gt;&lt;P&gt;And as you see the the message is logged well.&lt;BR /&gt;&lt;BR /&gt;if you want to return "KO", you can, for example, type&amp;nbsp;&lt;STRONG&gt;model["status"] = "KO"&lt;/STRONG&gt; in the &lt;U&gt;catch block&lt;/U&gt; and then handle it in the freemarker file.&lt;BR /&gt;You can add a message property to the model to show details about error in the result of the webscript.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jan 2023 15:19:55 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131647#M35509</guid>
      <dc:creator>malekgn</dc:creator>
      <dc:date>2023-01-13T15:19:55Z</dc:date>
    </item>
    <item>
      <title>Re: Webscript not creating nodes</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131648#M35510</link>
      <description>&lt;P&gt;I don't want to return a KO value, what I posted is just a dummy ftl to recreate the problem we're facing... I want the webscript to create the nodes. The message is logged well, of course, but the ws is not doing what it's&amp;nbsp;meant to.&lt;/P&gt;&lt;P&gt;Thank you anyway for your response &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jan 2023 09:28:54 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131648#M35510</guid>
      <dc:creator>tcuser</dc:creator>
      <dc:date>2023-01-16T09:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: Webscript not creating nodes</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131649#M35511</link>
      <description>&lt;P&gt;Fts queries results are based on Solr indexes.&amp;nbsp;If the node has just been created it is possible that Solr has not indexed it yet.&lt;/P&gt;&lt;P&gt;Using&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;boxesFolder.createNode(1000, "tc:caja");&lt;/PRE&gt;&lt;P&gt;already return the ScriptNode created.&lt;/P&gt;&lt;P&gt;I suggest using&lt;/P&gt;&lt;PRE&gt;tmpBox.exists()&lt;/PRE&gt;&lt;P&gt;to check if the node has been created correctly.&lt;/P&gt;&lt;P&gt;Alternately, you can use CMIS queries or using methods like&lt;/P&gt;&lt;PRE&gt;boxesFolder.childByNamePath("1000")&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Jan 2023 10:00:59 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131649#M35511</guid>
      <dc:creator>prorobin</dc:creator>
      <dc:date>2023-01-16T10:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: Webscript not creating nodes</title>
      <link>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131650#M35512</link>
      <description>&lt;P&gt;Finally! it worked, thank you very much,&amp;nbsp;&lt;A class="" href="https://hub.alfresco.com/t5/user/viewprofilepage/user-id/94701" target="_self" rel="nofollow noopener noreferrer"&gt;&lt;SPAN class=""&gt;prorobin&lt;/SPAN&gt;&lt;/A&gt;&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://connect.hyland.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;))&lt;/P&gt;</description>
      <pubDate>Mon, 16 Jan 2023 10:11:16 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/alfresco-forum/webscript-not-creating-nodes/m-p/131650#M35512</guid>
      <dc:creator>tcuser</dc:creator>
      <dc:date>2023-01-16T10:11:16Z</dc:date>
    </item>
  </channel>
</rss>

