cancel
Showing results for 
Search instead for 
Did you mean: 

Actions in Behavior Definition For CDS Custom Entity

aatan
Explorer
0 Kudos
565

Hi everyone,

The scenario is;  I have created a RAP application that will display data from Web Service. CDS Custom entity was created and the query implementation class called Web service.

The next step is to save the data from my Fiori Report List into my custom Z* table and update the "Status" field on the Fiori List.

Why does the Read Entity result fail? 

What should be the way?

aatan_2-1745164448614.png

aatan_1-1745163997420.png

Kind regards, aatan.

 

 

 

 

 

 

Accepted Solutions (0)

Answers (3)

Answers (3)

MBartsch71
Participant

As a custom entity does not have a database in the background to store the information the READ ENTITIES statement will not work. You have to determine the data to be updated and stored again and implement all by yourself. I suggest to modularize the read functions for the custom entity so that you can reuse the logic in the action implementation of the behavior.

MBartsch71
Participant
Keys contains the key values from your custom entity.
aatan
Explorer
0 Kudos

Hello,

In the SetExchange(for Action) method, the Keys values are coming through successfully.

What I want to achieve is to update the icons on the screen based on these key values and refresh the UI accordingly.

Additionally, after calling the SetExchange( is empty code ) method, the if_rap_query_provider~select method is triggered.

In this call, the low value from io_request->get_filter()->get_as_ranges() is empty.

I couldn't find a clear example addressing this specific scenario.

Kind regards, aatan.

suzanne_alivand
Explorer
0 Kudos

what I think is that you use READ ENTITIES OF zc_exchange_bank IN LOCAL MODE, you're attempting to read from the RAP transactional buffer. However for custom entities that get data from external sources this buffer is initially empty. The data you see in your Fiori UI comes directly from your query implementation class but those instances never get automatically stored in the transactional buffer unless explicit CUD operations are performed on them.
I think you need to bypass the buffer reading attempt and work directly with the key values that are already provided to your method. use these keys (ForexType, SapDate, CurrencyCode) directly to perform your Z-table insert, then populate the RESULT parameter to update the UI with new status values.

METHOD SetExchange.
  
  " Data structures for Z-table 
  DATA: ztable_data TYPE TABLE OF ztable_exchange_log,
        failed      TYPE RESPONSE FOR FAILED zc_exchange_bank,
        reported    TYPE RESPONSE FOR REPORTED zc_exchange_bank.
  
  " Key structure matching entity primary keys
  TYPES: BEGIN OF ts_entity_key,
           ForexType    TYPE c LENGTH 1,
           SapDate      TYPE d,
           CurrencyCode TYPE c LENGTH 5,
         END OF ts_entity_key.
  
  " Tables for fetching process
  DATA: keys_for_fetch TYPE TABLE OF ts_entity_key,
        full_data      TYPE TABLE OF zc_exchange_bank_type.
  
  " Extract keys from selected entries
  LOOP AT keys ASSIGNING FIELD-SYMBOL(<key>).
    APPEND VALUE #( 
      ForexType    = <key>-%key-ForexType
      SapDate      = <key>-%key-SapDate
      CurrencyCode = <key>-%key-CurrencyCode
    ) TO keys_for_fetch.
  ENDLOOP.
  
  " Fetch complete data from web service via query implementation
  IF keys_for_fetch IS NOT INITIAL.
    TRY.
        DATA(query_impl) = cl_abap_get_instance=>get_instance( 'ZCL_RAP_EXCHANGE' ).
        
        CALL METHOD query_impl->('GET_DATA_BY_KEYS')
          EXPORTING
            it_keys    = keys_for_fetch
          RECEIVING
            rt_result  = full_data.
            
      CATCH cx_root INTO DATA(error).
        " Handle fetch errors
        LOOP AT keys ASSIGNING <key>.
          INSERT VALUE #( %tky = <key>-%tky ) INTO TABLE failed-exchange.
          INSERT VALUE #( %tky = <key>-%tky
                          %msg = NEW zcx_rap_error( 
                                   severity = if_abap_behv_message=>severity-error
                                   textid   = 'FETCH_ERROR'
                                   text     = '' ) 
                        ) INTO TABLE reported-exchange.
        ENDLOOP.
        failed   = failed.
        reported = reported.
        RETURN.
    ENDTRY.
  ENDIF.
  
  " Process data and prepare Z-table records and UI updates
  LOOP AT keys ASSIGNING <key>.
    READ TABLE full_data ASSIGNING FIELD-SYMBOL(<entity>) 
      WITH KEY ForexType    = <key>-%key-ForexType
               SapDate      = <key>-%key-SapDate
               CurrencyCode = <key>-%key-CurrencyCode.
               
    IF sy-subrc = 0.
      " Prepare Z-table entry with key and non-key fields
      APPEND VALUE #(
        forex_type    = <key>-%key-ForexType
        sap_date      = <key>-%key-SapDate
        currency_code = <key>-%key-CurrencyCode
        isim          = <entity>-Isim          
        sapdate_ui    = <entity>-SapDate_UI    
        processed_on  = sy-datum
        processed_by  = sy-uname
        status        = 'COMPLETED'
      ) TO ztable_data.
      
      " Prepare UI update data
      APPEND VALUE #(
        %tky  = <key>-%tky
        %data = VALUE #( 
                  StatusCode = '1'                 
                  StatusIcon = 'sap-icon://accept'  
                )
      ) TO result.
    ELSE
      " Handle missing data
      INSERT VALUE #( %tky = <key>-%tky ) INTO TABLE failed-exchange.
      INSERT VALUE #( %tky = <key>-%tky
                      %msg = NEW zcx_rap_error( 
                               severity = if_abap_behv_message=>severity-error
                               textid   = 'NOT_FOUND' 
                               text     = 'Entity data not found' )
                    ) INTO TABLE reported-exchange.
    ENDIF.
  ENDLOOP.
  
  " Save data to Z-table
  IF ztable_data IS NOT INITIAL.
    TRY.
        INSERT ztable_exchange_log FROM TABLE @ztable_data.
        IF sy-subrc <> 0.
          " Handle database insert errors
          CLEAR result.
          LOOP AT keys ASSIGNING <key>.
            INSERT VALUE #( %tky = <key>-%tky ) INTO TABLE failed-exchange.
            INSERT VALUE #( %tky = <key>-%tky
                            %msg = NEW zcx_rap_error( 
                                     severity = if_abap_behv_message=>severity-error
                                     textid   = 'DB_ERROR'
                                     text     = 'Failed to save to database' )
                          ) INTO TABLE reported-exchange.
          ENDLOOP.
        ENDIF.
      CATCH cx_root INTO DATA(db_error).
        " Handle database exceptions
        CLEAR result.
        LOOP AT keys ASSIGNING <key>.
          INSERT VALUE #( %tky = <key>-%tky ) INTO TABLE failed-exchange.
          INSERT VALUE #( %tky = <key>-%tky
                          %msg = NEW zcx_rap_error( 
                                   severity = if_abap_behv_message=>severity-error
                                   textid   = 'EXCEPTION'
                                   text     = '' )
                        ) INTO TABLE reported-exchange.
        ENDLOOP.
    ENDTRY.
  ENDIF.

  " Set framework response parameters
  failed   = failed.
  reported = reported.
ENDMETHOD.
junwu
SAP Champion
SAP Champion
0 Kudos

did you implement the read method in the handler class?

aatan
Explorer
0 Kudos

Hi @junwu,

Yes, it has been implemented. I am calling the service data again with the selected key values and filling the "RESULT" return.

When I click the SetData(For Action) button, the setExchange method is called first, followed by the Read method.

Kind regards, aatan.

junwu
SAP Champion
SAP Champion
then just debug, why the read method didn't return anything