Dynamic escalation


Badge +11
It doesn't matter where or how you do it - as long as you've got a Process level datafield (containing a comma-separated list of users) or XML field (containing a repeating node with list of users) with the appropriate users who should act as Destinations for the Activity BEFORE you reach the line of code:
K2.Destinations.Add(DestinationType.User, "K2MEGAKarenB")

You would then typically:
Loop through List of Users
'Assign current user as Destination
K2.Destinations.Add(DestinationType.User, CurrentUser)
End the Loop


Hope this helps,
Ockert

10 replies

Badge +8
Hi all

I want to route process dynamically and will be picking values from db table. Well just to see how k2 creates code i dragged one user in destination rule....here is the code.

Sub Main(ByVal K2 As DestinationRuleContext)


Dim oDSH As New SourceCode.K2Utilities.DSHelper()
Dim oDEManager As System.DirectoryServices.DirectoryEntry
Dim sManager As String
Dim bAllTrue As Boolean = True
Dim oDest1 As New CDest1
oDest1.Main(K2)
If oDest1.IsSuccess Then
'== sendto
K2.Destinations.Add(DestinationType.User, "K2MEGAKarenB")
If Not bAllTrue Then Exit Sub
End If
End Sub

'// Destination Classes

Private Class CDest1
Public IsSuccess As Boolean = False

Sub Main(ByVal K2 As DestinationRuleContext)
Me.IsSuccess = True
End Sub

End Class


Now the question is how do i pick values from db table ...do i have to write code in above mentioned block or can i write code in normal aspx page.

Any input will be appreciated. Code snippets will be highly appreciated.

Thanks in advance
Badge +8
Hi Again

Thanks for the immediate reply. I really appreciate. I would appreciate if you explain more as i think i am not going to add any user when i am creating process in studio so how will i get comma seperated list of users in process level data field.

Please explain ... Thanks in advance
Badge +8
Hey Again ,

Do you mean that while creating worklfow in studio i genrate code and
K2.Destinations.Add(DestinationType.Users , K2.ProcessInstance.DataFields("name").ToString).

and make this change i.e. instead of actual username i change it to process instance data fields and then i can insert domainusername,password in this data field while executing from aspx page..

Thanks in advance
Badge +11
You're on the right track...

The easiest way to get a list of usernames from SQL, would be to use a SQL Data event in a separate Activity BEFORE the Activity which should contain Dynamic Destinations. Drag a SQL Data Event to an Activity and complete the steps in the Wizard. Generate the code behind the event and change it to not only give you the last item retrieved from SQL but rather a comma separated list of values...

The code behind your SQL Data Event should look something like (obviously you'll have a different sServer, sUID, sPWD, DataBase and SQL Query):

" & _ 
" FROM K2Users "

OpenConnection(sqlConn)

Dim rReader As System.Data.SqlClient.SqlDataReader
rReader = sqlComm.ExecuteReader
While rReader.Read
K2.ProcessInstance.DataFields("DestUsers").Value = K2.ProcessInstance.DataFields("DestUsers").Value & "," & rReader("UserName").ToString
Console.WriteLine(">>> " & K2.ProcessInstance.DataFields("DestUsers").Value)
End While
rReader.Close()
Catch ex As System.Exception
Throw New System.Exception(ex.Message)
End Try

SqlConn.Close()
K2.Synchronous = True

End Sub


Where K2.ProcessInstance.DataFields("DestUsers").Value is your Process level datafield containing the comma separated list of users.

After this event, the process level datafield should contain a list similar to:
",User1,User2,User3"

In your next Activity's Destination Rule, your code for extracting each username from the comma separated list should look something like:

Sub Main(ByVal K2 As DestinationRuleContext)

Dim oDSH As New SourceCode.K2Utilities.DSHelper()
Dim oDEManager As System.DirectoryServices.DirectoryEntry
Dim sManager As String
Dim bAllTrue As Boolean = True
Dim oDest1 As New CDest1

Dim DestUsers() As String
Dim intX As Integer

oDest1.Main(K2)
If oDest1.IsSuccess Then
'== sendto

DestUsers = Split(K2.ProcessInstance.DataFields("DestUsers").Value, ",")
For intX = 1 To UBound(DestUsers)
Console.WriteLine(">>> " & intX & ": " & DestUsers(intX))
K2.Destinations.Add(DestinationType.User, DestUsers(intX).ToString())
Next intX

If Not bAllTrue Then Exit Sub
End If
End Sub

'// Destination Classes
Private Class CDest1
Public IsSuccess As Boolean = False

Sub Main(ByVal K2 As DestinationRuleContext)
Me.IsSuccess = True
End Sub

End Class


Hope this helps,
Ockert
Badge +8
Thanks Again mate,

Well i really appreciate for the effort you put in to provide such a detail. Though your method is the easiest but i want to tell you the scenerio so that you can let me know which one should be easy to use.

In my workflow each activity will have dynamic destination. So where should i drag and drop k2.net sql data event.

I was thinking that i just drop destination rule onto activity and from my aspx page i will query user table and get the immediate manager of the person who started the process. Then i just update the k2 process level data field and assign to the k2 destination something like it. Will it work ?

K2.Destinations.Add(DestinationType.Users , K2.ProcessInstance.DataFields("name").ToString).

Thanks a tons for all of your responses.
Looking forward to hear from you
Badge +11
Sure you can do it through aspx pages - as long as you set the Process level datafield to the desired Destination User(s) for the next Activity. I've only used the SQL data event for illustration purposes.

Regards,
Ockert
Badge +8
Hi Again ,

Well i am having some issues with dynamic destinations. here is the scenerio.

While creating Process i generate code for destination user something like this

If oDest1.IsSuccess Then
'== sendto
K2.Destinations.Add(DestinationType.User, lcase(trim(K2.ProcessInstance.DataFields("Manager").ToString)))
If Not bAllTrue Then Exit Sub
End If

Where Manager is my process level data field "K2.ProcessInstance.DataFields("Manager")".

In My Aspx page i am doing ...


Dim myConn As New Connection
Dim myProc As SourceCode.K2ROM.ProcessInstance
Try

myConn.Open(Me.ServerName, "")
myProc = myConn.CreateProcessInstance(ProjectDetail)
myProc.Folio = FolioName
myProc.DataFields("Manager").Value = "k2megakarenb"

means setting value for data field Manager.
But it is not routing to user.. Please suggest.

Thanks in advance
Badge +8
Got It ...Thanks
Badge +11
So you're sorted??

If so, would it be possible for you to post your resolution and/or what you did wrong so that other forum members can benefit from your experience.

Thank you,
Ockert
Badge +11
Thanks K2.netdeveloper...

Solution posted here: http://forum.k2workflow.com/viewtopic.php?p=2565#2565

Reply