cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript executed twice

tytanix
Champ in-the-making
Champ in-the-making
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?
4 REPLIES 4

yogeshpj
Star Contributor
Star Contributor
How you have configured the content rules ? Have you configured both inbound and update rule?

tytanix
Champ in-the-making
Champ in-the-making
I've configured only the "on update" rule on root and checked rule inheritance for subfolders.

yogeshpj
Star Contributor
Star Contributor
There must be two transaction which updates your content and that may be the one of the reason why your update rule called twice.Could you please tell me what exactly happens once you upload the document?

tytanix
Champ in-the-making
Champ in-the-making
When i upload a document, i can choose the document type before upload starting (i've modified the flash-upload.get.js).
Clicking on "ok" the navigateTo() function is called to display the edit-metadata page. After adding value to metadata i click "Save" button. I thought that the scripts would be executed after this operation but it seems not like that.