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

How To Create A Basic Custom Entity?

ugurdurmus
Explorer
0 Likes
2,011

If you're working with SAP and want to fetch data from different sources without directly connecting to a database, Custom Entity is your go-to option. It helps you get data from APIs, web services, or other custom business logic without much hassle.

In this guide, we'll go through step-by-step instructions on how to set up a Custom Entity in Eclipse.

What is a Custom Entity?

A Custom Entity is like a virtual table in SAP. But instead of storing data, it pulls information from different sources whenever you need it. This makes it perfect for showing dynamic data or connecting with external systems.

Why Use Custom Entities?

  • No Database Needed: You don't need to create database tables.
  • Flexible Data Sources: Fetch data from APIs, services, or other systems.
  • Great for Reporting: Use it to display data without saving anything.
  • Works with CDS Views: You can combine it with other SAP data models.

Steps to Create a Custom Entity in Eclipse

1. Preparing Your Development Environment

First, make sure you have:

  • Eclipse IDE with SAP Development Tools (ADT) installed.

  • Access to your SAP BTP or ABAP system.

2. Building the CDS Data Model

In Eclipse:

  1. Open your ABAP Cloud Project.

  2. Right-click onCore Data Services, then choose New Data Definition.

  3. Name your entity and use this simple template.

 

@EndUserText.label: 'Custom Entity Example'
@AccessControl.authorizationCheck: #NOT_REQUIRED
@Metadata.allowExtensions: true
@OData.publish: true

define custom entity ZC_CUSTOM_ENTITY {
  key ID   : UUID;
  Name     : String(50);
  Age      : Integer;
  Country  : String(50);
}

 

This code creates a custom entity with fields like Name, Age, and Country. The ID field is the unique key.

3. Defining Custom Entity Behavior

Now, let's tell SAP what the Custom Entity should do. Create a Behavior Definition file with this code:

 

managed implementation in class ZCL_CUSTOM_ENTITY_HANDLER unique;

define behavior for ZC_CUSTOM_ENTITY persistent {
  use draft;
  field ( mandatory ) Name;
  field ( readonly ) ID;
  determination getData on modify;
}

 

  • Makes the Name field mandatory.
  • Marks ID as read-only.
  • Sets up a rule to fetch data whenever something is modified.

4. Implementing Business Logic

Now let's create the handler class where we fetch the data:

 

class ZCL_CUSTOM_ENTITY_HANDLER definition
  public final create public .

public section.
  interfaces if_rap_query_provider.

endclass.

class ZCL_CUSTOM_ENTITY_HANDLER implementation.
  method if_rap_query_provider~select.
    data lt_data type table of ZC_CUSTOM_ENTITY.
    append value #( ID = '123456' Name = 'John Doe' Age = 30 Country = 'USA' ) to lt_data.
    append value #( ID = '789012' Name = 'Jane Doe' Age = 28 Country = 'Germany' ) to lt_data.
    result = lt_data.
  endmethod.

  method if_rap_query_provider~select_by_association.
    " Custom implementation for associated queries
  endmethod.

  method if_rap_query_provider~select_by_key.
    data lt_data type table of ZC_CUSTOM_ENTITY.
    if keys is not initial.
      loop at keys assigning field-symbol(<key>).
        if <key>-id = '123456'.
          append value #( ID = '123456' Name = 'John Doe' Age = 30 Country = 'USA' ) to lt_data.
        elseif <key>-id = '789012'.
          append value #( ID = '789012' Name = 'Jane Doe' Age = 28 Country = 'Germany' ) to lt_data.
        endif.
      endloop.
    endif.
    result = lt_data.
  endmethod.

  method if_rap_query_provider~select_count.
    result = 2.
  endmethod.

endclass.

 

  • select: Fetches all records.
  • select_by_key: Fetches specific records by key.
  • select_by_association: Allows fetching data through associations (relationships between entities).
  • select_count: Returns the number of available records.

5. Testing Your Custom Entity

  • Right-click your project and choose Run As > ABAP Application.
  • Use SAP Gateway Client or Fiori Elements to check if your Custom Entity is returning data.

Conclusion

This simple yet powerful feature helps you fetch dynamic data without setting up database tables. If you need to get data from APIs or other systems, Custom Entities are a great option.

Let me know if you'd like any help or have questions.

Accepted Solutions (0)

Answers (1)

Answers (1)

junwu
SAP Champion
SAP Champion
0 Likes

I don't agree

Why Use Custom Entities?

  • No Database Needed: You don't need to create database tables. not "No database needed", if you have data in db that you can use, why bother to use custom entity?
  • Flexible Data Sources: Fetch data from APIs, services, or other systems.
  • Great for Reporting: Use it to display data without saving anything. great for reporting are you kidding?
  • Works with CDS Views: You can combine it with other SAP data models.

custom entity just gives you an option to model your data model where your data cannot be fetched from db directly. nothing great.

aldo_buson
Explorer
0 Likes
there are many errors and data extraction does not work. ai generated?