cancel
Showing results for 
Search instead for 
Did you mean: 

Rule to execute RM action 'File to'

darkredd
Star Contributor
Star Contributor
Hi all,

I am looking for a way to execute an action from a rule on RM "Unfiled record" folder. Here is the scenario; I would like for every node that comes into this folder to be moved to their respective folder in the File Plan based on their origin location, which is a metadata on the document. How would I go about achieving this. I have a general idea that I have to write a script that will call the "File to" action, I looked at the "actions.js" & "copy-move-link-file-to.js" but was unable to make sense of how to call those actions in the script.

Help much appreciated.
4 REPLIES 4

darkredd
Star Contributor
Star Contributor
Hello all,

In attempts to solve my problem I tried using the remote service API, which is by default not available to the JS processor. I added it as suggested here https://forums.alfresco.com/forum/developer-discussions/web-scripts/using-remote-endpoint-repository... and it works only in the JS console but when I implement the same script as a rule I get "remote" is not defined.
Is there anywhere else that I need to change to enable remote service to be available system wide?

Below is the script I am runing:
<javascript>
var node = document;
var connector = remote.connect("alfresco");
var ticket = session.getTicket();

function main()
{
   FileRecord(node);
}

function FileRecord(recNode)
{
    var apiURI = '/slingshot/doclib/action/move-to/site/rm/documentLibrary/' + sanitizePath() + '?alf_ticket=' + ticket;
    apiURI = replaceAll(apiURI, " ", "%20");

    var jsonString = '{"nodeRefs":["' + recNode.nodeRef + '"]}';
    var result = connector.post(apiURI, jsonString, "application/json");
}

function sanitizePath()
{
   var fullpath = document.properties["psa:filePath"].split("/");
   var filePlan = fullpath[0];
   var storeLocation = filePlan;
   
   for(var x = 1; x < fullpath.length; x++)
   {
      if(fullpath[x] != "documentLibrary")
      {
         storeLocation += "/" + fullpath[x];
      }
   }
   storeLocation = storeLocation.substr(0, (storeLocation.length -1));
   
   return storeLocation;
}
</javascript>

Here is the error message on the log:

Caused by: org.alfresco.error.AlfrescoRuntimeException: 03240032 ReferenceError: "remote" is not defined. (workspace://SpacesStore/f2ca366f-6c55-4ec8-adcd-1a41525edcc3#2)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:526)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:250)
   … 117 more
Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "remote" is not defined. (workspace://SpacesStore/f2ca366f-6c55-4ec8-adcd-1a41525edcc3#2)
   at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
   at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
   at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3413)
   at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1612)
   at org.mozilla.javascript.gen.c18._c0(workspace://SpacesStore/f2ca366f-6c55-4ec8-adcd-1a41525edcc3:2)
   at org.mozilla.javascript.gen.c18.call(workspace://SpacesStore/f2ca366f-6c55-4ec8-adcd-1a41525edcc3)
   at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
   at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
   at org.mozilla.javascript.gen.c18.call(workspace://SpacesStore/f2ca366f-6c55-4ec8-adcd-1a41525edcc3)
   at org.mozilla.javascript.gen.c18.exec(workspace://SpacesStore/f2ca366f-6c55-4ec8-adcd-1a41525edcc3)
   at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:502)




Thank you for the help.

afaust
Legendary Innovator
Legendary Innovator
Hello,

the reason why "remote" is not available in a rule is that the / my suggestion you used refers to "adding the remote object to the web scripts container". That means that object is specifically ONLY availabble in web script JavaScript. The JavaScript Console uses a web script to execute your script, but rules are a different matter.
In my post I also referred to practical problems adding "remote" to other contexts, like scheduled actions (and rules).
<blockquote>I don't know about scheduled actions though, as the ScriptRemote class is not a valid processor extension and can not be added as a root object to the internal Rhino script processor.</blockquote>

The only chance to add it to all JavaScript contexts running on the Repository is by building you own Java-based script processor extension that wraps ScriptRemote and delegates all invocations to the wrapped object.

Regards
Axel

afaust
Legendary Innovator
Legendary Innovator
And on the original note: It is not necessary and architeturally a dirty hack to call a Repository action web script from within a Repository scheduled action or rule. Not only do you break up your transaction and can end up in inconsistent data states (business logic wise), you also introduce much more overhead than would be appropriate, e.g. you block at least one more thread that could otherwise server a request from a user.

The way to go about this is to use the <a href="http://docs.alfresco.com/5.0/references/API-JS-Scripting-API.html">JavaScript API</a> in your rule JS directly to move / copy / modify the nodes that should be affected by your rule. You can take inspiration from existing web script JavaScript sources, but be aware that those may change between Alfresco versions (as internal implementation detail) and break your rule. So it's best to isolate yourself from that by not calling out to those…

Regards
Axel

darkredd
Star Contributor
Star Contributor
Hello Alex,

Thank you for the insight. I will certainly try using the API as suggested.