cancel
Showing results for 
Search instead for 
Did you mean: 

Document Move triggered by Rule.

richard_steele
Champ in-the-making
Champ in-the-making
All, I have been using the scripts provided by alfresco to create a set of rules / scripts to archive scanned documents into month folders once a particular criteria has been met.

I have managed to cobble a script to 'copy' the files into the archive - creating it if neccessary. But have not yet been able to sort the syntax to 'move' the document.

The copy code I use is:


// find the backup folder - create if not already exists
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];


var CurrentDayFolder = "Store -" + monthNames[month]  + " " + year;

var backupFolder = space.childByNamePath(CurrentDayFolder);

if (backupFolder == null && space.hasPermission("CreateChildren"))
{
   // create the folder for the first time
   backupFolder = space.createFolder(CurrentDayFolder);
}
if (backupFolder != null && backupFolder.hasPermission("CreateChildren"))
{
   // copy the doc into the backup folder
   var copy = document.copy(backupFolder);
   if (copy != null)
   {
      // change the name so we know it's a backup
      var backupName = "Backup of " + copy.name;
      copy.name = backupName;
      copy.save();
   }
  
   // record the time of the backup to a log file
   var logFile = backupFolder.childByNamePath("audit.txt");
   if (logFile == null)
   {
      logFile = backupFolder.createFile("backuplog.txt");
   }
   if (logFile != null)
   {
      logFile.content += "File: " + backupName + CurrentDayFolder +
                         "\tDate: " + new Date().toGMTString() +
                         "\tSize: " + copy.size + "\r\n";
   }
}


and tried the foloowing syntax to build the move


var dest = companyhome.childByNamePath("Sites/psn/documentLibrary/test1");
document.move(dest);


can any one suggest a way to join these together as It appears I am not passing the node reference correct.

I am a code newby.

TIA

Richard
8 REPLIES 8

muralidharand
Star Contributor
Star Contributor
Are you getting any errors or the rule is not triggered?

richard_steele
Champ in-the-making
Champ in-the-making
HI - I have been testing with a slightly modified move script

var nodeRef = "a38ba6c0-211f-4d8b-9d3a-48fdb25faa53";
var foundnode = companyhome.nodeByReference[nodeRef];

if(document.properties.title=="rick")
{
    document.move(foundnode);
}else{
    document.move(userhome);
}

This is triggered by a folder rule on updated content.  when the title is not rick the script does move the file into the userhome. but in the other case the error below appears.


and the error I get is :

090862802 Failed to execute script 'workspace://SpacesStore/0484dc73-b924-41bf-b4ff-2ac837af37c3': 090862801 TypeError: Cannot read property "a38ba6c0-211f-4d8b-9d3a-48fdb25faa53" from undefined (workspace://SpacesStore/0484dc73-b924-41bf-b4ff-2ac837af37c3#3)


Regards

Richard

muralidharand
Star Contributor
Star Contributor
Does the user has permission to access the "workspace://SpacesStore/0484dc73-b924-41bf-b4ff-2ac837af37c3" node?
Can you please provide the complete script and how to configure and trigger?
Let me try in local machine and will help you to fix.

There is a site called test, with a receive folder and an archive folder within documentlibrary.

The is a rule which states on update then call the javascript below.

The value for nodeRef below is for the folder within the test site called archive.

move.js

var nodeRef = "a38ba6c0-211f-4d8b-9d3a-48fdb25faa53";
var foundnode = companyhome.nodeByReference[nodeRef];

if(document.properties.title=="rick")
{
    document.move(foundnode);
}else{
    document.move(userhome);
}



All the code I popped in the first forum post works in respect of creating a folder on demand with the current month and 'copying' the document into that folder.

I have produced the above code to establish how the move function works.  And if the title is modified to anything other than rick then the document is moved to the userhome folder.  I want it to move to a name folder (byNodeRef if neccesary)

Rick

richard_steele
Champ in-the-making
Champ in-the-making
Hi depending on how it is triggered - the rule fires normally when I have a conditional move :

var nodeRef = "a38ba6c0-211f-4d8b-9d3a-48fdb25faa53";
var foundnode = companyhome.nodeByReference[nodeRef];

if(document.properties.title=="rick")
{
    document.move(foundnode);
}else{
    document.move(userhome);
}

for example the file gets moved to the userhome - but when trying to target the dynamically addressed destination the rule fails to complete.

kaynezhang
World-Class Innovator
World-Class Innovator

As far as I know java  script object companyhome dose not have method nodeByReference
Why not just use

var folder = companyhome .childByNamePath("****");

Thanks for your help, I have tried the javascript extract your provided, and the folder reference still failed to be accessible for the move.

I will do some error handling to see where the reference is going wrong, but my knowledge of javascript is not great.

I know the document.move obviously works as the file was moved to my userhome, its just finding a way to reference a known folder that appears to be the problem.

I have a folder called "MoveTo" inside the site where the script is called from (as I understand the .childByNamePath is relative to the current location.  the syntax I used was:

var folder = document.childByNamePath("MoveTo");

Have I misinterpreted the syntax requirement? Should it be more like

var folder = document.childByNamePath("Sites/Test/DocumentLibrary/MoveTo");

and can the folder object be passed to to the document.move(x) function in the same way as the userhoem object?

TIA

Kind Regards

Richard

kaynezhang
World-Class Innovator
World-Class Innovator
childByNamePath method's parameter is the relative path to the current node,so you should not use

document.childByNamePath("Sites/Test/DocumentLibrary/MoveTo");

you shoud use

var folder = = companyhome.childByNamePath("Sites/Test/DocumentLibrary/MoveTo");