cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP RESTful programming copy action

former_member14709
Contributor

Hi Experts,

I am building a RAP based SAP Fiori Elements application. I am on S/4 HANA 2020 on premise release, FPS02.

I have a requirement to create an action button called 'Create as Copy'. The user will select some existing entry and hit this Create as Copy button to create a copy of the instance. The copy should be created in edit mode, so that before getting saved, user has an option to change few of the copied attributes. I was able to achieve the copy functionality, however, the copied entry gets saved to the DB directly with a new primary key, and I am landed to object page for the newly created entry in display mode. So user has to now manually click Edit and do an update operation if he wants to same change values in the newly created entry. Is there any way we can open this object page in edit mode directly?

Thanks,

Aman Garg

Former Member
0 Kudos

Hey Aman,

have you a solution for this? Actually I have the same requirement

regards

Accepted Solutions (0)

Answers (3)

Answers (3)

marunsct
Discoverer
0 Kudos

HI Aman Garg,

I am not sure if you are looking for this resolution without draft functionality still providing it here for others refrence. Instead of Instance Factory action create Static Factory action with input parameter same as entity and when pressing the copy button you will get a Dialog of all the values of the selected entity there you can change the values. But there is a downside that the action button will work even of no record is selected and dialog will appear asking all the details and will be difficult to implement for deep structures.

As of now Instance action doesnt support parameters that is why it has to be impletemented on Static factory method.

Regards,

Arun

Ramjee_korada
Active Contributor
0 Kudos

Hi Aman,

Yes, There is possibility to create to Object in draft mode.

Once you create the object in Draft mode , Object page opens in edit mode with CREATE button where user needs to confirm.

To do this, you have to add below code while creating new records in the copy method.

%is_draft = '01'  " for draft records<br>

sample code in my example is.

  METHOD copyRequisition.

    DATA : prhead TYPE  TABLE FOR CREATE zrk_i_pur_req_h\\_PRHead.

    READ ENTITIES OF zrk_i_pur_req_h IN LOCAL MODE
         ENTITY _PRHead
         ALL FIELDS WITH CORRESPONDING #( keys )
         RESULT DATA(pr_head_read_result) FAILED failed.

    LOOP AT pr_head_read_result ASSIGNING FIELD-SYMBOL(<fs_head>).

        APPEND VALUE #( %cid = keys[ 1 ]-%cid
                        %is_draft = '01'
                        %data = CORRESPONDING #( <fs_head> EXCEPT objectId ) )
                        TO prhead ASSIGNING FIELD-SYMBOL(<fs_new_req>).

    TRY.
        cl_numberrange_runtime=>number_get(
          EXPORTING
*        ignore_buffer     =
            nr_range_nr       = '01'
            object            = 'ZRK_NR_PR'
            quantity          = 1
*        subobject         =
*        toyear            =
          IMPORTING
                number            = DATA(number_range_key)
                returncode        = DATA(number_range_return_code)
                returned_quantity = DATA(number_range_returned_quantity)
        ).
      CATCH cx_number_ranges.
        "handle exception
    ENDTRY.
        DATA(max_id) = number_range_key - number_range_returned_quantity.
        <fs_new_req>-ObjectId = |PR{ max_id }|.
        <fs_new_req>-Description = |Copy of { <fs_head>-ObjectId }|.
        <fs_new_req>-StatCode = 'OPEN'.

    ENDLOOP.

    MODIFY ENTITIES of zrk_i_pur_req_h IN LOCAL MODE
        ENTITY _PRHead
        CREATE FIELDS ( ObjectId Description StatCode )
        WITH prhead
        MAPPED DATA(mapped_create)
        FAILED DATA(failed_create)
        REPORTED DATA(reported_create).


   READ ENTITIES OF zrk_i_pur_req_h IN LOCAL MODE
        ENTITY _PRHead
        ALL FIELDS WITH CORRESPONDING #( mapped_create-_prhead )
        RESULT DATA(read_created_result).

    mapped-_prhead = mapped_create-_prhead.

  ENDMETHOD.<br>

Best wishes,

Ramjee Korada.

former_member14709
Contributor
0 Kudos

Hello Ramjee,

Many thanks for sharing the code and information. I will try to implement this. Can you also tell me if we can add '%is_draft' only in draft enabled applications? My application is not draft enabled, can I still use this arrtibute?

Thanks,

Aman Garg

Ramjee_korada
Active Contributor
0 Kudos

Hi Aman,

In my perspective, I dont think its possible in Non-Draft application as the data in Object page, should come either from actual table or draft table.

I will also wait for feedback from Experts.

Best wishes,

Ramjee Korada

Altarkawi
Participant

Hello koradaramjee789

I've tried your code, however there is a porblem within the append value statement "no compenent exists with the name %cid"

i have also tried following solution but it gives the same error for %is_draft

loop at input_result ASSIGNING FIELD-SYMBOL(<fs_read_result>).
    
    APPEND value #( %cid = <fs_read_result>-Stdpd %is_draft = '01' %data = CORRESPONDING #( <fs_read_result> EXCEPT Stdpd Uname ) ) to lv_mvgensd ASSIGNING FIELD-SYMBOL(<nfs_read_result>).
 

any sugetions are welcomed

Mohammed

Ramjee_korada
Active Contributor
0 Kudos

Hello Mohammed,

Have you declared the action as factory in behavior definition?

factory action copyTravel [1];

Best wishes,

Ramjee Korada

Altarkawi
Participant

koradaramjee789 Hi again,

with the factory action, the error within the %cid is gone however the %is_draft still having the same error.

Ramjee_korada
Active Contributor
0 Kudos

Hi ,

Do you need draft feature ? if yes, please check if draft is enabled in behavior definition.

If no, then ignore it.

managed;
with draft;

define behavior for /DMO/R_Travel_D alias Travel
implementation in class /DMO/BP_I_TRAVEL_D unique
persistent table /DMO/A_TRAVEL_D
draft table /DMO/D_TRAVEL_D
lock master 
total etag LastChangedAtmanaged;

Best wishes,

Ramjee Korada.

CarineTchoutouo
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Aman,

please have a look at the implementation of the action "Copy Travel" provided in the RAP developer guide on the SAP Help Portal: SAP S/4HANA 2021 | SAP BTP ABAP Environment (aka Steampunk). This action is provided as example in the SAP S/4HANA documentation as of edition 2021, but the implementation should work on SAP S/4HANA 2020 as well.

You can have a closer look at the implementation code and test the action on the Steampunk trial. The implementation is provided in sub-package /DMO/FLIGHT_MANAGED of the Flight Reference Scenario (/DMO/FLIGHT).

Hope this helps you further.

Kind regards,
Carine

former_member14709
Contributor
0 Kudos

Hello Carine,

Thank you for taking out time to share answer and links.

I have tried implementing the same way, and I am able to create a copy using action, however, we want to present the copied instance in editable mode to the end user. But with this soultion, copy is created and user is landed to object page of newly created item in display mode. Is there any way we can open newly created item in edit mode? Or can we have possibility to edit items even before copying?

Thanks,

Aman Garg

mopalani
Explorer
0 Kudos

Hello Carine ,

Thank you for your contribution.

I've tried to implement factory action for one of our RAP (Managed implementation type with draft) based SAP Fiori elements with SAP S/4HANA 2020 FPS02, however, got an error that "Instance Factory Actions are not yet supported".

Please could you confirm that the factory action implementation work for SAP S/4HANA 2020 FPS02?

Thank you.

Warm Regards,

Mohandoss Palanisamy

CarineTchoutouo
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Mohandoss,


Factory Actions are supported for both implementation types, managed and unmanaged, on SAP BTP ABAP Environment.
They are currently only supported for the unmanaged implementation type on SAP S/4HANA on-premise. (restriction in RAP Dev Guide@SAP S/4HANA)

Kind regards,
Carine