AnsweredAssumed Answered

Javascript executed twice

Question asked by tytanix on Nov 25, 2013
Latest reply on Nov 27, 2013 by tytanix
Hello, my alfresco executes my scripts twice every time and i can't understand why. I've created this script:

<blockcode>
function main()
{
   
   var spaceName="Sites/trilogis/documentLibrary";
   var  rootSpaceName=companyhome.childByNamePath(spaceName + "/Offerte");
   
   if(rootSpaceName == null)
   {
      spaceName = companyhome.childByNamePath("Sites/trilogis/documentLibrary");
      rootSpaceName = spaceName.createFolder("Offerte");
   }
      
   
            var childDate = document.properties["tr:date"];
            
            if(childDate == null)
            {
               var date = new Date();
               var childYear = date.getFullYear();
            }
            else
            {
               var childYear = childDate.getFullYear();
            }
            
            var newSpaceYear = rootSpaceName.childByNamePath("/" + childYear);
            
            if(newSpaceYear == null)
            {
               newSpaceYear = rootSpaceName.createFolder(childYear);
            }
            
            var childClient = document.properties["tr:name_cli"];
            
            if(childClient == null)
            {
               childClient = "Default Client";
            }
            
            var newSpaceClient = newSpaceYear.childByNamePath("/" + childClient);
            
            if(newSpaceClient == null)
            {
               newSpaceClient = newSpaceYear.createFolder(childClient);
            }
            
            var parentFolder = document.parent;
            
            document.move(newSpaceClient);
         
            var childList = parentFolder.children;
            var element = childList.length;
            
            if(element === 0)
            {
               parentFolder.remove();
            }
      

   return;
   
}

main();
</blockcode>

I've associated it to a rule for which, when a document having type "Offerta" is updated, the scripts start and create right path to store the document. I've extended my alfresco in a way that allow the user to chose the document type in the upload form and, after uploading he is sending directly to the edit-metadata page. After clicking on save, the script starts.
Inspecting the code with many logger.log, i've noticed that the script is executed twice. The first time with all variables set to null, the second time the variables have the right content. In the first execution, the document is moved to a path created by default in the second, it is moved to the right path. So i've had to add the remove function to delete the default path because it was always empty.
Someone can explain me why my script is executed twice instead of once?

Outcomes