Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
ertugrul_bagci
Explorer
14,370

If you've started exploring SAP RAP, you may have noticed that classes like CL_BCS, which are typically used for sending emails in SAP, cannot be directly used in RAP. The main reason is that RAP does not allow the use of "COMMIT" which is required by these classes.

In this blog, I aim to address this limitation while also exploring how to create a custom action and build a simple RAP project.

The blog will be structured around the following key topics:

  1. Creation of Interface and Consumption CDS Views
  2. Creation of Metadata
  3. Creation of the Service
  4. Creation of Behavior
  5. Creation of Class and Action
  6. Email Sending Process

To ensure clarity, we will proceed with an example scenario throughout the blog. Our scenario involves listing invoices and some details from the VBAK table, and sending the selected data via email as part of the email sending process.

Creation of Interface and Consumption CDS Views

Interface CDS is used to abstract raw data into a reusable and meaningful structure, serving as the foundational data model. Consumption CDS, on the other hand, processes this data to make it user-friendly and tailored for specific use cases, such as reporting, Fiori UI, or analytical solutions.

Even though we might not need it for this project, it is generally good practice to follow this structure. Let's start by creating the Interface CDS view.

For the sake of example, I have added a few fields. You can expand it according to your scenario. Additionally, ensure that the entity is marked as "root," as this will be our top-level entity. [Figure-1]

ertugrul_bagci_0-1733693647522.pngFigure-1

 During the creation of the Consumption CDS, we used the "Audat" filter to narrow down our dataset and also to leverage the Consumption logic. [Figure-2]

ertugrul_bagci_1-1733694057130.png

Figure-2

Creation of Metadata

In SAP RAP (Restful ABAP Programming Model), metadata extensions are used to enhance the metadata of existing CDS views or entities without modifying the original CDS definition. This is especially useful for tailoring application behavior or UI representations in a decoupled and non-intrusive manner.

We create metadata from our Consumption CDS.[Figure-3]

ertugrul_bagci_1-1733694550358.png

Figure-3

After creating the metadata extensions, we add annotations.[Figure-4] 

ertugrul_bagci_2-1733694616782.png

Figure-4

Creation of the Service

Now, we create a service definition from our Consumption CDS.
You can follow Figures 5 and 6 for reference.

ertugrul_bagci_4-1733694729771.png

Figure-5

ertugrul_bagci_5-1733694749408.png

Figure-6

After the service is created, we must not forget to expose our Consumption CDS views.[Figure-7]

ertugrul_bagci_7-1733694864062.png

Figure-7

Now, we need to create a service binding. For this, we will use the service definition we previously created.[Figure-8]

ertugrul_bagci_8-1733694951607.png

Figure-8

Make sure to select "OData V2 - UI" as the Binding Type.
When the service is first created, it will not be published. To publish it, click the button I have highlighted.[Figure-9]

ertugrul_bagci_9-1733695053234.png

Figure-9

In the published service, you can view the entity sets and associations that you have added.[Figure-10]

ertugrul_bagci_0-1733695202499.png

Figure-10


If you want to test the service, simply double-click on the entity. This allows you to test your service easily.[Figure-11
]

ertugrul_bagci_1-1733695208679.png

Figure-11

Creation of Behavior

Behavior definitions for the business object entities in a given composition model are established for the root CDS view, outlining the behavior for all included entities.

Therefore, we create Behavior definitions for both the Interface and Consumption CDS views.

Let's start with the Interface CDS.[Figure-12]

ertugrul_bagci_2-1733695392048.png

Figure-12

After creating the Behavior, we add an alias.
Since we will not perform create, update, or delete operations, we can disable these actions. [Figure-13]

The class "zbp_i_vbak_example" will be created in later steps.

 

ertugrul_bagci_3-1733695462434.png

Figure-13

 

Now, we need to create a Behavior for our Consumption CDS.[Figure-14 & 15]

ertugrul_bagci_4-1733695539600.png

Figure-14

ertugrul_bagci_6-1733695561696.png

Figure-15

We have successfully created the Behavior within the Consumption CDS. Now, we can move on to the next step.

Creation of Class and Action

To make the action we are going to create functional, we need a class.
To create the class, go to the Behavior, right-click, and select "Quick Fix".[Figure-16]

ertugrul_bagci_7-1733695765262.png

Figure-16

Here, we select "Create" to create our class.[Figure-17]

ertugrul_bagci_8-1733695860621.png

Figure-17

 


The class creation process is now complete.
Make sure to note that all the code we have written is under "Local Types".[Figure-18]

 ertugrul_bagci_9-1733695892829.png

Figure-18

To create an action, we first need an abstract CDS. This CDS will define the input parameters for the action.
For this, we create a new Data Definition.
 Here, we will define the input parameter for the email address.[Figure-19]

ertugrul_bagci_10-1733695981340.png

Figure-19

Next, we go to the Interface ZI_** named behavior and define our action. We will provide the Abstract CDS as the parameter. [Figure-20]

ertugrul_bagci_13-1733696187612.png

Figure-20

Then, by clicking the Quick Fix button, we ensure the class method for the action is defined.[Figure-21]

ertugrul_bagci_14-1733696244429.png

Figure-21

Next, we go to the ZC_*-named behavior and define the action there as well.[Figure-22]

ertugrul_bagci_15-1733696355871.png

Figure-22

Finally, to make the button visible on the screen, we go to the Metadata Extension and add the relevant line.[Figure-23]

ertugrul_bagci_16-1733696408857.png 

Figure-23

 

 

 

 

: { lineItem: [ 
      { type: #FOR_ACTION, dataAction: 'SendMail', label: 'Send Mail' } ]} 

 

 

 

 

Once we execute the service again, you will be able to see the button added.[Figure-24]

 ertugrul_bagci_17-1733696481346.png

Figure-24

Email Sending Process

As we mentioned earlier, we cannot use classes like CL_BCS that include a "commit" in RAP for email sending. Therefore, we will use the CL_BCS_MAIL_MESSAGE class to send emails.

For this, we go to the class where we defined the Send_mail method.
The selected row and email information will be contained in the "keys" variable. We will read the table and send the email. I have not included many details in the email content, but you can add more as needed.[Figure-25]

ertugrul_bagci_18-1733696619664.png

Figure-25

 

 

 

 

 METHOD SendMail.
    DATA:lv_mail TYPE bcs_msg_sender.
    DATA(ls_keys) = VALUE #( keys[ 1 ] OPTIONAL ).
    lv_mail = ls_keys-%param-Mail.
    DATA(mail) = cl_bcs_mail_message=>create_instance( ).
    mail->set_sender( 'Sender@mail.com' ).
    mail->add_recipient( lv_mail ).


    mail->set_subject( 'Test' ).

    mail->set_main( cl_bcs_mail_textpart=>create_instance(
        iv_content      = '<h2>Hi</h2><p>Hi you can see the details of invoice number ' && ls_keys-Vbeln &&' below. </p>'
        iv_content_type = 'text/html' ) ).

    mail->send( IMPORTING et_status = DATA(lt_status) ).

  ENDMETHOD.

 

 

 

 

 

Now, we run a preview of the service. We select an invoice, click the Send Mail button, and enter the email address.[Figure-26]

ertugrul_bagci_19-1733696746091.png

Figure-26

To check the email, we will use the "SOST" transaction code.
As shown in the figure, the email has been successfully sent.[Figure-27]

ertugrul_bagci_20-1733696789414.png

Figure-27

I hope it has been a useful blog.
Regards.

5 Comments