Thursday, November 19, 2009

Running Sharepoint workflow in a loop

After installing MOSS service pack 2 some of the SPD workflows are not invoked when it calls itself. To solve this problem following method can be used.

In this solution , one workflow invoke the other one and continue in a loop. Loop is broken by using some conditions.

Workflow 1 (Test) - :
This is the main workflow which contains the business logic. For my example I use this workflow to create a task for the “Test List” entry.

Workflow 2 (Test 1) -:
This is a used for run the workflow 1 (Test) in a loop.

In the “Test List” three fields has been created as follows.



A new Item is added to the “Test List” as follows.



Here this should create 3 tasks in the task list and the workflow 1(Test) is given below



Here a new task is created (Task name = Title of the entry + Current number) and “Current number” is increased by 1. Then the workflow is paused for 1 minute. After completing the pausing “Task Created” set as “Yes” and stop (since now workflows are not run in a loop). Now workflow 2 (Test 1) starts to run. See the workflow 2 (Test 1) below.



Now workflow 2 check whether the value of current number is less than the value of maximum number. If it’s true that set the value of “Task Created” to “No”. Since this is an update, workflow 1 (Test) is invoked and it runs second cycle. If the value of current number is greater than the value of maximum number then the workflow 2 (Test 2) is stopped and that breaks the loop. Workflow 1 (Test) won’t be invoked again.

Please see the following steps to check the flow.



Three tasks has been created in the task list.