on ‎2025 Mar 03 12:58 AM
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.
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.
First, make sure you have:
Eclipse IDE with SAP Development Tools (ADT) installed.
Access to your SAP BTP or ABAP system.
In Eclipse:
Open your ABAP Cloud Project.
Right-click onCore Data Services, then choose New Data Definition.
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.
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;
}
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.
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.
Request clarification before answering.
custom entity just gives you an option to model your data model where your data cannot be fetched from db directly. nothing great.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 10 | |
| 7 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.