Saturday, October 04, 2008

Starting an alfresco workflow using Javascript API

The script to execute workflow is as below :

var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$sanz:testWorkflow";
workflow.parameters["bpm:workflowDescription"] = upload.name;
workflow.parameters["bpm:groupAssignee"] = people.getGroup("GROUP_IT Manager");
workflow.parameters["sanz:notifyMe"] = true;
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
workflow.parameters["bpm:workflowDueDate"] = futureDate;
workflow.execute(document);


A bit explanation :
  • "start-workflow" is the action name to create a workflow action (see Action API)
  • workflow name is prefixed by "jbpm$", the workflow name used is the name set in the process definition along with its namespace
  • other parameters as defined in the task(inside the start-state) 's type could be set here, e.g. the "sanz:notifyMe" property in the example above
  • pass a document (Content noderef) to workflow.execute. Remember if we start a workflow from the web-client interface, we always start from a content/document

Tips :
  • if you want to pass a group as the groupAssignee, we can use people.getGroup(groupName) API, but one thing to note, the group name to pass is : GROUP_group name. You should add "GROUP_" prefix to your group name.
  • to pass assignee, use people.getPerson(assignee name)
  • to pass assigness, wrap the assigness as array, using [], e.g. [people.getPerson("me"), people.getPerson("you")]

I guess that's all for now. Next target, synchronize user/group to ldap (or from ldap?)

15 comments:

Anonymous said...

hi Santoso,

Thanks for the informative tutorial, I was wondering, how can I "reference a content that is already uploaded in alfresco" if I know its noderef? I want to execute a workflow with a pre-defined content.

ex.

workflow.execute("content noderef")???


can i use noderef if yes, how? thanks

Santoso said...

Hi,

of course you can reference to existing content.

To get the noderef you could use scriptnode API (see http://wiki.alfresco.com/wiki/3.0_JavaScript_API#Root_Scope_Objects --> if you're using alfresco 3.0)

for example :
the document (test.txt) is located in docs under your company home.
var noderef = userhome.childByNamePath("/Company Home/docs/text.txt");

workflow.execute(noderef);


The above code should work for you. Please inform me whether it works for you or not :)

stockerMk said...

Hi Santoso,
i need for my clients workflow toolkit for alfresco that will be used without programing and deploying, is there anything like this?
Thank You
Miki

Santoso said...

Hi Miki,

AFAIK, I don't think there's a toolkit which could help you develop workflow easily.

At least you need to have your process definition, model and web client configuration.

The tool to prepare the process definition could be found here :
http://wiki.alfresco.com/wiki/WorkflowAdministration#Process_Designer_developed_Process_Archive

This tool could help you deploy the process definition.

For the model and web client configuration, I think you have to prepare it yourself using a text editor.

Anonymous said...

Hi Santoso

I have this requirement of using the document's custom properties in my workflow. I am, at this point, able to add a document belonging to a custom document type to the alfresco and also am able to launch a custom workflow (using a javascript and a rule defined on my space). Now, my requirement is to assign certain properties (like empId and empName) on the custom document type to the workflow parameters defined on my workflow. Is there a way to accomplish this?

Jean-Christophe Kermagoret said...

Hi,
BlueXML provides a Workflow modeler to design such workflows and generate everything Alfresco needs (model, process definition file, web client configuration, messages, ...)

No need to code anything,

HTH,

Unknown said...

hi,

Thanks for all information. I had a question, can I refer to different groupAssignee? Indeed, according to the space where the content is added, the groupAssignee will be different.
How can I do that?

Thanks.

Anonymous said...

Hi Santoso,
I was executing your script but I am getting error. "A system error happened during the operation: 07100013 Failed to execute script 'workspace://SpacesStore/891d8884-2fd7-4c62-8e1f-499815dbe33c': 07100012 Mandatory task properties have not been provided: {http://www.alfresco.org/model/bpm/1.0}assignee"

My code as below

var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:review";
workflow.parameters["bpm:workflowDescription"] = document.name;
workflow.parameters["bpm:groupAssignee"] = people.getGroup("GROUP_DEVELOPER");
workflow.parameters["wf:notifyMe"] = true;
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
workflow.parameters["bpm:workflowDueDate"] = futureDate;
workflow.execute(document);

Please help.

Unknown said...

How do you access that sanz:notifyMe property in a task inside a beanshell script and how inside a javascript script?

Bruce W. Ford said...

Help! This workflow won't start and I'm not seeing the issue!

I'm including my webscript JS below and the value of my variables are supplied in the model assignment lines below:

[code]
var nodeId = args.node_id;

var node = args.node_id;

if (node != null)
{
var workflowType = "jbpm$" + args.type;

var assignTo = people.getPerson(args.assignto);

var description = args.desc;

var workflow = actions.create("start-workflow");

workflow.parameters.workflowName = workflowType;

workflow.parameters["bpm:workflowDescription"] = description;

workflow.parameters["bpm:assignee"] = assignTo;

workflow.execute(node.nodeRef);
}

model.assignto = assignTo; //Node Type: {http://www.alfresco.org/model/content/1.0}person Node Ref: workspace://SpacesStore/70a6d0fe-0aa1-452b-97ae-b343e10a2654
model.node = node; //Node Type: {http://www.alfresco.org/model/content/1.0}person Node Ref: workspace://SpacesStore/70a6d0fe-0aa1-452b-97ae-b343e10a2654
model.type = workflowType; //jbpm$2 - the stock review and approve (single user) workflow...

[/code]

Error that I'm getting is:

03190082 Wrapped Exception (with status template): 03190150 Failed to execute script '/start_workflow.post.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions)': 03190149 Can't find method org.alfresco.repo.jscript.ScriptAction.execute(org.mozilla.javascript.Undefined). (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts Extensions/start_workflow.post.js#21)

I'm not understand what is undefined! I'd appreciate any pointers!

Bruce

Vivek said...

Hi Santoso,

The script is working fine when I am invoking it from my browser, but getting HTTP response code 505 when it is invoked from java code.

Below is the java code used to invoke the script:
URL conn = new URL(
"http://10.10.12.120:8080/alfresco/service/lombardi/initiateworkflow?n=FSMIS/Central Delhi/Firm/Temp/F15102012107/Raheja Industries&alf_ticket="+ticket);
URLConnection yc = conn.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));

Please help.

Sexyladyme26 said...

Managers and analysts can continuously improve performance by discovering process bottlenecks and inefficiencies.



alfresco workflow

Anonymous said...

Is it possible to execute a Workflow via Javascript without passing a document? I have only a ScriptTask in my process definition.

Thanks

Unknown said...

hi sir! thanks for your code tutorial. But can you please give the whole code of starting workflow from the start. Where is the connection to the repository? Thanks!!

Livingitup said...

.i don't want to use the forms which are available within the Alfresco Activiti BPM Suite v1.4.0. I want to create my own html forms. The html forms should be able to trigger a workflow in alfresco activiti. Any pointers as to how this can be done ??