How many out there think there are two different scopes for data and XML fields: process and activity? Or are there? There are actually three different scopes for data fields: process, activity, and activity destination instance (slot) scope. Depending on the scope, you get different behavior and access the data fields differently in a server code event.
What’s the scoop on scope?
• Process data/xml fields have scope for the entire life of the process instance
• Activity data/xml fields are in scope only during activity in which they reside and there is only one copy of the field for the whole activity
• Activity destination instance data/xml fields are in scope only during activity in which they reside but there is one copy of the field for each slot in the activity
I think the difference between process scope and activity scope is common knowledge. But why the difference between activity and activity destination instance? Suppose you need to capture the distinct action selected by each user in a client event with multiple slots. In this case, you would use activity destination instance scope. In this example that was created automatically by a client event wizard, notice the Shared box is not checked. This is what makes it an activity destination instance field.

To enable complex scenarios in blackpearl, we separate the Actions a user can take from the Outcomes of a client event. There are plenty of good explanations of Actions & Outcomes posted elsewhere, but for review think of it as the difference between the possible actions any given user can take versus the outcome of a group consensus when there are multiple slots. Here is the Outcome data field automatically created by a client event wizard. Notice in this case the Shared box is checked because there will be only one outcome for the whole activity:

How do you access the different data field scopes in a code event?
You can only access activity data fields in a code event or line rule that is in the same activity where the field is declared. To access the current activity instance fields you must use the Plan Per Destination / Plan All At Once advanced destination rule option. Below is a simple example that shows how to access the different data fields in a server code event. The same code will also work if you have only one slot with the Plan Just Once destination option.
System.Diagnostics.Debug.WriteLine("***Process Data Fields***");
foreach (DataField df in K2.ProcessInstance.DataFields)
{
System.Diagnostics.Debug.WriteLine(df.Name + " = " + df.Value.ToString());
}
System.Diagnostics.Debug.WriteLine("***Activity Data Fields***");
foreach (DataField df in K2.ActivityInstanceDestination.ActivityInstance.DataFields)
{
System.Diagnostics.Debug.WriteLine(df.Name + " = " + df.Value.ToString());
}
System.Diagnostics.Debug.WriteLine("***Destination Data Fields***");
foreach (DataField df in K2.ActivityInstanceDestination.DataFields)
{
System.Diagnostics.Debug.WriteLine(df.Name + " = " + df.Value.ToString());
}
Final Thoughts…
If you change the definition of a field that you create between activity scope and activity instance, you will have to re-run all wizards where you used it, delete the old field, and add the new field, even if the field name is the same. That’s because even though the name is the same, you are declaring it in a different scope and it is evaluated according to that scope.