Webscript not creating nodes

cancel
Showing results for 
Search instead for 
Did you mean: 
tcuser
Customer

Webscript not creating nodes

Jump to solution

Hi all,

We're working in a webscript that creates custom type nodes inside a folder. Alfresco 7.1 installed.

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.

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.

The custom type is therefore well defined in our model and looks like this:

 

<type name="tc:caja">
    <title>Caja</title>
    <parent>cm:cmobject</parent>
    <properties>
        <property name="tc:uiAgtcCaja">
            <type>d:int</type>
            <index enabled="true">
                <atomic>false</atomic>
                <stored>false</stored>
                <tokenised>both</tokenised>
            </index>
        </property>
        <property name="tc:cajaLibre">
            <title>Caja libre</title>
            <type>d:boolean</type>
            <mandatory>true</mandatory>
            <default>true</default>
        </property>
    </properties>
</type>

 

I've developed a dummy ws to recreate the case, with this descriptor:

 

<webscript>
    <shortname>test</shortname>
    <description>test</description>
    <url>es/mycompany/transfer/test</url>
    <authentication runas="admin">user</authentication>
</webscript>

The js code:

 

 

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();

Caja means box in Spanish Smiley LOL

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.

 

if I run the webscript in debug mode, the log shows:

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

Hope someone can help me, thank you very much in advance Smiley Happy

1 Solution

Accepted Solutions
prorobin
Active Member

Re: Webscript not creating nodes

Jump to solution

Fts queries results are based on Solr indexes. If the node has just been created it is possible that Solr has not indexed it yet.

Using 

boxesFolder.createNode(1000, "tc:caja");

already return the ScriptNode created.

I suggest using

tmpBox.exists()

to check if the node has been created correctly.

 

Alternately, you can use CMIS queries or using methods like

boxesFolder.childByNamePath("1000")

 

View solution in original post

4 Replies
malekgn
Active Member II

Re: Webscript not creating nodes

Jump to solution

The error is handled in the catch block by just logging it as a warn message

try {
            boxesFolder.createNode(1000, "tc:caja");
        } catch (error) {
            logger.warn(error);
        }

 

And as you see the the message is logged well.

if you want to return "KO", you can, for example, type model["status"] = "KO" in the catch block and then handle it in the freemarker file.
You can add a message property to the model to show details about error in the result of the webscript.

tcuser
Customer

Re: Webscript not creating nodes

Jump to solution

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 meant to.

Thank you anyway for your response Smiley Happy

prorobin
Active Member

Re: Webscript not creating nodes

Jump to solution

Fts queries results are based on Solr indexes. If the node has just been created it is possible that Solr has not indexed it yet.

Using 

boxesFolder.createNode(1000, "tc:caja");

already return the ScriptNode created.

I suggest using

tmpBox.exists()

to check if the node has been created correctly.

 

Alternately, you can use CMIS queries or using methods like

boxesFolder.childByNamePath("1000")

 

tcuser
Customer

Re: Webscript not creating nodes

Jump to solution

Finally! it worked, thank you very much, prorobin Smiley Happy))