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.

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 more 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 text: 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. Select for this.

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.

  • Let the system create the template for the missing method. 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 Start of the navigation pathEdit Next navigation step Change Release Status Next navigation step Object Type Component Next navigation step To ImplementedEnd of the navigation path.

  3. Choose .

    The system informs you if the object type still contains errors. Try to solve the error using the error overview (Start of the navigation pathGoTo Next navigation step Error ListEnd of the navigation path).

Test the attributes created so far.

  1. Choose .

    You get to the Test Object Type <Object Name>: No instance connected.

  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).