colinj
Contributor
Contributor

How do I find the last time a workflow was run?

I'm having some trouble figuring if I can have a workflow  figure out when it was last run (or if it has ever been run). The  scenario is this: I want the workflow to be run periodically and check a  database for new entries. The simplest thing to do is to have the  workflow run every five minutes and query the database for rows created  in the last five minutes. The problem there is what happens if the  workflow can't run or contact the database for a while?

So, what I would like to do is have the workflow do the following:

  1. Start
  2. Figure out the last time it ran successfully, if at all.
  3. Query the database for new rows since the last time it ran (or all rows if this is the first time the workflow is run).
  4. Get on with it's business

What I can't seem to figure out is how to ask when  the last time a workflow was run. I'm guessing that I'm missing  something very obvious.

Thanks!

0 Kudos
1 Reply
Burke-
VMware Employee
VMware Employee

For each workflow execution, there are various properties that include the variable values and start and stop times.. while you could iterate through to find the last execution and extract the dates, the   much easier way - and the way I have done in the past is like this:

  1. Create a Configuration Element to store some attributes for your workflows to store and retrieve values from.
  2. Create an attribute in the new configuration element named something like "lastRunTime"
  3. Add that configuration element as an attribute to your workflow as something like "config" (Type: ConfigurationElement)
  4. In the first scriptable task of your workflow, add the "config" attribute as an input
  5. Then enter the following in the script:
    <code>
    var lastRunTime = config.getAttributeWithKey("lastRunTime").value;
    System.log("Last Run Time: "+lastRunTime);
    </code>

    You could also assign "lastRunTime" as an output attribute to be used in other parts of your script.
  6. In  the last Scriptable Task of your workflow (Only in the  SUCCESSFUL  branch of the workflow), Add the "config" attribute as an  input and  enter the following in the script:
    <code>
    config.setAttributeWithKey("lastRunTime",);
    </code>

The overall thing (simplified) would look similar to this:

If you find this to be a reasonable solution that meets your needs, please be sure to give my reply credit Smiley Wink

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vRealize Orchestrator tips and tutorials - @TechnicalValues on Twitter
0 Kudos