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 calculates the value to be returned.

You use a virtual attribute to implement the total amount of the expense report. In programming terms, you can think of a virtual attribute as a method or a pair of methods to either set or get a value.

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 Attribute dialog box appears.
  2. Enter the word total in the Name field.
  3. Select Decimal from the Type drop-down list, and click OK. The total attribute is created, and the attribute 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 is now 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 Read Access. 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 (a line with return 0 should be all that you find), 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.