
Step 1- Create a CDS Root Custom Entity for the Header (using Eclipse tool)
Start by creating a root custom entity for the PO Header entity. This entity will have an association to the PO Items.
@ObjectModel.query.implementedBy:'ABAP:YCL_MM_PO_DETAILS'
@EndUserText.label: 'Header data'
define root custom entity YMM_PO_HEADER
// with parameters parameter_name : parameter_type
{
key EBELN : ebeln;
BUKRS : bukrs;
BSTYP : ebstyp;
BSART : esart;
LOEKZ : loekz;
_Items : composition[1..*] of YMM_PO_ITEM;
}
Step 2- Create a CDS custom entity for Item
Next, create a custom entity for PO Item. The Item entity will reference the Header entity using an association.
@ObjectModel.query.implementedBy:'ABAP:YCL_MM_PO_DETAILS'
@EndUserText.label: 'PO Item'
define custom entity YMM_PO_ITEM
// with parameters parameter_name : parameter_type
{
key EBELN : ebeln;
key EBELP : ebelp;
MATNR : matnr;
@Semantics.quantity.unitOfMeasure: 'MEINS'
MENGE : bstmg;
MEINS : bstme;
@Semantics.amount.currencyCode: 'currency'
NETPR : bprei;
@Semantics.currencyCode
currency : abap.cuky( 5 );
_Header : association to parent YMM_PO_HEADER on $projection.EBELN = _Header.EBELN;
}
Step 3- Create a your own Custom class which you mentioned in Entity
Now, create a custom ABAP class to implement the functionality behind your CDS entities. The class will implement the necessary interfaces.
@ObjectModel.query.implementedBy:'ABAP:YCL_MM_PO_DETAILS'
interfaces IF_OO_ADT_CLASSRUN .
interfaces IF_RAP_QUERY_PROVIDER .
Step 4- Implement the class
class YCL_MM_PO_DETAILS definition
public
final
create public .
public section.
types : begin of ty_header,
EBELN type ebeln,
BUKRS type bukrs,
BSTYP type ebstyp,
BSART type esart,
LOEKZ type loekz,
end of ty_header.
data : lt_header type table of ty_header.
types : begin of ty_item,
EBELN type ebeln,
EBELP type ebelp,
MATNR type matnr,
MENGE type bstmg,
MEINS type bstme,
NETPR type bprei,
end of ty_item.
data : lt_item type table of ty_item.
interfaces IF_OO_ADT_CLASSRUN .
interfaces IF_RAP_QUERY_PROVIDER .
protected section.
private section.
ENDCLASS.
CLASS YCL_MM_PO_DETAILS IMPLEMENTATION.
method IF_OO_ADT_CLASSRUN~MAIN.
endmethod.
method IF_RAP_QUERY_PROVIDER~SELECT.
CASE io_request->get_entity_id( ).
when 'YMM_PO_HEADER'.
DATA(lt_parameters) = io_request->get_parameters( ).
DATA(lv_offset) = io_request->get_paging( )->get_offset( ).
DATA(lv_page_size) = io_request->get_paging( )->get_page_size( ).
DATA(lv_header_filter) = io_request->get_filter( )->get_as_sql_string( ).
DATA(lv_max_rows) = COND #( WHEN lv_page_size = if_rap_query_paging=>page_size_unlimited
THEN 0
ELSE lv_page_size ).
select * from ekko
where (lv_header_filter)
order by EBELN
into corresponding fields of table _header
up to _max_rows rows
offset _offset.
* IF io_request->is_total_numb_of_rec_requested( ).
io_response->set_total_number_of_records( lines( lt_header ) ).
io_response->set_data( lt_header ).
* endif.
when 'YMM_PO_ITEM'.
DATA(lt_para_item) = io_request->get_parameters( ).
DATA(lv_offset_item) = io_request->get_paging( )->get_offset( ).
DATA(lv_page_size_item) = io_request->get_paging( )->get_page_size( ).
DATA(lv_item_filter) = io_request->get_filter( )->get_as_sql_string( ).
DATA(lv_max_rows_item) = COND #( WHEN lv_page_size_item = if_rap_query_paging=>page_size_unlimited
THEN 0
ELSE lv_page_size_item ).
select * from ekpo
where (lv_item_filter)
order by EBELN
into corresponding fields of table _item
up to _max_rows rows
offset _max_rows_item.
io_response->set_total_number_of_records( lines( lt_item ) ).
io_response->set_data( lt_item ).
endcase.
endmethod.
ENDCLASS.
Note- If your Item entity is not getting triggered possibly you might have missed filter .
Step 5- Create Service definition for your root view entity and expose you entities
->Right click on your root view entity->Click new service definition
@EndUserText.label: 'Po Details'
define service YMM_PO {
expose YMM_PO_HEADER as Header;
expose YMM_PO_ITEM as Item;
}
Step 6- Create Service binding for your service definition
After defining the service, create a service binding to make the service available for consumption.
If your service is an OData V4 service, you may encounter issues during publishing. For assistance with publishing and activating your service, refer to this
Step 7- Test the GET Call with Association
Finally, test your service by making a GET request to retrieve the Header and Item details as a nested JSON structure.
Note- If you want to pass the URL with key value
Header('value')?$expand=_Items
If you're interested in consuming this data in RAP with a tree structure using Flexible Programming Model (FPM), feel free to reach out.
Enjoy Coding!
Thanks and Regards,
Thulasiram Ammati
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
19 | |
16 | |
9 | |
5 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 |