Folder Rule javascript - Create folders and move file

Showing results for 
Search instead for 
Did you mean: 
Member II

Folder Rule javascript - Create folders and move file

I have created the below folder rule javascript which is intended to the the following:

Checks to see if a folder exists based on text in the document description, if the folder exists, move the file to that found folder. If not, create the subsequent folder stucture Year > Month Name > Date > CIF, then move the file to the resulting CIF folder. I can get certain segments of code to work in isolation within the javascript console, but cannot get it to fire when plugged into the server rule. Would appreciate any advice, code below:

var d = new Date();
var n = d.getFullYear();
var m = d.getMonth();
var o = d.getDate();
var Descr =["description"];
var CIF = Descr.substring(3, 8);
var yearFolder = space.childByNamePath(n);
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var thisMonth = months[m];
var monthFolder = yearFolder.childByNamePath(thisMonth);
var dateFolder = monthFolder.childByNamePath(o);
var cifFolder = dateFolder.childByNamePath(CIF);
var foundFolder = search.luceneSearch("TYPE:\"acme:folder\"" + CIF);

function main()
	if (foundFolder == null) {
		if (yearFolder == null) {
		//create year folder
		yearFolder = space.childByNamePath.createFolder(n);
		if (monthFolder == null) {
		//create month folder by month-name
		monthFolder = yearFolder.childByNamePath.createFolder(thisMonth);
		if (dateFolder == null) {
		//create month date folder
		dateFolder = monthFolder.childByNamePath.createFolder(o);
		if (cifFolder == null) {
		//create CIF folder based on file CIF12345 description
		cifFolder = dateFolder.childByNamePath.createFolder(CIF);
		//move file to CIF folder
		var move1 = document.move(cifFolder);
	} else {
		var move2 = document.move(foundFolder);

3 Replies
Established Member II

Re: Folder Rule javascript - Create folders and move file

do you get any errors in the logs? how have you configured the rule fom share?

Member II

Re: Folder Rule javascript - Create folders and move file

Sorry I am very new to Alfresco, I'm not sure how to check the logs. The rule is configured from Share as a folder rule that fires when a document in the folder is modified. If the description contains CIFXXXXX it will fire the script above.

Senior Member II

Re: Folder Rule javascript - Create folders and move file

maybe you should add some code for easier debugging:

function logMe(message) {
    var d = new Date();
    var ds = utils.toISO8601(d) + "";
    logger.system.out(ds.replace("T", " ").trim() + " " + + ": " + message);

add the logMe function to your code and call it to test your expected variable values, look if your script is triggered with
logMe("script is triggered"); on startup

The output is visible i.e. on in the catalina.out