cancel
Showing results for 
Search instead for 
Did you mean: 

Share script requires a manual re-run to affect all nodes

srowsell
Champ in-the-making
Champ in-the-making
I have a share script which is run as a rule on a folder within a site's document library, and I will paste the relevant parts below.  It is meant to operate in the following manner:

1.  An external operation causes up to a few hundred documents at a time to be imported into a watched folder.
2.  Upon entering the watched folder the rule is invoked on each document, using the document's filename to infer properties (which are obtained from the site's datalist), and then uses those properties to file the document where it belongs.

My problem is this:  one of the documents doesn't get filed.  There's nothing special about it, nothing different from the other documents as far as I can tell, but for some reason it doesn't obtain its properties, and therefore doesn't get filed.  The crazy thing is that it will act in the desired manner if I manually run the scripts for the watched folder afterward, but it won't get acted upon at import time if I don't.

The script is configured to run *not* in the background.

Any thoughts?

Steve

var pathToDataList="Sites/reports/dataLists/";
var dataListName="Reports Recipients";
var docAspect="ra:reportProperties";

var reportLists = companyhome.childByNamePath(pathToDataList).getChildren();
var goodDoc=true;
var reportList;
var siteShortName="";

for each (child in reportLists)
{
   if (child.properties["cm:title"]==dataListName)
   {
      reportList=child;
      goodDoc=true;
      break;
   }
   else
   {
      goodDoc=false;
   }
}

if (!document.hasAspect(docAspect))
{
   document.addAspect(docAspect);
   document.save();
}
var filenameArray=new Array();
var reportName=document.properties.name;
var today=new Date();
var thisYear=today.getFullYear();
var thisDate=today.getFullYear()+"-"+(today.getMonth()+1)+"-"+today.getDate();
var reportNameArray=reportName.split(".");
var reportNameWithoutExtension=reportNameArray[0];
filenameArray=reportNameWithoutExtension.split("-");
var uniqueIdentifier=(filenameArray[0]).replace(/^\s+|\s+$/g, '');
document.properties.description=uniqueIdentifier;
      document.save();
var firstBracketPosition=reportName.indexOf('[');
var lastBracketPosition=reportName.indexOf(']');
var suffix="";
if ((lastBracketPosition-firstBracketPosition)>1)
{
   suffix=reportName.substring(firstBracketPosition,lastBracketPosition+1);
}
      document.properties.description+="mingood";
      document.save();
var fallback=false;
var fallbackOverride=false;
var fallbackDepartment="";
var fallbackReportName="";
if (goodDoc)
{
   document.properties.description+="goodDoc";
   document.save();
   var items=reportList.getChildren();
   for each (child in items)
   {
      if (uniqueIdentifier==child.properties["reportdl:uniqueIdentifier"])
      {
         if (child.properties["reportdl:suffix"].length>0 && suffix.indexOf(child.properties["reportdl:suffix"])>0)
         {
            document.properties.description+="suffixGood";
            document.save();
            fallbackOverride=true;
            document.properties["ra:uniqueIdentifier"]=(uniqueIdentifier.replace("$","_")).replace("#","_");
            document.properties["ra:reportName"]=child.properties["reportdl:reportName"];
            document.properties["ra:date"]=today;
            document.properties["ra:department"]=child.properties["reportdl:department"];
            document.properties["ra:notInWorkflow"]=true;
            if ((child.properties["reportdl:suffix"]!=null))
            {
               document.properties["ra:suffix"]=child.properties["reportdl:suffix"];
            }
            document.save();

            var sortedFoldersHome=companyhome.childByNamePath("Sites/reports/documentLibrary/Sorted-Folders");
            var department=sortedFoldersHome.childByNamePath(document.properties["ra:department"]);
            if (department==null)
            {
               department=sortedFoldersHome.createFolder(document.properties["ra:department"]);
            }
            var years = companyhome.childByNamePath("Sites/reports/documentLibrary/Sorted-Folders/"+document.properties["ra:department"]).getChildren();
            var destinationParent;
            var parent=companyhome.childByNamePath("Sites/reports/documentLibrary/Sorted-Folders/"+document.properties["ra:department"]);
            var goodDoc2=false;
         
            for each (child in years)
            {
               var childYear = child.properties.name;
               if (childYear==thisYear)
               {
                  destinationParent=child;
                  goodDoc2=true;
                  break;
               }
            }
            if (goodDoc2)   //if the Sorted-Folders/YYYY path is found, move the document to the correct sub-directory
            {
               var goodDoc3=true;
               var dates=destinationParent.getChildren();
               for each (child in dates)
               {
                  var childDate=child.properties.name;
                  if (childDate==thisDate)
                  {
                     document.move(child);
                     goodDoc3=false;
                     break;
                  }
               }
               if (goodDoc3)
               {
                  var dateFolder=destinationParent.createFolder(thisDate);
                  document.move(dateFolder);
               }
            }
            else
            {
               var yearFolder = parent.createFolder(thisYear);
               var dateFolder = yearFolder.createFolder(thisDate);
               document.move(dateFolder);
            }
            break;
         }
         else if (child.properties["reportdl:suffix"]=="Nil")
         {
            fallback=true;
            fallbackDepartment=child.properties["reportdl:department"];
            fallbackReportName=child.properties["reportdl:reportName"];
         }
         
      }
      else
      {
         document.properties["ra:uniqueIdentifier"]=(uniqueIdentifier.replace("$","_")).replace("#","_");
         document.properties["ra:reportName"]=(uniqueIdentifier.replace("$","_")).replace("#","_");
         document.properties["ra:date"]=today;
         document.properties["ra:department"]="Unknown";
         document.properties["ra:notInWorkflow"]=true;
         document.save();
      }
   }
   
   if (fallback && !fallbackOverride)
   {
      document.properties.description="fallbackGood "+fallbackReportName+fallbackDepartment;
      document.properties["ra:uniqueIdentifier"]=(uniqueIdentifier.replace("$","_")).replace("#","_");
      document.properties["ra:reportName"]=fallbackReportName;
      document.properties["ra:date"]=today;
      document.properties["ra:department"]=fallbackDepartment;
      document.properties["ra:notInWorkflow"]=true;
      document.properties["ra:suffix"]="Nil";
      document.save();
      var sortedFoldersHome=companyhome.childByNamePath("Sites/reports/documentLibrary/Sorted-Folders");
      var department=sortedFoldersHome.childByNamePath(document.properties["ra:department"]);
      if (department==null)
      {
         department=sortedFoldersHome.createFolder(document.properties["ra:department"]);
      }
      var years = companyhome.childByNamePath("Sites/reports/documentLibrary/Sorted-Folders/"+document.properties["ra:department"]).getChildren();
      var destinationParent;
      var parent=companyhome.childByNamePath("Sites/reports/documentLibrary/Sorted-Folders/"+document.properties["ra:department"]);
      var goodDoc2=false;
      for each (child in years)
      {
         var childYear = child.properties.name;
         if (childYear==thisYear)
         {
            destinationParent=child;
            goodDoc2=true;
            break;
         }
      }
      if (goodDoc2)   //if the Sorted-Folders/YYYY path is found, move the document to the correct sub-directory
      {
         var goodDoc3=true;
         var dates=destinationParent.getChildren();
         for each (child in dates)
         {
            var childDate=child.properties.name;
            if (childDate==thisDate)
            {
               document.move(child);
               goodDoc3=false;
               break;
            }
         }
         if (goodDoc3)
         {
            var dateFolder=destinationParent.createFolder(thisDate);
            document.move(dateFolder);
         }
      }
      else
      {
         var yearFolder = parent.createFolder(thisYear);
         var dateFolder = yearFolder.createFolder(thisDate);
         document.move(dateFolder);
      }
   }   
}
1 REPLY 1

srowsell
Champ in-the-making
Champ in-the-making
I have gotten around this problem, but it's kind of a silly fix.

Right now the way this works is an external application is causing files to get imported into Alfresco.  This application performs a number of checks once this is done, and then executes a web script which kicks off some workflows.  In the process of running these checks I now have it calling another web script which will add a character to the description of any files still in the watched folder, causing the script (which is not being executed the first time around) to run again, and so the "stranded" node will get treated after all.

This is, as I said, a very silly solution, and I'd still be glad for some insight into what (if anything) I could do to not have to do this in the first place.