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
-
Position the cursor on the entry Attributes.
-
Choose
and answer the query Create with ABAP Dictionary field proposals? with No. -
Enter the following texts for the attribute to be created:
-
Attribute: TextWithDocumentNo
-
Name: Text and number
-
Short text: Text and number of sales order
-
-
Select Virtual in the area Source.
-
Select Dictionary in the Data type reference area.
-
Enter SYST in the Reference table field.
-
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).
-
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.
-
Position the cursor on the attribute TextWithDocumentNo.
-
Choose .
-
Choose
.The system informs you if the object type still contains errors. Try to solve the error using the error overview ().
You can now test the attributes created up to now:
-
Choose
.You get to the Test Object Type <Object Name>: No instance connected.
-
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.
-
Choose
.The Test Object Type <Object Name> screen then appears, in which you can test your object type (execute methods, check attribute values).

