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:
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]
Figure-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]
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]
Figure-3
After creating the metadata extensions, we add annotations.[Figure-4]
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.
Figure-5
Figure-6
After the service is created, we must not forget to expose our Consumption CDS views.[Figure-7]
Figure-7
Now, we need to create a service binding. For this, we will use the service definition we previously created.[Figure-8]
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]
Figure-9
In the published service, you can view the entity sets and associations that you have added.[Figure-10]
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]
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]
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.
Figure-13
Now, we need to create a Behavior for our Consumption CDS.[Figure-14 & 15]
Figure-14
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]
Figure-16
Here, we select "Create" to create our class.[Figure-17]
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]
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]
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]
Figure-20
Then, by clicking the Quick Fix button, we ensure the class method for the action is defined.[Figure-21]
Figure-21
Next, we go to the ZC_*-named behavior and define the action there as well.[Figure-22]
Figure-22
Finally, to make the button visible on the screen, we go to the Metadata Extension and add the relevant line.[Figure-23]
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]
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]
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]
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]
Figure-27
I hope it has been a useful blog.
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 27 | |
| 24 | |
| 21 | |
| 20 | |
| 14 | |
| 13 | |
| 13 | |
| 12 | |
| 12 | |
| 11 |