Wednesday, November 28, 2007

Voyages with BlackPearl - 3.Team member becomes owner of a task

I have a task in the workflow that is assigned to a group of users. I want the person that actions the task to then 'own' subsequent tasks which are assigned only to them.

I'm using a workflow that is integrated to InfoPath and there are two InfoPath client activities. Let's call the first activity 'TEAM Activity' and then the subsequent activity 'USER Activity'.

One of the good things about BlackPearl is the ability to assign a Destination User not just to a user or a role but to a data field.
This solution uses that ability, so I created a DataField called 'DestinationUser' and an Environment variable called 'Domain'.

It also uses a field from the InfoPath form which stores the current user. Just use the userName() function as the default value and you have the name of the current user. See this post

1. TEAM Activity is assigned to the Role of the 'A Team' where membership of this role is determined through K2.
2. So when you run through the InfoPath client wizard, select the 'A Team' role in the usual way.
3. Now on the TEAM Activity create a Server Code event and make sure it is underneath the InfoPath client event.
4. In the Server Event add the following code:

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

// call custom utility that uses SelectSingleNode to retrieve value of

//node using namespace
string CurrentOwner = Utils.GetXMLValue(doc, "my:CurrentOwner");

K2.ProcessInstance.DataFields["NewOwner"].Value = CurrentOwner;
// Set DestinationUser datafield
K2.ProcessInstance.DataFields["DestinationUser"].Value = K2.StringTable["Domain"] + @"\" + K2.ProcessInstance.DataFields["NewOwner "].Value.ToString();

Note the use of K2.ActivityInstanceDestination.XmlFields["Root"] on the second line. Using ProcessInstatance won't work because it hasn't been updated yet.

On the USER Activity when you run through the InfoPath client wizard, select the Destination User to be the 'DestinationUser' data field.

A word of caution. Bad things happen if you don't set the DataField to a value. K2 will understandably throw a runtime error as it doesn't know who to assign the client activity to. Also think through the implications of redirecting a task. You may need to reset the current user on other activities. But I've found this trick really useful.

No comments: