cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript Rule Delete Node

strugglingdev
Champ in-the-making
Champ in-the-making

Issue with javscript that removes another node when a node is deleted.

function main() {

      var parent = space.parent;
      var parentOfParent = parent.parent;

      var staffnetFolder = companyhome.childByNamePath("Shared/Staffnet/" + parentOfParent.name + "/" + parent.name);

      var pdf = staffnetFolder.childByNamePath(document.name + ".pdf");

      if (pdf !== null) { pdf.remove(); }
}

main();

Basically, when an item is created we have a rule to create a pdf of it in another space. When the item is deleted, I want to delete the pdf as well. It's finding the PDF successfully, but when I run the remove() on the pdf I get this error - 

"message" : "JavaException: org.springframework.dao.ConcurrencyFailureException: Deleted 0 but expected 1"

6 REPLIES 6

douglascrp
World-Class Innovator
World-Class Innovator

Are you sure you are getting the right "other" document?

Try to print the noderef for both document (the one you are deleting) and pdf (the one you are trying to delete in response of the first one) objects.

When I output the two nodeRefs they are what they should be. When I try to use the code in another action (such as an update rule) it works and deletes the PDF. It just seems to be the delete action that is causing this problem.

I am not sure if this is also true for folder rules, as I have never tried to do what you are trying, but I know that when you are using behaviours, you have to use another approach, as described in this blog post Alfresco – Implementing delete behavior | Programming and So 

The use case is basically the same, but using Java code.

Thank you Douglas - sorry if I'm misunderstanding. So you're saying that I should look into developing behaviours instead of putting the action as a rule and use this transactional logic? Or am I able to implement transactional logic in rule scripts?

I believe that would be the right thing to do.

mehe
Elite Collaborator
Elite Collaborator

Maybe this is caused by „retriggering“ the delete rule with pdf.remove()?

But I can‘t see from your question if the staffnet folder is also in the range of your delete rule...

I'm not sure if you get what you want with using "space", maybe document.parent - but that depends were your rule is anchored and if it's trigger in sub-spaces (the whole tree)