cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Custom entity on public cloud RAP

ozdemiryigit
Explorer
0 Kudos
1,293

Hello Everyone,

I created a CDS based on a custom entity in SAP RAP cloud. I added a new button to this CDS. This button will perform an action on all records displayed on the screen. However, in the class I defined in the behavior, I cannot read the data on the screen. How can I do it?

Accepted Solutions (0)

Answers (2)

Answers (2)

junwu
SAP Champion
SAP Champion
0 Kudos

you will only get the key, I believe. you have to read from remote again if you need the data.

AndreasMuno
Product and Topic Expert
Product and Topic Expert
0 Kudos

Thank you for your question, @ozdemiryigit.  There is very scarce context to go by so I will stretch my own imagination a bit:
When defining the list of records make it a worklist with selectable items. The ID of each selected record in the list will then be available to transact on with your custom button.

For the below I asked chatGPT for assistance. I have no way to verify the accuracy of it, but wanted to share regardless as there might still be some useful aspects to be found. The use case she assumed (from my prior conversation) was the pressed button would generate a presentation deck from selected line items:

"In SAP ABAP RESTful Application Programming Model (RAP), you can define a custom function and add a corresponding button in your Fiori Elements application. Below, I’ll guide you through the process, including creating a custom function, defining the behavior, and integrating it into the Fiori Elements UI.

Step-by-Step Guide to Define a Custom Function in SAP RAP and Add a Button in Fiori Elements

Step 1: Enhance the CDS View with a custom action

First, create or enhance the existing CDS view to define a custom action.

 

@AbapCatalog.sqlViewName: 'ZCUSFUNC'@EndUserText.label: 'Custom Function View'define root view entity ZI_CustomFunction  as select from my_table_name{   key field1,   field2,   field3}
@EndUserText.label : 'Generate Deck'            define action GenerateDeck                result [0..1] $self;

 

 

Step 2: Implement Behavior Definition

Create a behavior definition for the CDS view where you will implement your custom action.

 

managed; define behavior for ZI_CustomFunctionimplementation in class ZBP_CustomFunction unique;define action GenerateDeck;}

 

 

Step 3: Implement Behavior Class

Implement the custom action in the behavior class. You do this in the ABAP class corresponding to your behavior definition.

 

CLASS ZBP_CustomFunction DEFINITION  PUBLIC  FINAL  CREATE PUBLIC .
  PUBLIC SECTION.    INTERFACES if_beh_save.    INTERFACES if_beh_modify.    INTERFACES if_beh_query.    INTERFACES if_beh_lock.    INTERFACES if_origin_handler.    INTERFACES if_consistent_check.    INTERFACES if_stack_save.
    METHODS generate_deck      IMPORTING        value_aspects type any table       RETURNING        value_value type ZI_CustomFunction_TAB . ENDCLASS.

CLASS ZBP_CustomFunction IMPLEMENTATION.

METHOD generate_deck.  " Your logic here. Access selected entries via value_aspects "ENDMETHOD.
ENDCLASS.
 

Step 4: Extend the Fiori Elements UI

Extend your Fiori Elements application to include the custom action as a button in the UI.

  1. Update the Metadata Annotations:

    Add the custom action to the metadata annotations to specify the button's appearance and behavior.

    <Annotations Target="Your.OData.Service.ZI_CustomFunction">  <Annotation Term="Capabilities.ActionSupport">    <PropertyValue Property="GenerateDeck" Bool="true" />  </Annotation>  <Annotation Term="UI.LineItem">     ...     <Record Type="UI.DataFieldForAction">        <PropertyValue Property="Label" String="Generate Deck"/>        <PropertyValue Property="Action" String="Your.OData.Service.ZI_CustomFunction/GenerateDeck"/>     </Record>  </Annotation></Annotations>
  2. Updating the manifest.json:

    Define the custom action in manifest.json to make it available in the UI.

    {  "sap.ui5": {    "extends": {      "extensions": {        "sap.ui.controllerExtensions": {          "sap.fe.templates.ListReport.ListReportController": {            "controllerName": "your.namespace.ext.controller.CustomController"          }        }      }    },    "models": {      "": {        "dataSource": "mainService",        "preload": true      }    },    "dataSources": {      "mainService": {        "uri": "/odata/v4/YourService/",        "type": "OData",        "settings": {          "odataVersion": "4.0"        }      },      "manifest":[       {         "addCustomButtonAction": {             "handler": "initCustomAction",             "text": "{i18n>GenerateDeck}"          }       }     ]    },     "actions": {           "GenerateDeck": {               "text": "Generate Deck",               "press": "function(){console.log('Button pressed');}"           }       }  }}
  3. Create Controller Extension for Custom Function:

    Create a controller extension where you will handle the button press event to call the custom action.

    sap.ui.define([  "sap/ui/core/mvc/Controller",  "sap/m/MessageToast"], function (Controller, MessageToast) {  "use strict";
      return Controller.extend("your.namespace.ext.controller.CustomController", {    onInit: function () {      // Any initialization code    },
        onGenerateDeck: function (oEvent) {      var oView = this.getView();      var oModel = oView.getModel();
          // Get selected items      var aSelectedContexts = oView.byId("tableId").getSelectedContexts();
          // Collecting the IDs of the selected items      var aSelectedIds = aSelectedContexts.map(function (oContext) {        return oContext.getProperty("ID");      });
          // Calling the action      oModel.callFunction("/GenerateDeck", {        method: "POST",        urlParameters: {          "IDs": aSelectedIds        },        success: function (oData, response) {          MessageToast.show("Deck generated successfully!");        },        error: function (oError) {          MessageToast.show("Error generating deck!");        }      });    }  });});

    Conclusion

    By following these steps, you can define a custom function in SAP RAP and integrate it with a button in your Fiori Elements application. This includes creating the necessary CDS views, behavior definition and class implementation, and configuring the Fiori Elements UI and controller extension to handle the custom action appropriately."

         

  4.  
  5.  

@ozdemiryigit