Defining a Virtual Attribute

A virtual attribute is not a stored data value like a regular attribute. Instead, when you access the value of a virtual attribute, code is that calculates the value to be returned. You will use a virtual attribute to implement the total amount of the expense report.

To define a virtual attribute:

  1. In the Project Navigator, right-click on the ExpenseReport BPM Object (BPM Object icon 
				), and click New > Attribute (Attribute icon 
				). The Atribute dialog box appears.
  2. Enter total in the Name field.
  3. Select Decimal from the Type drop-down list, and click OK. The total attribute is created, and the atrribute editor for it opens.
  4. Set the Decimal Digits to 2.
  5. In the Storage Constraints section, set the Virtual option.
  6. Click Save (Save icon 
				) and close the editor. The total attribute becomes virtual.
  7. In the Project Navigator, expand the total attribute. Two methods are shown: Read Access (Get Method icon 
				) and Write Access (Set Method icon 
				).
  8. Double-click on the Read Access icon, or right-click and click Open (Open icon 
				). A PBL (Process Business Language) method editor window opens. The method is named read_access_code_total.
  9. The total attribute is the sum of every item's amount value. To have the total attribute return this value, it must be calculated by adding the amounts of all the items. Remove any existing code, and enter the following PBL code into the editor exactly as shown, including capitalization:
    amount as Decimal(2)
    
    amount = 0
    
    for each item in items do
        amount = amount + item.amount
    end
    
    return amount
  10. Save your changes and close the editor. You do not need to enter code for the Write method for this attribute.

You have added a virtual attribute to the ExpenseReport BPM object. In the following task you will define valid values for the costCenter attribute.