How to Create SM30 Like Table Maintenance T-Code in SAP RAP – Part 2 – Validations and Action via RAP

This is the continuation of my previous blog post to replicate TMG like FIORI application using ABAP RAP. In the […]

How to Create SM30 Like Table Maintenance T-Code in SAP RAP – Part 2 – Validations and Action via RAP

This is the continuation of my previous blog post to replicate TMG like FIORI application using ABAP RAP. In the previous post, we have created a RAP Based service where we were able to create, delete and edit (Multi inline). In this blog, we will add a few validations and a new action for copying the existing records.

In our RAP based application, Following functionalities will be added:

  • Validation for mandatory fields and duplicate records
  • Copy an existing record

Application in ADT Preview mode will be look like as follow:

Error display when duplicate order is entered

Once we select some existing records and click on the Copy As button, selected records in the table will be copied.

Once we select some existing records and click on the Copy As button, selected records in the table will be copied.

Before we proceed with our tutorial, we would like to give you an opportunity to join our ZAPYard’s learning community where we have more than 32 groups and more than 1000 real SAP Consultants interacting with each other daily. Only SAP topics and not BS. Else, they will be banned from the community without warning. ????????????????

If you want to be part of ZAPYard’s Discussion Community, please feel free to check the below Link. We Ask, Answer, Help and Learn together. There are more than 32 groups from different topics like RAP, BPT, Fiori, iRPA, CAI, CPI, PI/PO, ABAP on HANA, SAPUI5, SAP Build, SAP Adobe Forms, ChatBots, SAC etc. Join any group of your interest and interact with our Community.

Join ZAPYard’s WhatsApp Community

Use Case 1 : Duplicate Order No.

While creating records, Users can provide redundant order no as well. Application should not allow users to save records with duplicate order no. To handle this, we can add a validation in our RAP Business Object for Order node.

    1. New Validation in BDEF:

  • We have created validation validateDuplicateID within child node Order for Behavior Definition ZPBDEMO_I_SINGLETON.
  • Trigger time for validations can only be set as save i.e. when we will try to save the entities, validations will be triggered (typically during CheckBeforeSave in save sequence phase)
  • Trigger conditions have been provided using trigger operations create, update and trigger field OrderNo .
  • Validation is defined within a new group OrderValidations which has its own BIL class. It will help our implementation to be modularized properly i.e. all validations will be implemented in a separate implementation class.

    2. Add Validation in BDEF as Determine Action:

  • For Draft enabled applications, It’s important to include validations within draft determine action Prepare,
  • Draft determine action has to be defined in the root node where all other draft actions are defined.
  • We can also define validations of child entities using node name as:

Order ~ ValidateName

    3. Implementation of Validation in Behavior Implementation:

    3.1. Method Definition:

  • Use a quick assistant in BDEF for validation implementation.
  • Method with validation name ValidateDuplicateID will be defined with importing parameter keys which contains transactional key data for input BO instances.
  • Since trigger operation for this validation is defined as Create and Update, all the BO instances which are newly created or updated , will be provided by keys structure.
  • Keys component consists of %tky which will have draft indicator and key fields of Order BO instance.

    3.2. Method Implementation:

With this validation, we need to check if there is any duplicate order no. entered by end users in any of the records. If found, an error message has to be raised and the impacted cell should be highlighted. Following steps have to be implemented in this method:

  • Using the keys parameter, we need to get the root data. There is an association to root entity from Order entity. We will use the READ EML statement to get the root node data.
  • Now, from this root table, we need to get all the draft child entities(existing + newly entered records). There is an association to the child entity in the root node. We will use this association in another READ EML statement to get all the Child instances from the root node:
  • We need to prepare a temp table with Order no as the first column so that we can sort and find if there are any duplicate records with the same order no.
  • When duplicate Order No is found, we need to fill FAILED structure with the transactional key(draft indicator + keys) of the corresponding instance.
  • We also need to raise the error message in the UI application. For that, we need to fill REPORTED structure with following information:

%tky                –           Transactional key of the failed BO instance

%state_area    –           Unique state area(any string uniquely define the validation)

%msg              –           contains reference from RAP message wrapper

%path              –           required for child entities and pass transactional key of root

%element        –           field name to be highlighted.

Complete code for finding duplicate order and raising error message:

Use Case 2 : Copy Order

For quick data entry, Copy functionality can be used where user can select some existing records from the Order table and copy those to create new entries. It should only be possible in Edit mode. For that, a new custom action can be created in the RAP BO Order node.

1. New Action in BDEF:

  • New factory action copy has been created in RAP BDEF  ZPBDEMO_I_SINGLETON within node Order.
  • Factory actions in RAP are used for creating new BO instances. It doesn’t require any output parameter. It will automatically take the output structure(MAPPED) as of the entity in which it is declared. In our case, output for copy action will be of type entity Order.
  • We have added Feature instance so that this action is only enabled for draft instances (Edit mode).
  • Action is defined within a new group Actions which has its own BIL class. It will help our implementation to be modularized properly i.e. all actions will be implemented in a separate implementation class.

2. Action Implementation:

2.1. Method Definition:

There will be different methods defined for our action:

  1. Feature Control method
  2. Instance Authorization method
  3. Business Logic for Copy Action method
  • Use a quick assistant in BDEF for action implementation.
  • Importing Parameter Keys in all the methods will contain transactional key data for input BO instances.
  • By using feature control for action copy, there are additional methods defined by the RAP framework for handling field control values for copy action. We can dynamically enable/disable the action.
  • Feature control method will have resulting parameter RESULT which should be filled with transactional key of the instance and features for operation control parameter ( enabled/disabled)
  • If there are any authorization based on selected BO instances, we can also implement it in the method get_instance_authorization.
  • Keys component consists of %tky which will have draft indicator and key fields of Order BO instance.

2.2. Method Implementation:

  • Feature Control

We need to check if the current BO instance is in draft mode. Copy action should only be enabled when BO instance is in draft(edit mode). For active instances, copy action should be disabled.

  • We will use the READ EML statement to get the order node data using the keys structure.
  • When the root node is in edit mode, draft instances will be shown and all the items(Orders) will also be available in draft mode only. We will prepare output table RESULT by assigning the feature control parameter for copy action as enabled only for draft instances.
  • Copy

In this implementation, we need to create new draft instances for order node by copying the data from selected nodes from keys structure.

  • We will read complete node data for the selected record using keys structure
  • Declare internal table as Order Entity for create operation.

  • RAP offers special data declaration for EML.Since we are creating new instances for Order node, we need a table type for CREATE operation.
  • Also, in our BDEF, we have not defined direct CREATE operation for Child entity(Order) but It can be created using association from root node.
  • We need to prepare Order data in the internal table lt_order_cba. Following components have to be filled:

%is_draft                     –           should be marked as on

OrderSingletonID        –           key data for root entity(in our case, Its 1 always)

%target                        –           table with Item Data.

%target Component:

%is_draft                     –           should be marked as on

%cid                            –           content id(unique string for each record)

data for Order record

  • We need to update BO Node with this new Order records prepared in table lt_order_cba. We need to use the MODIFY EML statement for create operation.

Complete code for Copy action can be referred as:

3. Use the Action in Projection Behavior Definition:

For action to be exposed in UI service, we need to expose this action in the projection BO as:

4. MetaData Extension in Projection CDS:

For action to be shown in the action toolbar of the table Order, we need to add UI annotation @UI.LineItem

With these steps, we will be able to add a fully functional Copy As feature similar to what we have used in SAP GUI transaction SM30.

In this blogpost, we have learnt:

  1. How to add and implement validations in a RAP BO?
  2. How to add a custom action ?
  3. How to handle dynamic feature control for actions?

How to use EML in RAP based programming logic? We have see by 2 examples each for READ and MODIFY EML.

In the next blog, we will consume this OData service in a List report template based FIORI application in Business Application Studio and also, learn some cool features in UI using SAP FIORI tools.

Please follow our LinkedIn PageLinkedIn Group , Facebook PageFacebook GroupTwitter , Instagram and Telegram SAP Technical Group Signal Group

Do not forget to SUBSCRIBE to our YouTube Channel for Free Courses and Unconventional Interesting Videos.

Do join ZAPYard’s Learning Community.