cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript API: child.delete() does not work

sbroussi
Champ in-the-making
Champ in-the-making
Hi,

I'm trying to create a "Inbound" rule to:
- detect all incoming ACP files
- import the ACP in one folder (built-in action)
- remove the ACP file (run a custom script)

I have an error when I invoke "child.delete()"

ERROR [repo.action.ActionServiceImpl] An error was encountered whilst executing the action 'composite-action'.
org.alfresco.service.cmr.repository.ScriptException: Failed to execute script 'workspace://SpacesStore/7b4086cf-efbb-11da-a650-b99
5eb94ad41': missing name after . operator (AlfrescoScript#14)
        at org.alfresco.repo.jscript.RhinoScriptService.executeScript(RhinoScriptService.java:145)

I've tried:

a) to delete the current node (document.delete())"
    (I can imagine that the Node that trigger the rule should
    not be so easily removed during the rule process…)

b) to rename the file "*.toBeRemoved" as for the next time
the script is run, the script removes all old files.


// DELETE DOES NOT WORK FOR THE CURRENT NODE
// document.delete();

// remove all previous document
var childList = space.children;
for (var i=0; i<childList.length; i++)
{
   var child = childList[i];
   if (child.name.indexOf(".ToBeRemoved") > 0)
   {
      child.delete();
   }
}

document.name = document.name + ".ToBeRemoved";
document.save();

=> is it a Bug ? I've read http://wiki.alfresco.com/wiki/JavaScript_API
11 REPLIES 11

kevinr
Star Contributor
Star Contributor
You have found a bug in the JavaScript engine, I've investigated it and found some (but not much!) information that suggests the fact that delete is a reserved word in JavaScript is causing intermitent problems with the JavaScript->Java method resolution (see this blog and search for "Java overloaded method resolution in JavaScript" section if you are interested).

It looks like sometimes it causes a problem and other times it does not - so I've changed the API call from node.delete to node.remove see http://wiki.alfresco.com/wiki/JavaScript_API#Modifying_and_Creating_Nodes and it will be available in the next 1.3 drop.

Thanks for spotting this!

Kevin

sburky
Champ in-the-making
Champ in-the-making
hum hum.
Do y have same pb with workflow engine ?
I just want to delete all process instances of a workflow definition.
I can list them


   var def = workflow.getDefinition(wf);
   var instances = def.getActiveInstances();
   var instance,i;
   var retour="";
  
   for (i in instances)
   {
      instance = instances[i];     
      retour = retour + "instance : " + instance.id + " démarré le " + instance.getStartDate() + " : " + instance.getDescription();     
      if (del == true) {
//          instance.delete();
         retour = retour + " <b>supprimée</b>";
      }
      retour = retour + "<br>";
       
   }
   if ( retour == "")
      retour = "pas d'instances démarrées pour ce workflow " + wf;
   return (retour);

but if i un-comment the line instance.delete()

i have an incomprehensible error :

Failed to execute script 'workspace://SpacesStore/dd674fbd-9abd-44fb-a189-403ae4c4737d': il manque un nom après un opérateur '.' (workspace://SpacesStore/dd674fbd-9abd-44fb-a189-403ae4c4737d#16)
caused by: org.mozilla.javascript.EvaluatorException: il manque un nom après un opérateur '.' (workspace://SpacesStore/dd674fbd-9abd-44fb-a189-403ae4c4737d#16)

kevinr
Star Contributor
Star Contributor
I think you want to use child.remove() as "delete" is a reserved method/word in javascript.

Kev

sburky
Champ in-the-making
Champ in-the-making
yes but in my case it's not the child i want to remove …
I want to kill workflow instance, and the remove method doesn't exist on JscriptWorkflowInstance object

I can use cancel but it's not the same fonctionnality

kevinr
Star Contributor
Star Contributor
OK I see what you mean - JscriptWorkflowInstance public void delete();
That is a bug: No JavaScript host object can have a delete() method as it is reserved. That method will need renaming (to remove()! or similar which is why it is remove() on scriptnode). I will raise an issue and get it fixed.

Cheers,

Kev

kevinr
Star Contributor
Star Contributor

sburky
Champ in-the-making
Champ in-the-making
yes  Smiley Very Happy

jpotts
World-Class Innovator
World-Class Innovator
I think you can work around the reserved word problem by using the bracket syntax, like this:

instance['delete']();

Jeff

sburky
Champ in-the-making
Champ in-the-making
hum … it works  Smiley Surprised
I don't know Javascript permits to run a method of an object by calling it on an array …..
thanks Jeff