Unit 4: Creating Virtual Attributes

Use

In this unit you create and implement the virtual attribute TextWithDocumentNo. This attribute returns the text sales order <order number> of <date>. This text must be combined in the implementation program of the object type.

A virtual attribute always requires a supplement to the proposed 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: TextWithDocumentNo

    • Name: Text and number

    • Short text: Text and number of sales order

  4. Select Virtual in the area Source.

  5. Select Dictionary in the Data type reference area.

  6. Enter SYST in the Reference table field.

  7. Enter TITLE in the field Reference field.

    (You could also specify any character field with the required length here. Note that it is not the content of the field that is of interest, but its data type).

  8. Choose .

Implementing attribute 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 then automatically goes to the implementation program of the object type you created.

Virtual attributes

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

When you implement a virtual attribute, you must write the attribute value determined to the container of the object. Use one of the following macros for this purpose:

  • SWC_SET_ELEMENT CONTAINER '<ElementName>' <Attribute> for single-line attributes.

  • SWC_SET_TABLE CONTAINER '<ElementName>' <Attribute> for multiline attributes.

These macro commands are 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.

  • The variable OBJECT-TEXTWITHDOCUMENTNO is declared for the attribute (line 13 of the program, generated automatically).

  • You carry out the implementation yourself between the two macro commands GET_PROPERTY TEXTWITHDOCUMENTNO CHANGING CONTAINER and END_PROPERTY (lines 47-57).

    • The text is constructed from the values of the attribute already created and the key field of the object type. These attributes are read with the macro command SWC_GET_PROPERTY with reference to the object itself (object reference SELF). (Lines 51-52).

    • Assemble the text and assign it to the container element TextWithDocumentNo using the macro command SWC_SET_ELEMENT. (Lines 53-56).

Result

Testing virtual attributes

In order to test the attribute, it must first be released and then the object type must be generated.

  1. Position the cursor on the attribute TextWithDocumentNo.

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

You can now test the attributes created up to now:

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