Unit 5: Creating Object Virtual Attributes

Use

In this unit you create and implement the object virtual attribute sales group. This attribute returns the sales group of the sales order as an object reference.

Objects of the type sales group have only one key field. Therefore, it is not actually necessary to implement this attribute as a virtual attribute. You could also create it as an object database field attribute.

This basic example is simply intended to demonstrate how a virtual attribute is implemented.

A virtual attribute always requires an implementation in the implementation program.

Prerequisites

You use the macro commands provided by the system to implement these attributes.

For further information on the macro commands provided, see:

Procedure

  1. Position the cursor on the entry Attributes.
  2. Choose and answer the query Create with ABAP Dictionary field proposals? with No.
  3. Enter the following texts for the attribute to be created:

    Attribute: SalesGroup
    Name: Sales group
    Short description: Sales group

  4. Select Virtual in the area Source.
  5. Select Object type in the Data type reference area.
  6. Enter TVKGR in the Object type field.
  7. Choose .

Implementing attributes in the implementation program

Check the definition of the object type so far. To do so, choose .

The system detects that the implementation is missing and allows you to generate a template automatically for the missing source text.

Make sure you choose this option.

The source text generated automatically for implementing virtual attributes is always incomplete and restricted to setting the relevant container element. You must make changes here and implement the read procedure in the implementation program, which determines the attribute value by evaluating the database contents at runtime.

The system goes to the implementation program.

Virtual attribute

A virtual attribute is implemented between the macro commands GET_PROPERTY <attribute name> CHANGING CONTAINER and END_PROPERTY . The program code here determines how the value of the virtual attribute is derived at runtime.

The value determined is then stored in a container. This container forms the interface of the object type.

When you implement a virtual attribute, you must create an object reference. To create an object reference with given key fields and a known object type, use the following macro command:

  • SWC_CREATE_OBJECT <Object> <ObjectType> <ObjectKey>.

The variable <Object> must first be declared with:

  • DATA: <Object> TYPE SWC_OBJECT.

This data declaration is already in the automatically-generated source text.

Analyze the implementation program as it stands. You can use the implementation program in the appendix as a comparison.

  • Since the attribute is to return an object reference, a variable OBJECT-SALESGROUP with type SWC_OBJECT is declared automatically.
  • The actual implementation must be carried out manually. It must be between the two macro commands GET_PROPERTY SALESGROUP CHANGING CONTAINER and END_PROPERTY . The system inserts the macro command between these macro commands, with which the object reference to be created is written to the container.

Add the following two lines to the implementation program:

SELECT SINGLE * FROM VBAK WHERE VBELN = OBJECT-KEY-SALESDOCUMENT.
SWC_CREATE_OBJECT OBJECT-SALESGROUP 'TVKGR' VBAK-VKGRP.

  • Save your implementation program.

Result

Testing the virtual attribute SalesGroup

  1. Position the cursor on the attribute just defined.
  2. Choose Edit ® Change Release Status ® Object Type Component ® To Implemented.
  3. Choose .

The system informs you if the object type still contains errors. Try to correct these errors in the error overview (Goto ® Error list).

Test the attributes created so far.

  1. Choose .

    The Test Object Type <Object Name>: No Instance screen appears.

  2. Choose Instance.

    Identify an object of the type sales order by entering the number of a sales order of your choice. Use the F4 input help if necessary.

  3. Choose .

The Test Object Type <Object Name> screen then appears, in which you can test your object type (execute methods, check attribute values).