Problem moving node (Error: Node has been pasted into its own tree.)

cancel
Showing results for 
Search instead for 
Did you mean: 
mperez
Active Member

Problem moving node (Error: Node has been pasted into its own tree.)

Jump to solution

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.

1 Solution

Accepted Solutions
mperez
Active Member

Re: Problem moving node (Error: Node has been pasted into its own tree.)

Jump 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.

View solution in original post

3 Replies
mperez
Active Member

Re: Problem moving node (Error: Node has been pasted into its own tree.)

Jump to solution

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.

mperez
Active Member

Re: Problem moving node (Error: Node has been pasted into its own tree.)

Jump to solution

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?

mperez
Active Member

Re: Problem moving node (Error: Node has been pasted into its own tree.)

Jump 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.