In this beginner blog post we are going to see how we can create Factory Actions in ABAP Restful Application Programming Model.

 

Ref: https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenbdl_action_nonfactory.htm

 

 

NOTE:

We are using same application code base which we have used to demo Large Object (LOB) or File Upload Blog. Refer below URL:

https://blogs.sap.com/2023/02/13/abap-restful-application-programming-model-rap-abap-rap-file-upload-large-objects-lob/

 

Before starting on Factory Actions lets understand what are Actions in ABAP RAP :

Actions:

An Action can be custom operation which is used to make changes to data of BO Instance.  RAP actions are non-standard operations which are used to modify the entity instance.

The Action is defined in Behavior Definition and must be Implemented in custom logic must be implemented in the Behavior Implementation Class.

 

Actions can be categorized in two main types :

 

Non-factory actions

For using non Factory Actions custom logic needs to be Implemented and used to changes the state of the instance.

 

Factory Action:

Factory actions are used to create RAP BO entity instances.

    1. Instance-Bound Actions : an instance-bound factory action used to copy one or more BO instances and creates new instances based on the copied data.
    2. Static Actions : Static factory actions can be used to create instances with default values.
    3. Factory Actions does not have any return parameter.

 

Adding Factory Action to Behavior Definition

 

managed implementation in class zbp_student_hdr_tab_i unique;
strict ( 1 ); with draft;
define behavior for zstudent_hdr_tab_I alias Student
persistent table zstudent_hdr_tab
draft table zstudent_h_d_tab
lock master
total etag Locallastchangedat
authorization master ( global )
etag master Lastchangedat
{
  create;
  update;
  delete;
  association _Attachments { create; with draft; }
  field ( numbering : managed, readonly ) Id;
  draft action Edit;
  draft action Activate;
  draft action Discard;
  draft action Resume;
  draft determine action Prepare;

  //ADDING FACTORY ACTION FOR COPY
  factory action copyStudent[1];

  mapping for zstudent_hdr_tab
  {
    Id = id;
    Firstname = firstname;
    Lastname = lastname;
    Age = age;
    Course = course;
    Courseduration = courseduration;
    Dob = dob;
    Gender = gender;
    Lastchangedat = lastchangedat;
    Locallastchangedat = locallastchangedat;
    Status = status;
  }
}

define behavior for zstudent_att_tab_i alias Attachments
persistent table zstudent_att_tab
draft table zstudent_a_d_tab
lock dependent by _Student
authorization dependent by _Student
etag master LastChangedat
{
  update;
  delete;
  field ( readonly ) Id;
  association _Student { with draft; }
  mapping for zstudent_att_tab{
    AttachId    = attach_id;
    Attachment  = attachment;
    Comments    = comments;
    Filename    = filename;
    Id          = id;
    Mimetype    = mimetype;
  }
}

 

Create Method Implementation in Behavior Implementation Class

 

 

Method Implementation is created

 

 

 

Refer full copyStudent method code

 

  1. Created Internal table of Interface View type for Entity. This Internal table will store record selected from frontend.
  2. Reading Entities if selected Key values from Frontend.
  3. Append selected values to Internal table for further use.
  4. Finally use Modify and set new record in MAPPED.

 

CLASS lhc_Student DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.

    METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION
      IMPORTING REQUEST requested_authorizations FOR Student RESULT result.

    METHODS copyStudent FOR MODIFY
      IMPORTING keys FOR ACTION Student~copyStudent.



ENDCLASS.

CLASS lhc_Student IMPLEMENTATION.

  METHOD get_global_authorizations.
  ENDMETHOD.


  METHOD copyStudent.

    DATA: lt_student TYPE TABLE FOR CREATE zstudent_hdr_tab_I.

    " Reading selected data from frontend

    READ ENTITIES OF zstudent_hdr_tab_I IN LOCAL MODE
    ENTITY Student
    ALL FIELDS WITH CORRESPONDING #( keys )
    RESULT DATA(students)
    FAILED failed.

    LOOP AT students ASSIGNING FIELD-SYMBOL(<lfs_students>).

      APPEND VALUE #( %cid = keys[ KEY entity %key = <lfs_students>-%key ]-%cid
                      %is_draft = keys[ KEY entity %key = <lfs_students>-%key ]-%param-%is_draft
                      %data = CORRESPONDING #( <lfs_students> EXCEPT id )

       )  TO lt_student ASSIGNING FIELD-SYMBOL(<lfs_newStudent>).

    ENDLOOP.

    "Create BO Instance by COpy

    MODIFY ENTITIES OF zstudent_hdr_tab_I IN LOCAL MODE
    ENTITY Student
    CREATE FIELDS ( Firstname Lastname Age Course Courseduration Status Gender Dob )
    WITH lt_student
    MAPPED DATA(mapped_create).

    mapped-student = mapped_create-student.

  ENDMETHOD.

ENDCLASS.

 

Add Copy Button to Fiori List report Application

 

 

All done…

 

Validating Changes:

 

Lets validate application Copy Action

Copy Student Button is available, refer below screen shot.

 

Select one record to Copy and click on Copy Student Button.

 

Since we are using Draft capability in this application and we have mentioned %is_draft and set its value dynamically (00 or 01)

Refer %is_draft line in below code…

 

APPEND VALUE #( %cid = keys[ KEY entity %key = <lfs_students>-%key ]-%cid
                      %is_draft = keys[ KEY entity %key = <lfs_students>-%key ]-%param-%is_draft
                      %data = CORRESPONDING #( <lfs_students> EXCEPT id )

       )  TO lt_student ASSIGNING FIELD-SYMBOL(<lfs_newStudent>).

 

When User click on Copy Student button new record will be created in Draft mode (Edit Mode), OR new draft entry will be created. Controls will be open to Edit the values in Object Page.

 

 

When Application is not using Draft functionality then Copied record will directly saved in Persistent table, record will not go to Draft table.

Refer below code, commented %is_draft

 

APPEND VALUE #( %cid = keys[ KEY entity %key = <lfs_students>-%key ]-%cid
                      "%is_draft = keys[ KEY entity %key = <lfs_students>-%key ]-%param-%is_draft
                      %data = CORRESPONDING #( <lfs_students> EXCEPT id )

       )  TO lt_student ASSIGNING FIELD-SYMBOL(<lfs_newStudent>).

 

Output looks like below:

 

 

Go back to List Page and new copied record is available.

 

Previous

ABAP RAP – File Upload Large Objects (LOB)

ABAP RAP – #ISOLATED

ABAP RAP – #CHANGE_SET

 

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x