Sei sulla pagina 1di 18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

GettingStarted Newsletters

Hi,Guest

LogOn

JoinUs

Store

SearchtheCommunity

Products

Services&Support

AboutSCN

Downloads

Industries

Training&Education

Partnership

DeveloperCenter

Activity

LinesofBusiness

UniversityAlliances

Events&Webinars

Innovation

Browse

Communications

Actions

SAPGateway

Let'scodeassociation/navigationanddataprovider
expandinODataservice!
PostedbyChandrashekharMahajaninSAPGatewayonSep24,20143:26:52PM
Share

Like 4

Introduction
Scenario
Procedure
Coding
Association/Navigation
Frameworkexpand
UseofNavigationPathtoreadnavigationkeys
DataProviderExpand
ImplementingGET_EXPANDED_ENTITYSET
ImplementingGET_EXPANDED_ENTITY
ClosingRemarks

Introduction

Inmyearlierblog LetscodeCRUDQandFunctionImportoperationsinODataservice!weunderstoodthebasic
operationperformedinODataservice.

InthisblogIwillexplaincreationofsimpleSAPGatewayODataservicehavingassociationandnavigation
betweenentities.Alsowewillseehowtoimplementitthroughcodebasedapproachandfinallyconcludewith
implantationofGET_EXPANDED_ENTITYSETandGET_EXPANDED_ENTITY
invokedby$expand.

NoteStepsmentionedinthisblogareperformedinSAPGatewaySystemSP08(EmbeddedArchitecture)

Letsseewhatismeantbyassociationandnavigationproperty.

AssociationsdefinetherelationshipbetweentwoormoreEntityTypes(forexample,
EmployeeWorksForDepartment).InstancesofassociationsaregroupedinAssociation
Sets.
NavigationPropertiesarespecialpropertiesonEntityTypeswhichareboundtoaspecific
associationandcanbeusedtorefertoassociationsofanentity.
Finally,allinstancecontainers(EntitySetsandAssociationSets)aregroupedinanEntity
Container.

ReferenceOverview|OpenDataProtocol

Alsolet'sunderstandthedifferencebetweenassociation/navigationand$expand.Inshort,itisasbelow,

Association/Navigation Givemeassociated(dependent)entity/entitiesusing
http://services.odata.org/OData/OData.svc/Categories(1)/Products?
Navigationproperty
$format=json
$expand

Givemeassociated(dependent)entity/entities+
Principalentity/entities
usingNavigationproperty

http://services.odata.org/OData/OData.svc/Categories(1)?
$expand=Products&$format=json

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

1/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

youcanalsoreferthisniceblog

ImplementingExpandEntity/EntitySetby

SrikanthGajula

Scenario

WewillreadSalesorder,itemsandproductdatafromEnterpriseProcurementModel(EPM).Thisisthepictorial
representationofsalesorder,itemsandproductwiththeirassociation.

Wewillhave3entitiesasdisplayedinbelowEDMdiagram.

SalesOrderwillbeassociatedwithSalesOrderItemandSalesOrderItemwithProduct.

PrincipleEntity

DependentEntity

NavigationProperty

Cardinality

SalesOrder

SalesOrderItem

OrderToItems

1:N

SalesOrderItem

Product

ItemToProduct

1:1

WewillusebelowBAPIstogettheSalesOrder,ItemsandProductdatainDPC_EXTclassmethods.
BAPI_EPM_SO_GET_LIST
BAPI_EPM_SO_GET_DETAIL
BAPI_EPM_PRODUCT_GET_DETAIL

Wewillcodeforassociation/navigationanddataproviderexpandscenarioandwillalsounderstandtheframework
expand.

BeforethatjustlookatdifferencebetweenDataproviderexpandandframeworkexpand

FrameworkExpand

DataProviderExpand

Formerlycalledasgenericexpand

Formerlycalledasbasicexpand

Requiresnoimplementa oneort

Requiresimplementa oneort

Asthisishandledbyframework,samelogicmaybe
calledmul ple mesinloopresul nginpoor
performance

Insomecases,thisprovidesbe er
performancedependingonhowthecodeis
implemented

ReferenceExpandinFrameworkandDataProviderSAPNetWeaverGatewaySAPLibrary

Procedure
http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

2/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

Createen tySalesOrderbyimpor ngDDICstructureasshownbelow.PleasenotethatEn tysetwillbe


createdbydefaultifthecheckboxCreateDefaultEn tySetischecked.

RepeattheprocessforentitiesSalesOrderItemandProduct.Endresultwillbeasdisplayedbelow.

Nowletscreateassociation,navigationetc.ByusingCreateAssociationwizard,itisjust3stepsasdisplayedbelow.
ThiswillcreateNavigationproperty,Associationsetetc.

CreateassociationbetweenentitiesSalesOrderandSalesOrderItemwithnavigationpropertyasOrderToItems.

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

3/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

Onsimilarlines,createassociationbetweenentitiesSalesOrderItemandProductwithnavigationpropertyas
ItemToProduct.

PleasenotethatwedonothavereferentialconstraintscreatedbetweenSalesOrderItemandProduct.

ThefinalODatamodelingwilllooklikeasbelow,

Coding

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

4/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

Coding

Inthissection,wewillredefinemethodsinDPC_EXTclass.Pleasenotethatcodeprovidedinthisblogisinsimplest
form.Youmayneedtoconsiderpropererrorhandlingandotherbestpracticeswhilewritingthecode.

Association/Navigation

FirstwewillimplementlogicinmethodSALESORDERSET_GET_ENTITYSETbyredefiningit.

WewillusebelowexecutionURIinGatewayClient(Transaction/IWFND/GW_CLIENT)tochecktheresponse
payload.

/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet
METHODsalesorderset_get_entityset.

DATA:lt_salesorderTYPETABLEOFbapi_epm_so_header,
ls_salesorderLIKELINEOFlt_salesorder,
ls_entityLIKELINEOFet_entityset,
l_max_rowsTYPEbapi_epm_max_rows.

l_max_rowsbapimaxrow='10'.

CALLFUNCTION'BAPI_EPM_SO_GET_LIST'
EXPORTING
max_rows=l_max_rows
TABLES
soheaderdata=lt_salesorder.

*FillET_ENTITYSET
LOOPATlt_salesorderINTOls_salesorder.
MOVECORRESPONDINGls_salesorderTOls_entity.
APPENDls_entityTOet_entityset.
ENDLOOP.
ENDMETHOD.

RedefinemethodSALESORDERSET_GET_ENTITYasbelowandthenexecutewithbelowURI

/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000009')
METHODsalesorderset_get_entity.

DATA:ls_salesorderTYPEbapi_epm_so_header,
ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_soidTYPEbapi_epm_so_id.

*Getthekeypropertyvalues
READTABLEit_key_tabWITHKEYname='SoId'INTOls_key_tab.

lv_soid=ls_key_tabvalue.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soid
IMPORTING
output=lv_soid.

CALLFUNCTION'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id=lv_soid
IMPORTING
headerdata=ls_salesorder.

*FillER_ENTITY
MOVECORRESPONDINGls_salesorderTOer_entity.

ENDMETHOD.

RedefinemethodSALESORDERITEMSE_GET_ENTITYSETasbelowwithURI

/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000008')/OrderToItems
METHODsalesorderitemse_get_entityset.
DATA:ls_salesorderTYPEbapi_epm_so_header,
lt_itemdataTYPETABLEOFbapi_epm_so_item,
ls_itemdataTYPEbapi_epm_so_item,

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

5/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

ls_entityLIKELINEOFet_entityset.

DATA:ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_soidTYPEbapi_epm_so_id.

*Getthekeypropertyvalues
READTABLEit_key_tabWITHKEYname='SoId'INTOls_key_tab.

lv_soid=ls_key_tabvalue.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soid
IMPORTING
output=lv_soid.

CALLFUNCTION'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id=lv_soid
IMPORTING
headerdata=ls_salesorder
TABLES
itemdata=lt_itemdata.

LOOPATlt_itemdataINTOls_itemdata.
MOVECORRESPONDINGls_itemdataTOls_entity.
APPENDls_entityTOet_entityset.
ENDLOOP.

ENDMETHOD.

NoticethatweusednavigationpropertyOrderToItemstogettheassociatedentities.

Frameworkexpand

ByexecutingbelowURI,itwillcallframeworkexpandbydefault.

/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet?$expand=OrderToItems

ThiscallsmethodSALESORDERSET_GET_ENTITYSETandSALESORDERITEMSE_GET_ENTITYSETinloop.

CheckthevaluesinheadernamesapgwstatisticsforURI
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet?$expand=OrderToItems&sapstatistics=true

ThiswillgiveyouperformancestatisticsforODatarequest.Formoreinformation,refer SomenewfeaturesinSAP
NWGateway2.0SP08

IfweexecuteURI/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000009')?
$expand=OrderToItems

ItwillcallmethodSALESORDERSET_GET_ENTITYandSALESORDERITEMSE_GET_ENTITYSET.

ForbelowURItowork
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000000')/OrderToItems(SoId='0500000000',SoItemPos='0000000010')

Weneedtoimplementlogicwithnavigationpropertykeys.

Alternativelywecanreadas

/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderItemSet(SoId='0500000000',SoItemPos='0000000010')

LetsimplementSALESORDERITEMSE_GET_ENTITYsothatwecanreaddataforaboveURIas

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

6/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

METHODsalesorderitemse_get_entity.
DATA:ls_salesorderTYPEbapi_epm_so_header.

DATA:ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_soidTYPEbapi_epm_so_id,
lv_soitemposTYPEsnwd_so_item_pos,
lt_itemdataTYPETABLEOFbapi_epm_so_item,
ls_itemdataTYPEbapi_epm_so_item.

*Getthekeypropertyvalues
READTABLEit_key_tabWITHKEYname='SoId'INTOls_key_tab.
lv_soid=ls_key_tabvalue.

READTABLEit_key_tabWITHKEYname='SoItemPos'INTOls_key_tab.
lv_soitempos=ls_key_tabvalue.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soid
IMPORTING
output=lv_soid.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soitempos
IMPORTING
output=lv_soitempos.

*GetdatafromBAPI

CALLFUNCTION'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id=lv_soid
IMPORTING
headerdata=ls_salesorder
TABLES
itemdata=lt_itemdata.

READTABLElt_itemdataINTOls_itemdataWITHKEYso_id=lv_soid
so_item_pos=lv_soitempos.
IFsysubrc=0.
MOVECORRESPONDINGls_itemdataTOer_entity.
ENDIF.
ENDMETHOD.

UseofNavigationPathtoreadnavigationkeys

ToreadthedataforbelowURI,weneedtoimplementlogicasbelowinmethodPRODUCTSET_GET_ENTITY

ExecutionURI
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000001')/OrderToItems(SoId='500000001',SoItemPos='0000000010')/ItemToProduct
METHODproductset_get_entity.
DATA:ls_salesorderTYPEbapi_epm_so_header.

DATA:ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_soidTYPEbapi_epm_so_id,
lv_soitemposTYPEsnwd_so_item_pos,
lt_itemdataTYPETABLEOFbapi_epm_so_item,
ls_itemdataTYPEbapi_epm_so_item.

DATA:ls_navigationTYPE/iwbep/s_mgw_navigation_path,
lv_propertyTYPEstring.

DATA:lv_product_idTYPEbapi_epm_product_id,
lv_product_headerTYPEbapi_epm_product_header.

IFiv_source_name=iv_entity_name.

*Getthekeypropertyvalues
READTABLEit_key_tabWITHKEYname='ProductId'INTOls_key_tab.

IFsysubrc=0.
*MOVECORRESPONDINGls_itemdatatoer_entity.

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

7/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

lv_product_id=ls_key_tabvalue.

CALLFUNCTION'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id=lv_product_id
IMPORTING
headerdata=lv_product_header.

MOVECORRESPONDINGlv_product_headerTOer_entity.

ENDIF.

ELSE.

IFit_navigation_pathISNOTINITIAL.
READTABLEit_navigation_pathINTOls_navigationINDEX1.
IFsysubrcEQ0.
CASEls_navigationnav_prop.
WHEN'OrderToItems'.
LOOPATls_navigationkey_tabINTOls_key_tab.
CASEls_key_tabname.
WHEN'SoId'.
lv_soid=ls_key_tabvalue.
WHEN'SoItemPos'.
lv_soitempos=ls_key_tabvalue.
WHENOTHERS.

ENDCASE.
ENDLOOP.
ENDCASE.
ENDIF.

ENDIF.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soid
IMPORTING
output=lv_soid.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soitempos
IMPORTING
output=lv_soitempos.

*GetdatafromBAPI_EPM_SO_GET_DETAIL

CALLFUNCTION'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id=lv_soid
IMPORTING
headerdata=ls_salesorder
TABLES
itemdata=lt_itemdata.

*FillER_ENTITY
READTABLElt_itemdataINTOls_itemdataWITHKEYso_id=lv_soid
so_item_pos=
lv_soitempos.
IFsysubrc=0.
lv_product_idproduct_id=ls_itemdataproduct_id.

CALLFUNCTION'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id=lv_product_id
IMPORTING
headerdata=lv_product_header.

MOVECORRESPONDINGlv_product_headerTOer_entity.

ENDIF.

ENDIF.
ENDMETHOD.

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

8/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

AlsonotethatwecanreadproductdirectlyusingURI/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/ProductSet('HT
1030')

NowtrywiththisURI/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000000')?
$expand=OrderToItems/ItemToProduct
Productdetailswillnotfilledasinthenavigationkeysareemptybecausewedonothavereferentialconstraint.

Alsotrywith
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderItemSet(SoId='0500000000',SoItemPos='0000000020')/ItemToProduct
andcheckwhyitisnotworking.

Additionallyyoucanqueryas

/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000000')/$links/OrderToItems
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000009')/$links/OrderToItems/$count

DataProviderExpand

Inthissection,wewillseehowtoimplementdataproviderexpandbyredefiningGET_EXPANDED_ENTITYSETand
GET_EXPANDED_ENTITY.

ImplementingGET_EXPANDED_ENTITYSET

LetsredefineGET_EXPANDED_ENTITYSET.Withredefinition(justblankcode)executeagaintheURI
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet?$expand=OrderToItems

Youwillnotgetanyresponseasyouneedtoimplementthecodeyourself.

OneoftheimportantpointwhileimplementinglogicisDatadeclaration!Basedonleveltillwhichyouwanttoexpand,
youneedtodefineyourinternaltablehavingnestedstructureortabletype.

Inbelowcode,wewanttoexpandSalesOrderanditsitems.Hencetheexpandtechnicalclausewillbe
ORDERTOITEMS.
METHOD/iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.

DATA:BEGINOFt_expand_so.
INCLUDETYPEzcl_ztest_dp_expand_mpc_ext=>ts_salesorder.
DATA:ordertoitemsTYPEzcl_ztest_dp_expand_mpc_ext=>tt_salesorderitem,
ENDOFt_expand_so.

DATA:lt_expand_soLIKETABLEOFt_expand_so,
ls_expand_soLIKEt_expand_so,
ls_itemTYPEzcl_ztest_dp_expand_mpc_ext=>ts_salesorderitem.

DATA:lt_salesorderTYPETABLEOFbapi_epm_so_header,
ls_salesorderLIKELINEOFlt_salesorder,
lt_itemdataTYPETABLEOFbapi_epm_so_item,
ls_itemdataTYPEbapi_epm_so_item,
l_max_rowsTYPEbapi_epm_max_rows.

CONSTANTS:lc_expand_tech_clauseTYPEstringVALUE'ORDERTOITEMS'.

*ReadSalesOrderandItemdata
l_max_rowsbapimaxrow='10'.
CALLFUNCTION'BAPI_EPM_SO_GET_LIST'
EXPORTING
max_rows=l_max_rows
TABLES
soheaderdata=lt_salesorder
soitemdata=lt_itemdata.

*Dataprocessinglogic
LOOPATlt_salesorderINTOls_salesorder.
MOVECORRESPONDINGls_salesorderTOls_expand_so.
LOOPATlt_itemdataINTOls_itemdataWHEREso_id=ls_salesorderso_id.
MOVECORRESPONDINGls_itemdataTOls_item.
APPENDls_itemTOls_expand_soordertoitems.
CLEAR:ls_item.
ENDLOOP.
APPENDls_expand_soTOlt_expand_so.
CLEAR:ls_expand_so.
ENDLOOP.

*FillEE_ENTITYSET
copy_data_to_ref(
EXPORTING

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

9/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

is_data=lt_expand_so
CHANGING
cr_data=er_entityset).

*InsertNavigationpropertyintoET_EXPANDED_TECH_CLAUSES
INSERTlc_expand_tech_clauseINTOTABLEet_expanded_tech_clauses.
ENDMETHOD.

QuerywithURI/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet?$expand=OrderToItems&sap
statistics=truetochecktheruntimestatisticsandcomparethevalueswithbeforeimplementingdataproviderexpand.

Nowwewilltrytoexpandtoonelevelmorei.e.wewillexpandsalesorder,itsitemsandproductofeachitem.Inthis
casetheexpandtechnicalclausewillbeORDERTOITEMS/ITEMTOPRODUCT.

Belowisthecodeweneedtoputtoachievethedesiredresult.

METHOD/iwbep/if_mgw_appl_srv_runtime~get_expanded_entityset.

DATA:BEGINOFt_orderitems.
INCLUDETYPEzcl_ztest_dp_expand_mpc_ext=>ts_salesorderitem.
DATA:itemtoproductTYPEzcl_ztest_dp_expand_mpc_ext=>ts_product,
ENDOFt_orderitems.

DATA:BEGINOFt_expand_so.
INCLUDETYPEzcl_ztest_dp_expand_mpc_ext=>ts_salesorder.
DATA:ordertoitemsLIKETABLEOFt_orderitems,
ENDOFt_expand_so.

DATA:lt_expand_soLIKETABLEOFt_expand_so,
ls_expand_soLIKEt_expand_so,
ls_itemLIKEt_orderitems.

DATA:lt_salesorderTYPETABLEOFbapi_epm_so_header,
ls_salesorderLIKELINEOFlt_salesorder,
lt_itemdataTYPETABLEOFbapi_epm_so_item,
ls_itemdataTYPEbapi_epm_so_item,
l_max_rowsTYPEbapi_epm_max_rows.

DATA:lv_product_idTYPEbapi_epm_product_id,
ls_product_headerTYPEbapi_epm_product_header.

CONSTANTS:lc_expand_tech_clauseTYPEstringVALUE
'ORDERTOITEMS/ITEMTOPRODUCT'.

*ReadSalesOrderandItemdata
l_max_rowsbapimaxrow='10'.
CALLFUNCTION'BAPI_EPM_SO_GET_LIST'
EXPORTING
max_rows=l_max_rows
TABLES
soheaderdata=lt_salesorder
soitemdata=lt_itemdata.

*Dataprocessinglogic
LOOPATlt_salesorderINTOls_salesorder.
MOVECORRESPONDINGls_salesorderTOls_expand_so.
LOOPATlt_itemdataINTOls_itemdataWHEREso_id=ls_salesorderso_id.

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

10/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

MOVECORRESPONDINGls_itemdataTOls_item.
lv_product_id=ls_itemdataproduct_id.
CALLFUNCTION'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id=lv_product_id
IMPORTING
headerdata=ls_product_header.
MOVECORRESPONDINGls_product_headerTOls_itemitemtoproduct.
APPENDls_itemTOls_expand_soordertoitems.
CLEAR:ls_item.
ENDLOOP.
APPENDls_expand_soTOlt_expand_so.
CLEAR:ls_expand_so,lv_product_id.
ENDLOOP.

*FillEE_ENTITYSET
copy_data_to_ref(
EXPORTING
is_data=lt_expand_so
CHANGING
cr_data=er_entityset).

*InsertNavigationpropertyintoET_EXPANDED_TECH_CLAUSES
INSERTlc_expand_tech_clauseINTOTABLEet_expanded_tech_clauses.
ENDMETHOD.

Pleasenotethatweneedtoinsertcompleteexpandclauseinet_expanded_tech_clauses

ImplementingGET_EXPANDED_ENTITY

LetsimplementGET_EXPANDED_ENTITY.

Belowisthequerytoexecuteandcodetoforimplementation.
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000005')?$expand=OrderToItems/ItemToProduct

METHOD/iwbep/if_mgw_appl_srv_runtime~get_expanded_entity.

DATA:BEGINOFt_orderitems.
INCLUDETYPEzcl_ztest_dp_expand_mpc_ext=>ts_salesorderitem.
DATA:itemtoproductTYPEzcl_ztest_dp_expand_mpc_ext=>ts_product,
ENDOFt_orderitems.

DATA:BEGINOFt_expand_so.
INCLUDETYPEzcl_ztest_dp_expand_mpc_ext=>ts_salesorder.
DATA:ordertoitemsLIKETABLEOFt_orderitems,
ENDOFt_expand_so.

DATA:ls_expand_soLIKEt_expand_so,
ls_itemLIKEt_orderitems.

DATA:ls_salesorderTYPEbapi_epm_so_header,
lt_itemdataTYPETABLEOFbapi_epm_so_item,
ls_itemdataTYPEbapi_epm_so_item.

DATA:ls_key_tabTYPE/iwbep/s_mgw_name_value_pair,
lv_soidTYPEbapi_epm_so_id.

DATA:lv_product_idTYPEbapi_epm_product_id,
ls_product_headerTYPEbapi_epm_product_header.

CONSTANTS:lc_expand_tech_clauseTYPEstringVALUE
'ORDERTOITEMS/ITEMTOPRODUCT'.

*GetthekeypropertyvaluesandReadSalesOrderandItemdata
READTABLEit_key_tabWITHKEYname='SoId'INTOls_key_tab.
lv_soid=ls_key_tabvalue.

CALLFUNCTION'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input=lv_soid
IMPORTING
output=lv_soid.

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

11/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

CALLFUNCTION'BAPI_EPM_SO_GET_DETAIL'
EXPORTING
so_id=lv_soid
IMPORTING
headerdata=ls_salesorder
TABLES
itemdata=lt_itemdata.

*Dataprocessinglogic
MOVECORRESPONDINGls_salesorderTOls_expand_so.
LOOPATlt_itemdataINTOls_itemdataWHEREso_id=ls_salesorderso_id.
MOVECORRESPONDINGls_itemdataTOls_item.
lv_product_id=ls_itemdataproduct_id.
CALLFUNCTION'BAPI_EPM_PRODUCT_GET_DETAIL'
EXPORTING
product_id=lv_product_id
IMPORTING
headerdata=ls_product_header.
MOVECORRESPONDINGls_product_headerTOls_itemitemtoproduct.
APPENDls_itemTOls_expand_soordertoitems.
ENDLOOP.

*FillER_ENTITY
copy_data_to_ref(
EXPORTING
is_data=ls_expand_so
CHANGING
cr_data=er_entity).

*InsertNavigationpropertyintoET_EXPANDED_TECH_CLAUSES
INSERTlc_expand_tech_clauseINTOTABLEet_expanded_tech_clauses.
ENDMETHOD.

ClosingRemarks

Importantpointstobeconsideredwhilecodingforassociation/navigationanddataprovider$expand,
Useofnavigationpathandnavigationkey.Entitiescanbedirectlyaccessedorvianavigationproperty.Codefor
bothscenariousingnavigationpathandnavigationkeys.
Datadeclarationofinternaltablesincaseofdataproviderexpand.Understandtherelationsbetweenentities.
whiledeclaringinternaltable,usenavigationpropertynametoaddressdependententitystructure.Itshouldbe
same.Checkbelowdatadeclaration.
DATA:BEGINOFt_orderitems.
INCLUDETYPEzcl_ztest_fw_expand_mpc_ext=>ts_salesorderitem.
DATA:itemtoproductTYPEzcl_ztest_fw_expand_mpc_ext=>ts_product,
ENDOFt_orderitems.

DATA:BEGINOFt_expand.
INCLUDETYPEzcl_ztest_fw_expand_mpc_ext=>ts_salesorder.
DATA:ordertoitemsLIKETABLEOFt_orderitems,
ENDOFt_expand.

DATA:lt_soLIKETABLEOFt_expand,
ls_soLIKEt_expand,
ls_itemLIKEt_orderitems.
Parentanditsimmediatechildwillbeseparatedusing"/"fore.g$expand=OrderToItems/ItemToProductifwe
wouldhave2ndsiblingatorderlevelfore.gPartnersofSalesorderthenwecouldhavenavigationpropertyas
OrderToPartnerswithsayitschildasPartnerToAddressthenweneedtoaccessitas
OrderToPartners/PartnerToAddress.Togettheexpandedresultforbothhierarchies,theexpandclausewilllook
as$expand=OrderToItems/ItemToProduct,OrderToPartners/PartnerToAddress(separatedby",")
Navigationpropertyseparatedwith"/"willbeinsertedintoexpandedtechnicalclause.2ndhierarchywillbe
appendedinexpandedtechnicalclause
Inshort,forexamplementionedinpoint3,itwouldbe,
ls_expanded_clause_items='ORDERTOITEMS/ITEMTOPRODUCT'.
ls_expanded_clause_partners='ORDERTOPARTNERS/PARTNERTOADDRESS'.
APPENDls_expanded_clause_itemsTOet_expanded_tech_clauses.
APPENDls_expanded_clause_partnersTOet_expanded_tech_clauses.

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

12/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

alsoreferthisthread

Errorwith$expandandnavigation:Resourcenotfoundforthesegment'NavAtp'

whetherframeworkexpandordataproviderexpandprovidesbetterresultwilldependsonyourscenario.Please
noticethatwith$expand,wearemakingsinglecalltoreadtheparentandchilddatabutatthesametimeensure
thatthedatatoberetrievedisnottoolarge.

Ihopeyouenjoyedreadingthisblogandnowwillbeabletoplaywithassociation/navigationanddataprovider
$expand!

Pleasefeelfreeifyouhaveanydifferentthoughtstoimproveanysectionofthisblog.

HappyLearning&Coding

33620Views
Products:sap_netweaver_gatewayTags:rest,json,gateway,odata,netweaver_gateway,navigation,association,sap_gateway,
navigation_properties,$expand,get_expanded_entityset,get_expanded_entity,basic_expand,data_provider_expand

AverageUserRating
(19ratings)

Share

Like 4

26Comments
AshwinDuttRSep24,20145:58PM

HelloChandra,

Greatandverywellexplainedby
emphasizingonthescenarioslikeSalesOrderSet('500000005')?
$expand=OrderToItems/ItemToProductaswewouldencounterthosekindofscenariosaswell

Regards,
Ashwin
Like(1)

ChandrashekharMahajanSep25,20147:56AM(inresponsetoAshwinDuttR)

Thanks

AshwinDuttR!
Like(0)

PrabaharanAsokanSep25,201412:28PM

Hi,
Veryusefulblog.SaveditasaPDF.Thanks.

Prabaharan
Like(1)

SyamBabuSep25,20147:24PM

HiChandra,

GreatWork.

andalsoCoveredgatewayperformancewiththisBlog

Thanks,
Syam
Like(2)

ChandrashekharMahajanSep26,20148:45AM(inresponsetoSyamBabu)

Thanksforyourcomments SyamBabu!

Yesthegatewayperformanceisoneoftheimportantpointwhenitscomestoframeworkvs
dataprovider$expand.

Regards,
Chandra
Like(1)

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

13/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

AndreFischerSep25,20148:56PM

HiChandrashekhar,

verywellexplained!

IwillshowthesameintheupcomingdcodeeventsinmysessionDEV360

BestRegards,
Andre

Like(5)

ChandrashekharMahajanSep26,20148:53AM(inresponsetoAndreFischer)

Thanks AndreFischer!Yourappreciationmeansalottome!

IamverymuchexcitedandhonoredthatmyblogwillbeusefulforyoursessionDEV360

Regards,
Chandra
Like(1)

ArindamSamantaSep26,201412:00PM

Thisisgreatjobyouhavedoneonceagain.Itisabuatufullstaff.Thanksalotforthis.Alltheverybest
forfuturework!
Like(1)

VijayVegesanaSep29,20147:27PM

ThanksChandra,greatblog..
Like(1)

RonSargeantOct2,20143:00PM

NiceworkChandra!

itwouldbegoodifyoucouldexpand(nopunintended)ontheusecasefor
GET_EXPANDED_ENTITYSETandGET_EXPANDED_ENTITY.
Asyouhavealreadypointedout,$expandcanprocessanexpansionrequestbyloopingthroughthe
accessmethods.Itwouldbegreattohavesomeclarityofwhyyouwoulddothesemethodsasan
alternative.TheSAPHelpsortofexplainsitbutit'snoteasilyunderstooduntilyouhavedoneabitof
expandingpractice.

Onabestpracticenote,Irecommendusingthetechnicalrequestobjecttofindkeysandnavigation
values.I'mnot100%surebutthetechnicalrequestseemstohavebeenaddedtoassistinOData
compliance.
The'direct'keysetc.exposedinthemethodsignaturesaren'tquitethesame.Foronethingtechnical
elementnamesarealwaysinuppercaseevenifthemodelelementnameisnot.It'salsoeasierto
passonetechnicalobjectreferenceontoothermethodsthanallofthosesignatureparameters.

Regards

Ron.

P.S.it'salsonicetoseethatyoudidn'tgenerateyourservicefromtheBAPIinterfacesphew!
Like(1)

ChandrashekharMahajanOct7,201411:21AM(inresponsetoRonSargeant)

HiRon,

Thanksforyourcomments!

RonSargeantwrote:

itwouldbegoodifyoucouldexpand(nopunintended)ontheusecasefor
GET_EXPANDED_ENTITYSETandGET_EXPANDED_ENTITY.
Asyouhavealreadypointedout,$expandcanprocessanexpansionrequestby
loopingthroughtheaccessmethods.Itwouldbegreattohavesomeclarityof
whyyouwoulddothesemethodsasanalternative.TheSAPHelpsortof
explainsitbutit'snoteasilyunderstooduntilyouhavedoneabitofexpanding
practice.

Areyoureferringtoframeworkexpandvsdataproviderexpand?Ialreadyexplainedthose
pointswithexample.Pleaseletmeknowwhichusecaseyouwantmetoexploremore.
RonSargeantwrote:

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

14/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

Onabestpracticenote,Irecommendusingthetechnicalrequestobjecttofind
keysandnavigationvalues.I'mnot100%surebutthetechnicalrequestseems
tohavebeenaddedtoassistinODatacompliance.
The'direct'keysetc.exposedinthemethodsignaturesaren'tquitethesame.For
onethingtechnicalelementnamesarealwaysinuppercaseevenifthemodel
elementnameisnot.It'salsoeasiertopassonetechnicalobjectreferenceonto
othermethodsthanallofthosesignatureparameters.

YesIagreethatusingtechnicalrequestobjecttogetthekeysandnavigationvalue,filter
valueetcisbestway.andinmyearlierblog, LetscodeCRUDQandFunctionImport
operationsinODataservice!Iexplaineditwithalternativewaytogetthesevaluesinsteadof
methodsignatureparameter.

Regards,
Chandra
Like(0)

RonSargeantOct7,201411:42AM(inresponsetoChandrashekharMahajan)

HiChandra,

Iagreethatyouhaveshownthattherearealternatives,butIwouldn'tsaythey
wereexplainedasaresultofjustbeingshown.

"Areyoureferringtoframeworkexpandvsdataproviderexpand?",Yesandno,in
myunderstanding,thereisjust$expandandhowtheframeworkimplementsthe
requestusingit.Theconsumercan'taskforadifferentmeansofexpanding.A
properlyprovidedrequestcanexpandthefeedwithnospecialcoding,yetthereis
animplementationstubforexpandingfeeds.Ithinkyouhaveshown"ifyouchoose
tobuildanexpandmethod,youcandoitlikethis",butnotexplainedwhy.

Regardinguseoftechnicalobject,itcanbeconfusingforreaderstoseeanauthor
usingdifferentcodingmethods,especiallywhenthatdifferenceisnotthetopicof
thearticle.

InmyearlierblogsIusedthesignaturetablesbecausetherewasnotechnical
contextthatIrecallinearlierversions.NowIalwaysuseitanddon'trevertto"old
style".Itrytokeeptrueto'realcode'inexamples,evenifittakeslonger.Iuse
customcodepatternsintheeditortomakethiseasiertoregulate.

Regards

Ron.
Like(0)

YugandharReddyDec8,20146:32PM(inresponsetoRonSargeant)

HiChandra,

itsagreatblogwithgooddetails.
ItoohavesamequestionasRonandwanttounderstanddifference,why
andwhenweneedtogoforframeworkexpandvsdataproviderexpand.
Regards
YugandharReddy
Like(0)

'Pavan'GolesarJul22,201512:11PM(inresponsetoChandrashekharMahajan)

Hello Chandra,

Verygoodhelpfulblog..Itsmyproblemsolverinmanycases..irefertoitmany
times...IttouchesdownalmostallmajoraspectsthatIfeelIareveryhelpfulfor
learnerlikeme..

Secondly,Asmentionedearlierinthisblogbyyouaboutthe
Coding

Inthissection,wewillredefinemethodsinDPC_EXTclass.Please
notethatcodeprovidedinthisblogisin
simplestform.Youmayneedtoconsiderpropererrorhandlingand
otherbestpracticeswhilewritingthecode.
canyoupleaseshareanydocumenttorefertoforthesame.

Thanks&BestRegards,
PavanGolesar
Like(0)

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

15/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN
YueqiangZhuNov6,20149:57AM

ThanksChandra,GreatBlog!
Like(1)

PrashanthKumarDec4,201412:17PM

HiChandra,

Ihaveonedoubtregardingtheasociation,takeoneexampleihaveonerootentityandtwoormore
childentitiesundertherootentity.

ButIdon'twanttogettheparentaswellastwoormorechilddetailsundersingleroofi.e.byusing
get_expanded_entity.Insteadineedtohandlethenavigationdynamicallymeansifiwantparentand
firstchilddetailsorparentandsecondchilddetailsindividually,thiscanbepossiblebyusingthe
relatedurl.Inthiscasemyparenthastwoseparatenavigationsbasedonnavigationprovidedinthe
urlineedtorespondaccordingly.

Thiscanbepossibleifiwillgetthenavigationnameinget_expanded_entitymethod.Butiamnot
gettingtheNavigationNameinsidethemethod.

Isthereanywaytogetthenavigationnameinsidetheget_expanded_entity,sothaticannavigate
betweenparentand1stchildorparentand2ndchild.

Pleasesuggest.

Thanksinadvance.

Regards,
PrashanthKumarB.
Like(0)

ErikHovenJan21,201511:35AM

Greatwork!!!!
Like(0)

AnjuJhaFeb12,20153:28PM

HiChandra,

Greatblog.

IamfacingissueinmyGatewayservice.MylineItemdataisdisplayed1standthanHeaderdata.
MyrequirementistogetHeaderdata1st.Iamusingexpandstatementforexpandinglineitem.

/sap/opu/odata/sap/Z_GW_ECC_PO_SRV/PoHeaderSet/?$expand=PoLineitmSet

Association,Navigationexist

Amidoingsomethingwrong?

Thanks,
Anju
Like(0)

HemendraSabharwalApr14,201512:02PM

NiceblogChandra,thanksforsharing.

WarmRegards
Hemendra
Like(1)

KrishnaChauhanMay11,20156:33PM(inresponsetoHemendraSabharwal)

Nicepieceofinfo.Thanksforsharing!!!
Regards,
KrishnaChauhan
Like(0)

haythemJaidiDec11,201510:56AM

Hi,

Thankyouforsharing.

Ihaveaquestion,asyousaidadoptingframeworkorDataproviderExpandwilldependonthe
performance.SoWhatIneedtomakeitdependingonaspecificscenariowherebothlogicdidexist.
ForsomeIwantthattheFrameworkhandletheExpandandotherIwantthattheGWusestheData
providerimplmentation.

HowcanIachievethat?

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

16/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN
BestRegards
H.JAIDI
Like(0)

RalfDucksteinMar2,20163:54PM

Hi,

Ithinkthereisamistakeintheexampleabove.Thecardinalityfromitemtoproductisnot1:1butN:1.

1:1wouldmeaneachproductisrelatedtoexactlyoneorderitem.
N:1meanscertainorderitemsmaypointtothesameproduct.

Ofcourseinbothcasesanorderitemalwayscanpointonlytooneproduct.

Solet'sassumeyouliketoselectallorderitemsforagivenproduct:
.../ProductSet('12345')/OrderItems
Ofcoursewhenyoudon'tprovideanavigationpropertyforproductthatlinkstotheorderitems,then
thecardinalityisnotimportant.AtleastsofarInoticednodifferencethen.
Like(0)

MadhusudhanMysoreVasudevaMar4,20167:19AM

HiChandra,

ThisisaawesomereferencematerialtostartlearningSAPGateway.ThanksalotandAppreciate
youreffortshere.

OnesmallqIhavetoyou:Pleaseletmeknowyourcomments,wheneveryougetachance.

WhenIamtryingtoDebugFrameworkExpandwithURI:
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000000')/OrderToItems(SoId='500000000',SoItemPos='0000000010')
Iamgettingerror"Resourcenotfoundforsegment'SalesOrderItem'".Pleasefindattachedsnap
shot
ForbelowURItowork
/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderSet('500000000')/OrderToItems(SoId='0500000000',SoItemPos='0000000010')

Weneedtoimplementlogicwithnavigationpropertykeys.

Alternativelywecanread
as/sap/opu/odata/sap/ZTEST_DP_EXPAND_SRV/SalesOrderItemSet(SoId='0500000000',SoItemPos='0000000010')

ButwhenIusealternativeURIwhichismentionedjustbelowaboveURI,thatworks.

CanwenotuseURIwhichwearegettingerror?

Thanksalotinadvance.

Regards,
MadhuMV
Like(0)

MadhusudhanMysoreVasudevaMar4,20167:20AM(inresponsetoMadhusudhanMysoreVasudeva)

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

17/18

9/14/2016

Let'scodeassociation/navigationanddataprov...|SCN

Like(0)

MadhusudhanMysoreVasudevaMar3,20162:08PM(inresponsetoMadhusudhanMysore
Vasudeva)

NowIgotitIneedtouseIT_NAVIGATION_PATHtogetbothSoIDand
SoItemPos..
Like(0)

ChakramGovindarajanAug2,20165:26PM

HiChandra,

Veryniceandusefulblog.Ihavebookmarkedthislinkformyfuturereference.
Ihaveaquestionaroundexpandentity.TheexpandentityworksforGet,howeverwhenIdoaPOST,
itisnotworking.Howdowecreatetheunderlyingassociationentities?
HowtocreateassociationentitiesduringPOST.TheURIwithexpandagainsttheparententityisnot
allowingmetocreatetheexpandentityrecord.Canyoupleaseadvise?

Thanks,
ChakramGovindarajan
Like(0)

SiteIndex
Privacy

ContactUs
TermsofUse

SAPHelpPortal
LegalDisclosure

Copyright

http://scn.sap.com/community/gateway/blog/2014/09/24/letscodeassociationnavigationanddataproviderexpandinodataservice

FollowSCN

18/18

Potrebbero piacerti anche