How to Copy Records Using an Action Button in RAP

 

How to Copy Records Using an Action Button in RAP

Replicating data efficiently is a crucial feature in application development. In SAP RAP (Restful Application Programming), we can achieve this using an Action Button. This guide walks you through the steps required to implement a Copy Details button for duplicating records.

Step 1: Define the Action in the Behavior Definition

Begin by specifying the action in the Behavior Definition of the root entity. Use a marked line along with the action keyword and assign a relevant name to the button, such as 'Copydetails'.




Why Use a Factory Action?

Factory actions are useful in scenarios where:

  1. Data Initialization – When creating records with default values.
  2. Controlled Data Creation – When business logic needs to be enforced during the creation process.
  3. Batch Processing – When multiple records need to be created at once.
  4. Enhancing User Experience – Simplifies user input by automatically setting required fields.

Factory Action in Copy Records Implementation

In our Copy Details action, the factory action can help:

  • Generate new student records based on existing ones.
  • Ensure ID fields are excluded and unique identifiers are generated.
  • Apply custom business rules before persisting new records.

Step 2: Link the Action Button in the Projection Entity

In the Behavior Definition of the projection entity, define the same action button and ensure the name matches 'Copydetails'. Use the USE keyword to reference it correctly.



Note: The behavior definition of the root interface entity and projection entity are interdependent. You can verify their origin in the Definition section.

Step 3: Read and Prepare New Records

Now, let’s retrieve student records from zi_rap_stud_02 and prepare new entries for creation:

  • Declare an internal table to store student details.
  • Fetch student data in local mode, capturing successful and failed results.
  • Loop through the retrieved data to construct new records, excluding the ID field.
  • Append the new records to a table for creation.
  • Modify the zi_rap_stud_02 entity to create the records.
  • Capture the mapped, reported, and failed data in corresponding tables.
  • Retrieve and store successfully copied student records.


Step 4: Configure the Action Button in the Metadata Extension

To ensure the action button appears correctly in the UI, establish its relation with the behavior definition in the Metadata Extension file.

Use the appropriate annotation just before the field where the action button should be displayed.



Important: The button name must be consistent across both the Metadata and Behavior Definition.

Step 5: Verify the Functionality

Once all configurations are complete, you should now see the Copy Details action button available for executing the record duplication process.



Final Thoughts

With these steps, you’ve successfully created a Copy Action Button in SAP RAP. This approach not only simplifies data management but also enhances user experience by streamlining record replication.