2

I want to start a workflow programatically. So written a web script.

Execute Script :

function startWorkflow()
{
   var workflow = actions.create("start-workflow");
   workflow.parameters.workflowName = "activiti$alfGroupReview";
   workflow.parameters["bpm:workflowDescription"] = "Please review ";
   workflow.parameters["bpm:groupAssignee"] = people.getGroup( "GROUP_site_collaborators");;
   var futureDate = new Date();
   futureDate.setDate(futureDate.getDate() + 7);
   workflow.parameters["bpm:workflowDueDate"] = futureDate; 
   workflow.execute(document);
   return ;
}

For the above script, I am getting error "document is not defined". I am referring https://forums.alfresco.com/en/viewtopic.php?f=34&t=42677 and http://livinginjava.blogspot.in/2008/10/starting-alfresco-workflow-using.html links.

So I update my script to :

function startWorkflow()
{
var nodeRef = "workspace://SpacesStore/25285e6c-2995-49fe-aa50-1270cefc806a";
var docNode = search.findNode(nodeRef);
   var workflow = actions.create("start-workflow");
   workflow.parameters.workflowName = "activiti$alfGroupReview";
   workflow.parameters["bpm:workflowDescription"] = "Please review ";
   workflow.parameters["bpm:groupAssignee"] = people.getGroup( "GROUP_aloha_collaborators");;
   var futureDate = new Date();
   futureDate.setDate(futureDate.getDate() + 7);
   workflow.parameters["bpm:workflowDueDate"] = futureDate; 
   workflow.execute(docNode);
   return ;
}

Here, nodeRef : is ref of a document from document library.

Now new error is :

500 Description:    An error inside the HTTP server which prevented it from fulfilling the request.

Message:    06270056 Wrapped Exception (with status template): 06270273 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/justransform/startWF.get.js': null

Exception:  org.alfresco.scripts.ScriptException - 06270273 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/justransform/startWF.get.js': null

    org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:195)

thanks in advance.

4
  • Are you sure you can't get the node to be passed into your script as an argument? And if not, are you sure that's the right way to look up the node? Commented Jul 27, 2012 at 14:35
  • I can pass the node as an argument. But as I am testing, I used hard coded node ref. I am very new to workflows and even don't know the need of nodeRef to start workflow. thanks for reply Commented Jul 27, 2012 at 15:07
  • If you want to do a review workflow, then you need the noderef to specify what people should be reviewing! Commented Jul 27, 2012 at 15:29
  • ok got it. SO if I want to start Adhoc workflow. What changes are needed in above script ?? Or Do I need some other way ?? Commented Jul 27, 2012 at 15:54

2 Answers 2

7

Using Alfresco Workflow API. Note: wfDocs holds the array of doc nodes:

// 2 days from now
var dueDate2d = new Date((new Date()).getTime() + 2*(24*60*60*1000));

// Start workflow
var wfdef = workflow.getDefinitionByName("activiti$alfGroupReview");
if (wfdef) {
    var wfparams = new Array();
    wfparams["bpm:workflowDescription"] = "Please review";
    wfparams["bpm:groupAssignee"] = people.getGroup( "GROUP_site_collaborators");
    wfparams['bpm:workflowDueDate'] = dueDate2d;
    wfparams['bpm:workflowPriority'] = 1;
    wfparams['wf:notifyMe'] = true;

    var wfpackage = workflow.createPackage();
    for each (var n in wfDocs)
        wfpackage.addNode(n);  
    var wfpath = wfdef.startWorkflow(wfpackage, wfparams);
    var tasks = wfpath.getTasks();
    for each (task in tasks)
        task.endTask(null);
}
Sign up to request clarification or add additional context in comments.

Comments

5

This code runs fine if:

  • docNode is not null. You should add a check for this.
  • Your group exists. Probably worth adding a check for this.
  • The workflow exists with the ID specified. Use the workflow console to confirm this. For example, the ID your provided is not an out-of-the-box workflow. If it is custom, maybe you haven't deployed the workflow successfully or you have the ID incorrect.

Also, do not use a variable called "workflow". Alfresco already defines a root-scoped object called "workflow". Speaking of that, feel free to use the workflow JavaScript API to invoke your workflow instead of an action. Either should work, though.

I ran your code successfully using the JavaScript console and a workflow id of "activiti$activitiParallelGroupReview" (and after changing your workflow variable to workflowAct).

3 Comments

Jeff, thanks for reply. Now my script run successfully. Can we run the script without passing docNode. I am trying to start custom workflow, which does not need docNode reference. I tried calling execute() method without argument, but got an exception every time.
It is definitely possible to run a workflow that does not have a document. The action you are running wants a document context. If you don't want to do that, don't start an action. Use the workflow API instead.
Jeff thanks. Now how can I add multiple documents to workflow ? I don't see relevant method in workflow api. Also, I tried by calling execute method with array of docNodes. But no luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.