cancel
Showing results for 
Search instead for 
Did you mean: 

Pass params to a JavaScript

chicco0386
Champ on-the-rise
Champ on-the-rise
Hi, I've this function that execute a script:
private String startWorkflow(Reference refUponWF) throws RepositoryFault, NumberFormatException, RemoteException, WebServiceException, Throwable {

      log.info("startWorkflow, entrato");
      String toReturn = "";

      try {
         String nomeScriptWF = "prova.js";
         // return the reference to the script
         List<Reference> scriptSearchResults = RepositoryUtils.searchLucene("/Data Dictionaty", nomeScriptWF);
         if (scriptSearchResults.isEmpty()) {
            throw new Exception("Non posso eseguiro lo script perchè NON ho trovato nessuno script con nome [" + nomeScriptWF + "]");
         }
         Reference script = scriptSearchResults.get(0);
         toReturn = ActionUtils.executeScript(refUponWF, script);
         log.info("startWorkflow, risultato " + toReturn);
      } catch (Throwable t) {
         log.error("startWorkflow, errore generico.", t);
         throw new Throwable("Errore generico. Class: " + t.getClass().getName() + " Message: " + t.getMessage());
      }

      log.info("startWorkflow, uscito");
      return toReturn;
   }

It works fine, but how can I do for pass some params to the scripts?
After the script will go to read the passed params like this:
var param = args["paramName"];

CAN YOU HELP ME?
IS IT POSSIBLE?
THANK YOU
5 REPLIES 5

jcustovic
Champ in-the-making
Champ in-the-making
When you create object org.alfresco.webservice.action.Action you can call method setParameters(namedValueArray) and pass in NamedValue[] parameters.

chicco0386
Champ on-the-rise
Champ on-the-rise
Hi, thank you for the response, I've try to do this:
Map<String, String> parameters = new HashMap<String, String>(1);
parameters.put("script-ref", Utils.getNodeRef(script));
parameters.put("prova", "Passato da WS");
ActionUtils.executeAction(refUponWF, "script", parameters);

and the script:
function main() {
   try {
      var nodeRef = args.prova;
      logger.log("Parametro —- " + nodeRef);
      var nodeRef1 = args["prova"];
      logger.log("Parametro —- " + nodeRef1);

      return "OK";

   } catch (e) {
      logger.log("Error durante l'esecuzione dello script " + e);
      return e;
   }
}
main();

but I've a this error:
Error durante l'esecuzione dello script ReferenceError: "args" is not defined.

Can you help me?
Thank you

jcustovic
Champ in-the-making
Champ in-the-making
Try accessing parameter by the name you specified (without args).
Example:
logger.log("Parametro —- " + prova);

chicco0386
Champ on-the-rise
Champ on-the-rise
Nothing to do, others suggestions.

Thank you

richhart
Champ in-the-making
Champ in-the-making
If trying to access a param in a script that has been executed the following should work:

var scriptAction = actions.create("script");
   var jsNode = companyhome.childByNamePath("Data Dictionary/Scripts/testAction.js");
   scriptAction.parameters["script-ref"] = jsNode;
   scriptAction.parameters["testarg"] = "hello";
   scriptAction.execute(jsNode);

Then in testAction.js you can do the following:

var result = action.parameters["testarg"];
logger.log(result);

This should work.

HTH

Rich Hart
Alfresco Certified Engineer
Ixxus.com