start workflow using alfresco java script api or through web script

蓝咒 提交于 2020-01-14 02:35:20

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/11688688/start-workflow-using-alfresco-java-script-api-or-through-web-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!