cancel
Showing results for 
Search instead for 
Did you mean: 

how to start workflow using java

pamela
Champ in-the-making
Champ in-the-making
what if i already have a defined and deployed workflow and i just want to start it automatically using java?
  could my java file just contain all the details needed for the workflow? (admin as the assigned to the task and notified in email)

(we figured out how to upload files from UI (flex) to java then to alfresco already  *modifying the alfresco SDK firstwebclient*, so we're assuming that we can start a workflow too, using java, we just don't know how and where to start) please help… if there ever was a tutorial or some articles that you think could help us please point us there… :cry:
11 REPLIES 11

mrogers
Star Contributor
Star Contributor
Yes you can do this.

The process is roughly, create a package, set up your properties, get the workflow, start a new instance of the workflow.

Here's a snippet of some java code I am working on at the moment that kicks off a new workflow and runs the start task.

      NodeRef wfPackage = this.workflowService.createPackage(null);

      Map<QName, Serializable> workflowProps = new HashMap<QName, Serializable>(
            16);
      workflowProps.put(WorkflowModel.ASSOC_PACKAGE, wfPackage);
      workflowProps.put(WorkflowModel.ASSOC_ASSIGNEE, inviteeNodeRef);
      workflowProps.put(
            WorkflowModelModeratedInvitation.ASSOC_GROUP_ASSIGNEE,
            roleGroup);
      workflowProps.put(
            WorkflowModelModeratedInvitation.WF_PROP_INVITEE_COMMENTS,
            inviteeComments);
      workflowProps.put(
            WorkflowModelModeratedInvitation.WF_PROP_INVITEE_ROLE,
            inviteeRole);
      workflowProps.put(
            WorkflowModelModeratedInvitation.WF_PROP_INVITEE_USER_NAME,
            inviteeUserName);
      workflowProps.put(
            WorkflowModelNominatedInvitation.WF_PROP_RESOURCE_NAME,
            resourceName);
      workflowProps.put(
            WorkflowModelNominatedInvitation.WF_PROP_RESOURCE_TYPE,
            resourceType.toString());

      // get the moderated workflow

      WorkflowDefinition wfDefinition = this.workflowService
            .getDefinitionByName(WorkflowModelModeratedInvitation.WORKFLOW_DEFINITION_NAME);
      if (wfDefinition == null) {
         // handle workflow definition does not exist
         Object objs[] = { WorkflowModelModeratedInvitation.WORKFLOW_DEFINITION_NAME };
         throw new InvitationException("invitation.error.noworkflow", objs);
      }

      // start the workflow
      WorkflowPath wfPath = this.workflowService.startWorkflow(wfDefinition
            .getId(), workflowProps);

      String workflowId = wfPath.instance.id;
      String wfPathId = wfPath.id;
      List<WorkflowTask> wfTasks = this.workflowService
            .getTasksForWorkflowPath(wfPathId);

      // throw an exception if no tasks where found on the workflow path
      if (wfTasks.size() == 0)
      {
         Object objs[] = { WorkflowModelModeratedInvitation.WORKFLOW_DEFINITION_NAME };
         throw new InvitationException("invitation.error.notasks", objs);
      }

      try {
         WorkflowTask wfStartTask = wfTasks.get(0);
         this.workflowService.endTask(wfStartTask.id, null);
      } catch (RuntimeException err) {
         if (logger.isDebugEnabled())
            logger.debug("Failed - caught error during Invite workflow transition: "
               + err.getMessage());
         throw err;
      }

pamela
Champ in-the-making
Champ in-the-making
thank you! i've managed to call my workflow through webscripts though, using java scripts. Smiley Happy

nowhere
Champ in-the-making
Champ in-the-making
Hi,
can someone show me the foundamental steps to follow to start a workflow from a rule?
If my advanced workflow needs input from user (that'is assignee for example), how can I implement this? Is still possible to use a webscript?

Any help will be appreciated, I'm very confused now  :roll:

pamela
Champ in-the-making
Champ in-the-making
yes you can use webscripts, Smiley Happy

insert in your javascript the details you want your workflow to carry..

nowhere
Champ in-the-making
Champ in-the-making
Hi,
Thanks for answer…
I meant that I would show a dialog to allow user insert information needed by my workflow. Is this still possible using javascript?

mrogers
Star Contributor
Star Contributor
What you need to do is add a workflow task to gather the information from your user.

pamela
Champ in-the-making
Champ in-the-making
once you've set your workflow for that, and supposed to activate it using javascript,
you can set your script to accept values (or anything from the user).

from your example, you need to pass the bpm_assignee from the user right?
what i did in mine was i have hard coded whom to assign my workflow, like this:

workflow.parameters["bpm:assignee"] = people.getPerson("pamela");

the user "pamela" can be changed to receive values inputted by the current user.

sanket
Champ on-the-rise
Champ on-the-rise
I need to add a resource (file attached) when the workflow starts. I have used the same above code.
The task is generated and listed at the initiator's dashboard. But how can I add a resource ?

WorkflowModelNominatedInvitation.WF_PROP_RESOURCE_NAME,
resourceName);
workflowProps.put(
WorkflowModelNominatedInvitation.WF_PROP_RESOURCE_TYPE,
resourceType.toString());

What do I need to pass in the above parameters - resourceName and resourceType ?

I have the nodRef of the file which I need to pass as resource.I just need to know how do I pass it
as parameter ?

sanket
Champ on-the-rise
Champ on-the-rise