cancel
Showing results for 
Search instead for 
Did you mean: 

Iterate assocs in javascript to remove them

loftux
Star Contributor
Star Contributor
How do I iterate an associations of a specified type in javascript in order to remove them?
I've tried this (and some variants), but it does not work.
for (i in fileNode.assocs["xel:reviewedby"]){
   fileNode.removeAssociation(fileNode.assocs["xel:reviewedby"][i], "xel:reviewedby");
}

So what is the correct way?
4 REPLIES 4

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

In your for…in loop the variable "i" is set to the value of the object within the array being iterated over, not the current index in the array.

Try this ….


for (var targetNode in fileNode.assocs["xel:reviewedby"])
{
   fileNode.removeAssociation(targetNode, "xel:reviewedby");
}

loftux
Star Contributor
Star Contributor
Thanks for the reply.
It doesn't work, have created this test code
var namesuffix = userhome.children.length; //to make file name unique

var sourceNode = userhome.createNode("testassocsfile"+namesuffix +".txt", "cm:content");
sourceNode.content = "the original text";
sourceNode.addAspect("cm:attachable");
logger.log("LX Created source node");

var destNode = userhome.createNode("attached1-"+namesuffix +".txt", "cm:content");
destNode.content = "Text in attached document";
logger.log("LX Created dest node 1");
sourceNode.createAssociation(destNode, "cm:attachments");
logger.log("LX Created assoc destNode to sourceNode");

var destNode2 = userhome.createNode("attached2-"+namesuffix +".txt", "cm:content");
destNode2.content = "Text in attached document2";
logger.log("LX Created dest node 2");
sourceNode.createAssociation(destNode2, "cm:attachments");
logger.log("LX Created assoc destNode2 to sourceNode");

sourceNode.save();

for (var targetNode in sourceNode.assocs["cm:attachments"])
{
   logger.log("LX Removing " + targetNode) //targetNode returns integer here
   logger.log("LX Removing name " + targetNode.name) //returns undefined
   sourceNode.removeAssociation(targetNode, "cm:attachments");
}

When this code is run, it throws error (partial)
Caused by: org.alfresco.error.AlfrescoRuntimeException: 08210080 Can't find method org.alfresco.repo.jscript.ScriptNode.removeAssociation(string,string). (file:/opt/alfresco/xelerated/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts/test/lxtest.get.js#30)
As you can see in my test code, the logging of targetNode in the iteration returns an integer, starting at 0 (and why I tried to use it the way I did, I think…).

I tried to change to sourceNode.removeAssociation(targetNode.child, "cm:attachments");
but that didn't work either.

Running this code on 3.3g

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

Yes, my bad.  The form you where using does indeed return the index of the array being iterated over …


// example iterating array indexes using 'for .. in'
var out1 = "";
for (i in userhome.children)
{
   out1 += userhome.children[i].name + "<br>";
}

// example iterating array values using 'for each .. in'
var out2 = "";
for each (n in userhome.children)
{
   out2 += n.name + "<br>";
}

I need to go back to JavaScript school!

Cheers,
Roy

loftux
Star Contributor
Star Contributor
OK,
this is how you do it for my example code
for each (targetNode in sourceNode.assocs["cm:attachments"])
{
   sourceNode.removeAssociation(targetNode, "cm:attachments");
}

Your javascript skills clearly exceed mine, and I'll give you your second rating point Smiley Wink