How to trigger a workflow based on a date value in SQL?

  • 4 March 2014
  • 7 replies
  • 9 views

Badge +3

Hello all!

 

I'm hoping that someone can help me/been in the same situation... I want to start a workflow based on a value in SQL, the scenario is:

 

A table containing all employee records

Within the table, training courses may have an expiry date

If the expiry date is approaching (i.e. next 7 days) start a workflow.

 

Does anyone have any ideas on how to achieve this? As no workflow is active, I'm not sure how to start it from an outside system.

 

Any help you can provide is greatly appreciated.

 

Kind Regards,

Dayna


7 replies

Badge +2

Hello there.

 

This could be achieved in MANY different ways, but I'll explain one simple and easy way.  I'm not saying this is the best or worst way, but just one of many possible.  :)

 

- You could create a simple console application that is scheduled to run via Windows Task Scheduler (or similar).

- In the application, you would make a call to your database to get those employees that you want to start workflows for (those expiring within 7 days, etc.). 

- You would then iterate through each record and use the K2 Client API (SourceCode.Workflow.Client.dll) to create and start a new workflow.

- There are other things to think about also - as you mentioned expiring "within the next 7 days" - so you'll want to track who you've started workflows for if this process runs say, daily, for example.  You may not want to launch multiple workflows for the same employee when he/she shows up in the results the next time you run the app.

 

Here is some sample code that may help you to get an idea.  The section of note is within the ForEach - this is where the workflow is being started.

 

public void StartWorkflowsForUpcomingExpirations()
{
// get the employee records with matching criteria
// i.e. expiring within 7 days
var employees = repository
.Get<Employee>()
.Where(emp => emp.ExpiryDate > DateTime.Today && emp.ExpiryDate <= DateTime.Today.AddDays(7))
.ToList();

// iterate through each employee and start a new workflow for each one
employees.ForEach(emp =>
{
// create a new connection
var conn = new SourceCode.Workflow.Client.Connection();

// open the connection, passing in your server name
conn.Open(serverName);

// optional - if you need to impersonate a different user starting the workflows
conn.ImpersonateUser(someUserName);

// create the new process instance, passing in the name of the process
var procInst = conn.CreateProcessInstance(processName);

// set the folio name to something meaningful
procInst.Folio = string.Format("Employee Expiry Process for Emp: {0}", emp.EmployeeID);

// start the process instance
conn.StartProcessInstance(procInst, true);
});
}

 

I hope this helps.  Let me know if you need more detail on anything.

Badge +3

Mike,

 

Thank you for your comprehensive response! I'll give that a go, thank you!!

 

Kind Regards,

Dayna

Badge +4

I've not tried it yet, but when we posed this question to K2 support they suggested scheduling a PowerShell script from windows task scheduler that launched the workflow.

Badge +6

I think you could do this easily without code using K2 Studio.  You would just need a SmartObject to the table with the values in question. 

 

1.  Create a "Kickstart" K2 process with a server activity using plan pet slot (no destinations).  Use "Select a list field to determine how many slots should be created" and select the List method of your SmartObject.  Don't use a filter, and return the appropriate value.  In this case it might be expiry date.

 

2.  What I would probably do from here is create a sub-K2 process and place an IPC event in the activity created in step 1 that kicks off the sub-K2 process.

 

3.  In the IPC event, send in the expiry date and primary key column.

 

4.  In the sub-K2 process the first activity should be just an evaluation that determines if the expiry date is worthy of processing.  If it is, have it follow a line rule that runs the actual workflow processing.  If it is not within 7 days, just have the process end.

 

You can then schedule the "Kickstart" process to run as much as you would like using the Process Scheduler in SharePoint.

 

Using this method you will have a lot of processes running, but K2 can handle it, and you won't have to write any code.

Badge +4

True, I forgot you can also use SharePoint to schedule a process/workflow using the Process Portal. See the bottom of this blog article for instructions: http://old.k2underground.com/blogs/blackbelt/archive/2011/09/02/scheduling-a-workflow-on-several-list-items.aspx

 

(You can also access the article and its images here http://web.archive.org/web/20130910035109/http://www.k2underground.com/blogs/blackbelt/archive/2011/09/02/scheduling-a-workflow-on-several-list-items.aspx )

 

If you agree that K2 should have its own native scheduler for starting processes/workflows, submit a feature request support ticket at http://portal.k2.com and maybe we can get them to add it in the next release. :-)

Badge +3

You can also use the K2 Process Scheduler (in K2 process portal in sharepoint) to kick off a workflow that checks your sql table and evaluates your table.  If there are items that are set to expire or expired then you have your Workflow "Checker" kick off an an IPC or other workflow Asychronously.

 

Much easier with no code.

Badge

i am new to k2 . i am in need to implement the auto trigger batch process using k2 blackpearl

i got idea to implement the batch process via windows service.

using windows service i want to auto trigger the workflow.

focus of this task is generating payslip at end of every month and send the payslip via mail.

how to auto trigger the workflow via windows service?

Reply