Using the Entity Manipulation Language

Objectives

After completing this lesson, you will be able to:

  • Implement an EML statement

Entity Manipulation Language (EML)

EML consists of statements that you can use to manipulate the data of a business object. You use the READ ENTITIES statement to read data; for all other operations, you use the MODIFY ENTITIES statement with the corresponding addition UPDATE, CREATE, or DELETE.

Note

You can only use the CREATE, UPDATE, and DELETE operations if the behavior definition of the business object interface contains the corresponding use create, use update, or use delete directive. Trying to use a prohibited operation causes a syntax error.

To read data from a business object, you use the READ ENTITIES statement. The statement has two important parameters: one internal table containing the keys of the data that you want to read and another containing the results of the query.

These internal tables have special data types called derived behavior definition types. The system creates them automatically when a developer creates a behavior definition and they contain some or all of the fields of the business object along with further fields that control how the system processes a particular request. You declare the internal tables using the new addition TYPE TABLE FOR <operation> in the DATA statement.

The type TABLE FOR READ IMPORT contains the key field or fields of the business object. The %control structure is a generated structure that indicates which fields of the business object are actually used in the current operation. In our example, the system fills the structure automatically and you do not have to worry about it.

The typeTABLE FOR READ RESULT contains all of the fields of the business object. This table contains the result set after the read statement has been executed.

The read import table contains a column for each key field of the business object, in this case, agencyID. To read a particular agency, you add a row to the internal table containing its key. As well as the key field or fields, the table contains the columns %is_draft and %control. With %is_draft you can specify whether you want to read draft data or active data. The %control structure is used to specify which fields are to be read.

Note

In our example, we only read active data and the %control structure is filled by the framework, based on the field list after addition FIELDS.

When you process a business object, using the READ ENTITIES OF or MODIFY ENTITIES OF statement, you must first specify the name of the behavior definition. This is followed by keyword ENTITY and the name of the entity with which you want to work. If the entity has an alias name, you should use it, here.

Note

You cannot use the alias name after addition OF. This is because technically speaking, you specify the name of the behavior definition at this point, not the name of the entity.

The READ ENTITIES statement reads business object data according to the keys that you pass in the WITH addition. It returns the result in the internal table in the RESULT addition. In the statement, you can also specify which fields of the business object you need. In this example, we have used the ALL FIELDS addition to return all of the fields. However, if you only require a subset of the fields, you can use the variant FIELDS ( field1field2 … ) to restrict the amount of data that is read.

Note

Unlike in a SELECT statement, the field list is not comma-separated.

The result table contains all of the fields of the business object, along with the control field %is_draft. If your READ ENTITIES statement contains the ALL FIELDS variant, the system provides the values of all of the fields. If you use the FIELDS ( f1fn ) variant, only the fields that you requested will be filled.

Note
Key fields are always filled, even if they are not specified in the FIELDS ( f1fn ) variant.

If you want to update data, you declare an internal table with TYPE TABLE FOR UPDATE. This contains all of the fields of the business object and also the %control structure. In our variant of the MODIFY ENTITIES statement, the system fills the %control structure automatically.

The MODIFY ENTITIES statement updates data in the transactional buffer. In the FIELDS addition, you specify which fields should be changed. In the WITH addition, you pass the internal table containing the data that you want to update.

When you use EML outside the business object, you must use the COMMIT ENTITIES statement to trigger the save sequence and persist the data in the database.

Note

Later in this course we will use EML inside the behavior implementation of the business object. Inside the behavior implementation it is neither necessary nor allowed to trigger the commit with COMMIT ENTITIES.

How to Implement an EML Statement

Modify Data Using EML

In this exercise, you use EML to perform the standard operation update for the root entity of the business object /DMO/R_AGENCYTP. To avoid later inconsistencies, you do not access the business object directly but via its released stable interface /DMO/_I_AGENCYTP.

Template:

  • none

Solution:

  • /LRN/CL_S4D400_BOS_EML (global Class)

Task 1: Analyze Current Data

Use the Data Preview tool to check the current name of the travel agency with ID "0700##", where ## stands for your group number.)

Steps

  1. Open the definition of the CDS view entity /DMO/I_AgencyTP.

    1. If you still have the data definition /DMO/I_AGENCYTP open from the previous exercise, switch to the corresponding tab. Otherwise, press Ctrl + Shift + A to open the ABAP development object.

  2. Open the Data Preview tool.

    1. Right-click anywhere in the source code to open the context menu.

    2. From the context menu, choose Open WithData Preview.

  3. Check the current name of the travel agency with ID "0700##", where ## stands for your group number.)

    1. Scroll down to the entry with the value "0700##" in the column AgencyID and take a note of the value in the column Name.

Task 2: Create a Global Class

In your own package, create a new ABAP class.

Steps

  1. Create a new ABAP class called ZCL_##_EML, where ## stands for your group number. Ensure that the class implements the interface IF_OO_ADT_CLASSRUN.

    1. Choose FileNewABAP Class.

    2. Enter the name of your package in the Package field. In the Name field, enter the name ZCL_##_EML, where ## is your group number. Enter a description.

    3. In the Interfaces group box, choose Add.

    4. Enter IF_OO_ADT_CLASSRUN. When the interface appears in the hit list, double-click it to add it to the class definition.

    5. Choose Next.

    6. Select your transport request and choose Finish.

Task 3: Update Data

Implement an EML statement to change the name of the travel agency with ID "0700##". ( ## stands for your group number.)

Steps

  1. In the method IF_OO_ADT_CLASSRUN~MAIN, declare an internal table with the correct derived type for an EML update of entity /DMO/I_AgencyTP (suggested name: agencies_upd).

    1. Add the following code:

      Code snippet
      
           DATA agencies_upd TYPE TABLE FOR UPDATE /DMO/I_AgencyTP.
      
      Expand
  2. Fill the internal table with a single line containing "07000##" as the value for the column AGENCYID (where ## stands for your group number) and any new value for the column NAME.

    1. Add some code similar to the highlighted code:

      Code snippet
      
         DATA agencies_upd TYPE TABLE FOR UPDATE /dmo/i_agencytp.
      
      Expand
  3. Implement an EML statement MODIFY ENTITIES for the business object interface /DMO/I_AgencyTP. Update the data of the root entity, ensuring that only the field NAME is changed.

    Hint
    Remember that the interface behavior defines alias name /DMO/Agency for the root entity /DMO/I_AgencyTP.
    1. At the end of the method, add the following code:

      Code snippet
      
           MODIFY ENTITIES OF /dmo/i_agencytp
              ENTITY /dmo/agency
              UPDATE FIELDS ( name )
                WITH agencies_upd.
      
      Expand
  4. In order to get some visible result from the application, write a text literal to the console using the method out->write.

    1. After the MODIFY ENTITIES statement, add the following code:

      Code snippet
      
           out->write( `Method execution finished!`  ).
      
      Expand
  5. Activate and test the class.

    1. Press Ctrl + F3 to activate the class.

    2. Press F9 to run the class.

  6. Check whether the new name has been written to the database.

    1. Return to the data preview for CDS view entity /DMO/I_AgencyTP.

    2. From the toolbar of the Data Preview tab, choose Refresh.

  7. Add the COMMIT ENTITIES statement to your code to commit the changes.

    1. Return to the method IF_OO_ADT_CLASSRUN~MAIN in your global class ZCL_##_EML.

    2. Add the highlighted code :

      Code snippet
      
      METHOD if_oo_adt_classrun~main.
      
          DATA agencies_upd TYPE TABLE FOR UPDATE /dmo/i_agencytp.
      
          agencies_upd = VALUE #( ( agencyid = '700050' name = 'Some fancy new name' ) ).
      
          MODIFY ENTITIES OF /dmo/i_agencytp
            ENTITY /dmo/agency
            UPDATE FIELDS ( name )
              WITH agencies_upd.
      
          out->write( `Method execution finished!`  ).
      
        ENDMETHOD.
      
      Expand
  8. Activate and test the class again. Confirm that the changes are now in the database.

    1. Press Ctrl + F3 to activate the class.

    2. Press F9 to run the class.

    3. Return to the data preview for the CDS view entity /DMO/I_AgencyTP.

    4. From the toolbar of the Data Preview tab, choose Refresh.

Log in to track your progress & complete quizzes