When using a server event with a K2.ExpireActivity("EventName") in code, events following the server event in the same activity are not expired. This document explains how to work around the issue.

Retiring Events

Shown below is an example process. This process will be used to illustrate the steps required to retire all events, if required when the server event retires:


  

In the "Events" activity, there is a client event which sets a particular Boolean data field value ("Expire"). If "Expire" is true, the server event should expire the activity and continue to the "End Process" activity.
 

  

See below for an example of the Server event code:

C#

public void Main(ServerEventContext K2)
 {
  //if "Expire" data field is true, expire the activity
  if (bool.Parse(K2.ProcessInstance.DataFields["Expire"].Value.ToString()))
  {
  K2.ExpireActivity("Events");
  }
  else
  {
  //blank server event
  K2.Synchronous = true;
  }
 }

See below for an example of the Server event code :

VB

Sub Main(ByVal K2 As ServerEventContext)

 'if "Expire" data field is true, expire the activity
 If System.Convert.ToBoolean(K2.ProcessInstance.DataFields("Expire").Value.ToString()) = True Then
  K2.ExpireActivity("Events")
 Else
  'blank server event
  K2.Synchronous = True
  End If

 End Sub

 
When the process executes and the server event expires the activity, you will notice that the process instance status remains "Active", and that there is a client event that is "Active", event though one would expect that the client event would have been expired by the Server event.
 
Process Status is still active:

  

 
The client event after the server event was not expired:

  

 

In order to resolve this issue, it is necessary to add an additional line to the server event to execute the server event Asynchronously.

 

C#

public void Main(ServerEventContext K2)
{
 //if "Expire" data field is true, expire the activity
 if (bool.Parse(K2.ProcessInstance.DataFields["Expire"].Value.ToString()))
 {
  // add the following line to set the server event to execute asynchronously
  K2.Synchronous = false;
  K2.ExpireActivity("Events");
 }
 else
 {
  //blank server event
  K2.Synchronous = true;
 }
}

 VB

Sub Main(ByVal K2 As ServerEventContext)

'if "Expire" data field is true, expire the activity
If System.Convert.ToBoolean(K2.ProcessInstance.DataFields("Expire").Value.ToString()) = True Then
'add the following line to set the server event to execute asynchronously
K2.Synchronous = False
K2.ExpireActivity("Events")
Else
'blank server event
K2.Synchronous = True
End If

End Sub

If a server event is set to Asynchronous (K2.Synchronous = False), the server event item will not "complete", but instead wait for an external call to finish the server event. In the code sample above, the following line (K2.ExpireActivity) will expire the activity as a whole, before the server event completes and the following client event can execute.

For more information on Synchronous and Asynchronous server events, refer to the following location in the K2.net® 2003 Help File: "References|K2.net 2003 Objects|Namespaces|SourceCode.KO Namespace|Classes|ServerEventContext|Properties|Synchronous Property"

After making the change to the Server event code, exporting the process design and starting a new process instance, you can verify that the process status is "Completed" if the activity is expired:

  

 
You will notice that the subsequent client event was not created, since the activity was expired before the client event existed: