5 Replies Latest reply on Sep 18, 2019 8:48 AM by ImranMughal01

    Call A workflow In Scriptable Task

    sean_gadson Novice

      Hello,

      is there a way to call a workflow in a scriptable task like you can with actions? 

        • 1. Re: Call A workflow In Scriptable Task
          qc4vmware Master

          Yes, but you have to retrieve the workflow object somehow.  There are plenty of ways to do that from statically defining it or looking it up by name or uuid etc.   It has a .execute() method.  You'll need to pass it in a properties type set correctly for each input needed to run the workflow.

          • 2. Re: Call A workflow In Scriptable Task
            barjinders Novice
            VMware Employees

            The whole process can be summarised as below:

             

            1) Get the workflow ID  for the workflow you want to run. You can get it either in code or copy it from GUI ->General Tab-> ID.

            2)Use an inbuilt library action to get the WF Object.

                var wfObject = System.getModule("com.vmware.library.workflow").getWorkflowById("<WF ID>");

            3) Input Parameters are passed as properties:

                var workflowParameters = new Properties();

                 workflowParameters.put("<InputParameter Name>", <value>);

             

            4) Execute the workflow with properties as input parameters. 

            var token = wfObject.execute(workflowParameters);

             

            5) Wait for the workflow to complete. You can use an inbuilt library action for that.

                        System.getModule("com.vmware.library.workflow").waitAllWorkflowComplete([token]);

                       

            6) Log if you want:

            System.debug("Workflow: " + token.name + "State: " + token.state);

            1 person found this helpful
            • 3. Re: Call A workflow In Scriptable Task
              ImranMughal01 Lurker

              how can you grab the workflow ID from its name in a scriptable task?

               

              So i dynamically want to run a different workflow in the scriptable task on depending on what the script is doing.

               

              For instance, my script loops through API calls defined in a config attribute and depending on the API call I need to run a different workflow each time.

               

              I don't want to have to map a workflow id to a workflow name as a definition in a config attribute if possible.

               

              thanks

              • 4. Re: Call A workflow In Scriptable Task
                iiliev Champion
                VMware EmployeesCommunity Warriors

                Here is some scripting code showing how to retrieve workflow(s) with a given name:

                 

                var all = Server.query("Workflow", "name='My custom workflow'"); // provide your workflow name in the second parameter
                for each (var wf in all) {
                  System.log(wf.id);
                }
                

                 

                Note that there could be more than one workflow with the same name (in different folders), so you need to add some logic to handle this situation.

                 

                Another way to find the workflow(s) by name is to enumerate all workflows and filter out those whose names match. Check the scriptable action com.vmware.library.workflow/getAllWorkflows for sample code how to recursively enumerate all workflows.

                1 person found this helpful
                • 5. Re: Call A workflow In Scriptable Task
                  ImranMughal01 Lurker

                  works like a dream - thanks!