I've just written a script using the Javascript API so I can use it as a Rule to a Space. Whenever a document enters in a space, it basically checks document's name to get the parent directory it will be stored in (document's name contains parent's folder name), then creates future parent directory (if it doesn't exist), and finally moves the document into it.
I'm having troubles with this last step. Whenever I try to move the document into the recently created folder, I get the following error:
Node has been pasted into its own tree.
This is my code so far, which I think is pretty much auto-descriptive:
var fileName= document.properties.name;
var fields = fileName.split('.');var parentName= fields[0];
var newNode=space.childByNamePath(parentName);
if (newNode === null) { //create folder and move document into it
newNode=space.createFolder(parentName); //works
document.move(newNode); //I'm getting the error here
}else{ //folder already exists, just move document into it
document.move(newNode); //here too}
If I comment the document.move(newNode); lines everything else works fine. Parent folder is successfully created but obviously the document keeps stored at the root of the current space, which is not what I need. Indeed I need to move it into the actual folder.
Am I doing something wrong? Any help would be much appreciated. Thanks.
Solved! Go to Solution.
SOLUTION:
I posted the question in StackOverflow (sorry to say that) and after a few minutes a person came up with a really great solution.
In case you're facing the same issue all you've got to do is to execute the Rule in background (ensure "Run rule in background" is enabled). That will do the trick.
UPDATE: I've realized that the line:
document.move(newNode);
works well inside the if scope. The error is being reproduced inside else. It's probably a typing issue but I'm pretty lost with it and I don't know what am I missing.
UPDATE 2:
This is pretty weird. Depending on where I make the call it works or not.
The following code works and moves the document to the recently created folder:
var nodeNouProjecte = space.childByNamePath(nomProjecte);
if (nodeNouProjecte === null) {
nodeNouProjecte = space.createFolder(nomProjecte);
document.move(nodeNouProjecte);}
But the following doesn't:
var nodeNouProjecte = space.childByNamePath(nomProjecte);
if (nodeNouProjecte === null) {
nodeNouProjecte = space.createFolder(nomProjecte);
}
document.move(nodeNouProjecte);
Also if I make the call inside an else clause, it won't work either and the same error will be produced.
Any clue at all about what could be going on here?
SOLUTION:
I posted the question in StackOverflow (sorry to say that) and after a few minutes a person came up with a really great solution.
In case you're facing the same issue all you've got to do is to execute the Rule in background (ensure "Run rule in background" is enabled). That will do the trick.
Ask for and offer help to other Alfresco Content Services Users and members of the Alfresco team.
Related links:
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.