Please ensure Javascript is enabled for purposes of website accessibility
Powered by Zoomin Software. For more details please contactZoomin

AVEVA™ Work Tasks

How to Iterate Collection Object?

  • Last UpdatedJul 01, 2024
  • 4 minute read

This example illustrates how to iterate a collection object. It is possible to persist an object's state in a long running workflow by using variables of type Object. In some scenarios, complex objects may be a collection of other complex objects. Here, one may be required to iterate through the collection to get the desired object.

For example, EmployeeCollection object returned from a WCF Service contains collection of an employee object. To get all the information of an employee, you are required to iterate through the employee collection.

This can be done by collective use of some AVEVA Work Tasks activities as described below. It shows how one can iterate through the collection object in a workflow. This enhances the capability of holding the collection object in variables of Object type and iterating through the collection for using the items in the collection object.

Scenario

Employee collection object, which is returned from WCF Service, is to be iterated for displaying employee information.

Steps to design the scenario

  • Create a class with Employee object collection.

    Note: You can make use of an existing class to achieve the scenario.

Employee class: Holds the employee information.

EmployeeCollection class: Holds the Employee object collection i.e., collection of employee information.

Note: Only the objects that can be serialized are persisted in long running workflows. Objects that need to be stored in the variables should be marked as Serializable.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Demo

{

[Serializable]

public class Employee

{

string _name = string.Empty;

int _age = 0;

string _employeeId = string.Empty;

public string Name

{

get {return _name ;}

set { _name = value;}

}

public int Age

{

get { return _age; }

set { _age = value; }

}

public string EmployeeId

{

get { return _employeeId; }

set { _employeeId = value; }

}

public Employee()

{

}

public Employee(string Name, int Age, string EmployeeId)

{

this._employeeId = EmployeeId;

this._age = Age;

this._name = Name;

}

public Employee GetEmployee()

{

return this;

}

}

[Serializable]

public class EmployeeCollection

{

List<Employee> employeeList = new List<Employee>();

public EmployeeCollection()

{

this.employeeList.Add(new Employee("Kevin",30,"SK001"));

this.employeeList.Add(new Employee("Albert",30,"SK002"));

this.employeeList.Add(new Employee("Jim",30,"SK003"));

this.employeeList.Add(new Employee("Jo",30,"SK004"));

this.employeeList.Add(new Employee("Roger",30,"SK005"));

this.employeeList.Add(new Employee("John",30,"SK006"));

}

public List<Employee> GetEmployees()

{

return this.employeeList;

}

}

}

  • Use the code given above to create an assembly.

  • Place the assembly in SOA folder, and register the assembly in SOA Assemblies.

  • Create object instances for Employee and EmployeeCollection class in the SOA property editor.

  • Create variables in the Start action.

Variable description

  • count: To keep track of the iteration

  • EmployeeObject: To store the employee details

  • loopcount: Total number of times the loop needs to be executed

  • Add an Update Variable activity next to the Start activity. This activity is used to update the loopcount variable for the total number of times the EmployeeCollection object is to be iterated.

  • Total count can be derived from the EmployeeCollection object listed in the expression editor. As the GetEmployees() method returns the collection, you can get the total count of the collection from here.

Sample Code

EmployeeCollection.GetEmployees().Count - 1; //Get the actual count of the items in the collection object. Subtracting one from it will give the actual loop count required.

  • Drag and drop the For-Loop activity from the Engine activities.

  • Drag and drop the Logger activity next to the SOA Execute activity. This is done to display the employee information each time when the object is updated to the variable.

Sample code

((Demo.Employee)Variable.EmployeeObject).Name;

Variable EmployeeObject holds the employee information. In order to get the property of the object we need to type cast to the proper object type. i.e., the Employee object, Demo.Employee.

  • Place the Next Loop activity to iterate through the next item in the collection.

  • Execute the workflow.

  • During execution, the workflow gets the EmployeeCollection object and iterates through the items, and prints the employee name in the logger.

Using this, it is possible to persist complex objects in the workflow and iterate through the collection object to get an item from it.

TitleResults for “How to create a CRG?”Also Available in