SAP / styleguides

This repository provides SAP style guides for coding and coding-related topics.
Other
1.67k stars 444 forks source link

Clean ABAP for EML #220

Open uenal-akkaya opened 3 years ago

uenal-akkaya commented 3 years ago

With the ABAP RESTful Application Programming Model (RAP) were the ABAP language enriched by Entity Manipulation Language (EML) statements to access and interact with Business Objects (BO).

Here are some EML statements from the SAP Help portal: EML Read

READ ENTITIES OF /DMO/I_Travel_D
  IN LOCAL MODE
  ENTITY Travel
    FIELDS ( BeginDate EndDate TravelID )
    WITH CORRESPONDING #( keys )
RESULT DATA(lt_travel)
FAILED DATA(failed)
REPORTED DATA(reported).

Create

*Declare internal table using derived type
    DATA create TYPE TABLE FOR CREATE /DMO/I_Travel_D.

*Select valid flight data
    SELECT SINGLE AirlineID, ConnectionID, FlightDate FROM /DMO/I_Flight INTO @DATA(flight).

*Populate internal table for travel instance
    create = VALUE #( ( %cid                 = 'create_travel'
                        %is_draft            = if_abap_behv=>mk-off
                        CustomerID           = '1'
                        %control-CustomerID  = if_abap_behv=>mk-on
                        AgencyID             = '70006'
                        %control-AgencyID    = if_abap_behv=>mk-on
                        BeginDate            = flight-FlightDate
                        %control-BeginDate   = if_abap_behv=>mk-on
                        EndDate              = flight-FlightDate
                        %control-EndDate     = if_abap_behv=>mk-on
                        Description          = 'Travel 1'
                        %control-Description = if_abap_behv=>mk-on
 ) ).

*Create a travel instance and two associated booking instances
    MODIFY ENTITIES OF /DMO/I_Travel_D
        ENTITY Travel
          CREATE FROM create
          CREATE BY \_Booking
          FIELDS ( CustomerID AirlineID ConnectionID FlightDate ) WITH VALUE #( ( %cid_ref = 'create_travel'
                                                                                  %target = VALUE #( ( %cid         = 'create_booking_1'
                                                                                                       %is_draft    = if_abap_behv=>mk-off
                                                                                                       CustomerID   = '1'
                                                                                                       AirlineID    = flight-AirlineID
                                                                                                       ConnectionID = flight-ConnectionID
                                                                                                       FlightDate   = flight-FlightDate )
                                                                                                     ( %cid         = 'create_booking_2'
                                                                                                       %is_draft    = if_abap_behv=>mk-off
                                                                                                       CustomerID   = '1'
                                                                                                       AirlineID    = flight-AirlineID
                                                                                                       ConnectionID = flight-ConnectionID
                                                                                                       FlightDate   = flight-FlightDate )
                                                                                                     ) ) )

    MAPPED DATA(mapped)
    REPORTED DATA(reported)
    FAILED DATA(failed).

*Persist transactional buffer
    COMMIT ENTITIES.

There are different ways to write EML:

MODIFY ENTITES OF /DMO/I_TRAVEL
 ENTITY Booking
UPDATE FIELDS ( field1 field2 )
        WITH VALUE #( ( keyField1 = 'A'
                                     keyField2 = 'B'
                                     otherField1 = 12
                                     otherField2 = 24
                      ) )
   FAILED DATA(failed)
   MAPPED DATA(mapped)
   REPORTED DATA(reported).    
MODIFY ENTITES OF /DMO/I_TRAVEL
 ENTITY Booking
UPDATE FIELDS ( field1 field2 )
        WITH VALUE #( ( %key = VALUE #( keyField1 = 'A'
                                                                keyField2 = 'B' )
                                    %data = VALUE #( otherField1 = 12
                                                                  otherField2 = 24 )
                      ) )
   FAILED DATA(failed)
   MAPPED DATA(mapped)
   REPORTED DATA(reported).    
MODIFY ENTITES OF /DMO/I_TRAVEL
 ENTITY Booking
UPDATE FROM VALUE #( ( keyField1    = 'A'
                                           keyField2    = 'B'
                                           otherField1 = 12
                                           otherField2 = 24
                                          %control = VALUE #( keyField1    = if_abap_behv=>mk-on
                                                                             keyField2    = if_abap_behv=>mk-on
                                                                             otherField1 = if_abap_behv=>mk-on
                                                                             otherField2 = if_abap_behv=>mk-on
                                                                              )
                      ) )
   FAILED DATA(failed)
   MAPPED DATA(mapped)
   REPORTED DATA(reported).    
MODIFY ENTITES OF /DMO/I_TRAVEL
 ENTITY Booking
UPDATE SET FIELDS WITH VALUE #( ( keyField1    = 'A'
                                                            keyField2    = 'B'
                                                            otherField1 = 12
                                                            otherField2 = 24
                      ) )
   FAILED DATA(failed)
   MAPPED DATA(mapped)
   REPORTED DATA(reported).   
ruthiel commented 3 years ago

First time viewing this kind of language. Thank you for share!