cancel
Showing results for 
Search instead for 
Did you mean: 

Start workflow via JavaScript with the help of rules

fbehfar
Champ on-the-rise
Champ on-the-rise
Hi,
I want to create a rule, with the "execute a script" actions. I want to start the advanced workflow with the help of the rules.

Actually I want to automaticly start the workflow by adding a new content.

for example:
a user adds a content
the (whitepaper) workflow starts in backgroound and send a task for a specific users,
these users recieve a task on their pool tasks list.

Is it possible to start a workflow by defining a rule, at the time of adding a new content????
I guess one solution is to use the javascript in the rule definition, but how should the script look like?

Thank you,
FSB
34 REPLIES 34

cyberheap
Champ in-the-making
Champ in-the-making
Hi

I'am trying to create an JS script to start a paraller review work flow.

This is my JS:

var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:parallelreview";
workflow.parameters["bpm:workflowDescription"] = document.name;
workflow.parameters["bpm:assignees"] = "admin,psvent";
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
workflow.parameters["bpm:workflowDueDate"] = futureDate;
workflow.execute(document);

Here is the error log:

09:00:31,638 ERROR [org.jbpm.graph.def.GraphElement] action threw exception: Sourced file: inline evaluation of: ``wf_requiredPercent = wf_requiredApprovePercent; wf_reviewerCount = bpm_assignees . . . '' : Error in method invocation: No args method size() not found in class'java.lang.String'
Sourced file: inline evaluation of: ``wf_requiredPercent = wf_requiredApprovePercent; wf_reviewerCount = bpm_assignees . . . '' : Error in method invocation: No args method size() not found in class'java.lang.String' : at Line: 1 : in file: inline evaluation of: ``wf_requiredPercent = wf_requiredApprovePercent; wf_reviewerCount = bpm_assignees . . . '' : bpm_assignees .size ( )

So it looks like the bpm_assignees notation is not correct:

workflow.parameters["bpm:assignees"] = "admin,psvent";


Can anyone help me with this? Does someone know the correct notation for the bpm:assignees param?

Tnx,
Primoz

mikeh
Star Contributor
Star Contributor
The error would indicate that the code is expecting an array, rather than a string.

Thanks,
Mike

mikef
Champ in-the-making
Champ in-the-making
In reponse to wps07032 post above. You can pass in a nodeRef to you Web Script and then get a handle on the node using something like:

var document = search.findNode(yourNodeRef);

cyberheap
Champ in-the-making
Champ in-the-making
Hmmm…

I've also tried to assigne the paramater like this:

var users = new Array(2);
users[0] = "admin";
users[1] = "cyber";
workflow.parameters["bpm:assignees"] = users;

…still no effect.

No error but the workflow doesn't get assigned.

Does anyone have a working example of assigning a workflow via script action to multiple assignees?

Would appreciate very much.

Primoz

fbehfar
Champ on-the-rise
Champ on-the-rise
Hi cyberheap,
I've done it with the parallelReview, but it's working.
pay attention on the names, cause as I've understood they are case sensitive.
and check your workflow exact name, in workflow console,
and also check if the workflow has a mandatory aspect, if it's mandatory you should assign it in your script. I guess parallelReview has an aspect called requiredPercent. but you haven't assign it in your script.
I'm not sure about this, but put a space between comma and the next username. :!:
mine is working without arrays, just like this
workflow.parameters["bpm:assignees"] = "admin, X, Y";

hope these help

FSB

cyberheap
Champ in-the-making
Champ in-the-making
Tnx for the replies…


Now I have a "wierd" problem. I've listed the assignees as suggested: workflow.parameters["bpm:assignees"] = "bana, psvent";

…what happens now is this,

the first user get's the task assigned and everything works, but the second user listed doesn't see the task on the "My Task To Do" list, but sees it in the "All Active Tasks" list. If I then try to save changes or approve or reject the task, I get the error allready written in the previous post.

Any more clues from anybody?

Tnx, Primoz

cyberheap
Champ in-the-making
Champ in-the-making
I finally managed to make it work.

Here's the sample code:

var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:parallelreview";
workflow.parameters.requiredApprovePercent = 100;
workflow.parameters["bpm:workflowDescription"] = "Please review and approve: " + document.name;
workflow.parameters["bpm:assignees"] = [people.getPerson("admin"), people.getPerson("cyber")];
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
workflow.parameters["bpm:workflowDueDate"] = futureDate;
workflow.execute(document);

So the bpm:assignees parameter has to be an array of cmSmiley Tongueerson type nodes.

And here's a sample for the group review (It took me a while to figure it out and that I finally noticed that I have to put a GROUP_ prefix before the actual group name).

var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:parallelgroupreview";
workflow.parameters.requiredApprovePercent = 20;
workflow.parameters["bpm:workflowDescription"] = "Please review and approve: " + document.name;
workflow.parameters["bpm:groupAssignee"] = people.getGroup("GROUP_MyGroupName");
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
workflow.parameters["bpm:workflowDueDate"] = futureDate;
workflow.execute(document);

Regards,
Primoz

poptarts
Champ in-the-making
Champ in-the-making
This is extremely helpful. Thank you!

renzf
Champ in-the-making
Champ in-the-making
Hi, I found useful the code written above:
var document = search.findNode(yourNodeRef);
to link the workflow to a document, but can I start a workflow (using a script) without linking it to a document?
thanks, bye

witho
Champ on-the-rise
Champ on-the-rise
Hi,

I am using this JavaScript to start my advanced workflow and I get a NullPointerException at last line (workflow.execute(document);), because document doesn't exist. I see that I have to use the code written in this post:
var document = search.findNode(yourNodeRef);
to link the workflow to a document. But how can I write the NodeRef of my document, or my space?
And, if I want to start the workflow executing this Javascript with a rule every time I upload a new file to one of my user spaces how can I call the procedure execute of the workflow?

Thanks a lot and excuse my poor english.


var workflow = actions.create("start-workflow");
workflow.parameters.workflowName = "jbpm$wf:parallelgroupreview";
workflow.parameters.requiredApprovePercent = 0.0001;
workflow.parameters["bpm:workflowDescription"] = "Please review and approve: ";
workflow.parameters["bpm:groupAssignee"] = people.getGroup("GROUP_aprobdocscomercial");
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + 7);
workflow.parameters["bpm:workflowDueDate"] = futureDate;
workflow.execute(document);