mahesh
mahesh

Reputation: 597

start workflow using alfresco java script api or through web script

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.

Upvotes: 2

Views: 7449

Answers (2)

Luis Sánchez
Luis Sánchez

Reputation: 121

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);
}

Upvotes: 7

Jeff Potts
Jeff Potts

Reputation: 10538

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).

Upvotes: 5

Related Questions