How to create ODADA service with association in SAP ABAP.
ODATA service creation details.
Go to T-code:- SEGW
Create new
project.
Insert project name,
description and package name.
The name of the Project:
NGS_104_CAMP and the Description: Campaign Project.
After creating
the project right click on data model and import DDIC structure.
We have uploaded
two structures here which were created in se11.
First is
zcampcc.
Second is
zcustcc.
Repeat the same
process for zcustcc.
By selecting the
checkbox Create default entity set, it will automatically create the entity
set.
As shown below
after importing the structure entity sets zcampccset and zcustccset are
created.
The fields in
zcampcc are as shown below.
Here the field
column shows the name of the components in structure and the name column
indicates the name in odata.
The fields of
zcustcc are as shown below.
After uploading
the structure.
Check the project consistency and generate runtime time
objects(red color ball like icon).
If It shows warnings neglect that.
Go
to service maintains, select the server which you are working with:
Select
the register service button.
Enter
the name of the package and select next.
Your
project has been registered.
Go to runtime an
artifact which contains the data provider and model provider class.
Here
ZCL_ZNGS_104_CAMP_DPC_EXT is the data provider class.
And
ZCL_ZNGS_104_CAMP_MPC_EXT is the model provider class.
Right click on
the data provider class and select go to Abap workbench.
Go to inherited
methods and you will see every uploaded structure will have five in build
methods in inherited methods which are: ZCAMPCCSET_CREATE_ENTITY,
ZCAMPCCSET_DELETE_ENTITY, ZCAMPCCSET_GET_ENTITY, ZCAMPCCSET_GET_ENTITYSET,
ZCAMPCCSET_UPDATE_ENTITY and same would
be for zcustcc.
Right click on
ZCAMPCCSET_GET_ENTITYSET and click redefinition.
Now this method
will be shown in redefinition folder.
Now copy paste
the below code in each method:
Activate each method after copying the
code.
·
Zcamp_get _entityset.
METHOD ZCAMPCCSET_GET_ENTITYSET.
DATA: IT_CAMP TYPE TABLE OF ZCAMPCC,
WA_CAMP LIKE LINE OF IT_CAMP,
WA_ENTITY LIKE LINE OF ET_ENTITYSET.
*Get data from ZCAMPAIGN_DEMO table
SELECT * FROM ZCAMPCC INTO TABLE IT_CAMP.
*FILL ET_ENTITYSET
LOOP AT IT_CAMP INTO WA_CAMP.
MOVE-CORRESPONDING WA_CAMP TO WA_ENTITY.
APPEND WA_ENTITY TO ET_ENTITYSET.
ENDLOOP.
ENDMETHOD.
·
Zcamp_get_entity.
METHOD ZCAMPCCSET_GET_ENTITY.
DATA: LS_KEY_TAB LIKE LINE OF IT_KEY_TAB,"TYPE /IWBEP/T_MGW_NAME_VALUE_PAIR,
LV_ID TYPE ZCAMPCC-C_ID,
WA_COMP TYPE ZCAMPCC.
*Get the key property values
READ TABLE IT_KEY_TAB WITH KEY NAME = 'CId' INTO LS_KEY_TAB.
LV_ID = LS_KEY_TAB-VALUE.
*Get the single record from ZCAMPAIGN_DEMO and fill er_entity
SELECT SINGLE * FROM ZCAMPCC INTO WA_COMP WHERE C_ID = LV_ID.
IF SY-SUBRC = 0.
MOVE-CORRESPONDING WA_COMP TO ER_ENTITY.
ENDIF.
·
Zcamp_create_entity
METHOD ZCAMPCCSET_CREATE_ENTITY.
DATA: LS_REQUEST_INPUT_DATA TYPE ZCL_ZNGS_104_CAMP_MPC=>TS_ZCAMPCC,
WA_COMP TYPE ZCAMPCC.
* Read Request Data
IO_DATA_PROVIDER->READ_ENTRY_DATA( IMPORTING ES_DATA = LS_REQUEST_INPUT_DATA ).
* Fill workarea to be inserted
MOVE-CORRESPONDING LS_REQUEST_INPUT_DATA TO WA_COMP.
* Insert Data in table ZUSERINFO
INSERT ZCAMPCC FROM WA_COMP.
IF SY-SUBRC = 0.
ER_ENTITY = LS_REQUEST_INPUT_DATA. "Fill Exporting parameter ER_ENTITY
ENDIF.
·
Zcamp_update_entity
DATA: LS_REQUEST_INPUT_DATA TYPE ZCL_ZNGS_104_CAMP_MPC=>TS_ZCAMPCC,
LS_KEY_TAB LIKE LINE OF IT_KEY_TAB,
LV_ID TYPE ZCAMPCC-C_ID,
WA_COMP TYPE ZCAMPCC.
* Get key values
READ TABLE IT_KEY_TAB WITH KEY NAME = 'CId' INTO LS_KEY_TAB.
LV_ID = LS_KEY_TAB-VALUE.
IF LV_ID IS NOT INITIAL.
* Read request data
IO_DATA_PROVIDER->READ_ENTRY_DATA( IMPORTING ES_DATA = LS_REQUEST_INPUT_DATA ).
* Update fields of table ZUSERINFO
UPDATE ZCAMPCC SET C_NAME = LS_REQUEST_INPUT_DATA-C_NAME
C_TYPE = LS_REQUEST_INPUT_DATA-C_TYPE
C_GRP = LS_REQUEST_INPUT_DATA-C_GRP
C_TCUST = LS_REQUEST_INPUT_DATA-C_TCUST
C_EXE = 'X'""LS_REQUEST_INPUT_DATA-C_EXE'
WHERE C_ID = LV_ID.
IF SYSUBRC = 0.
ER_ENTITY = LS_REQUEST_INPUT_DATA. "Fill exporting parameter ER_
else.
er_entity-c_err = 'Error in Update'.
ENDIF.
·
Zcamp_delete_entity.
METHOD ZCAMPCCSET_DELETE_ENTITY.
DATA: LS_KEY_TAB LIKE LINE OF IT_KEY_TAB,
LV_ID TYPE ZCAMPCC-C_ID.
* Read key values
READ TABLE IT_KEY_TAB INTO LS_KEY_TAB WITH KEY NAME = 'CId'.
LV_ID = LS_KEY_TAB-VALUE.
IF LV_ID IS NOT INITIAL.
* Delete record from table ZUSERINFO
DELETE FROM ZCAMPCC WHERE C_ID = LV_ID.
ENDIF.
ENDMETHOD.
Zcust_get_entityset.
DATA: IT_COMP TYPE TABLE OF ZCUSTCC,
WA_COMP LIKE LINE OF IT_COMP,
WA_ENTITY LIKE LINE OF ET_ENTITYSET.
*Get data from ZCAMPAIGN_DEMO table
SELECT * FROM ZCUSTCC INTO TABLE IT_COMP.
*FILL ET_ENTITYSET
LOOP AT IT_COMP INTO WA_COMP.
WA_ENTITY-CUST_ID = WA_COMP-CUST_ID.
WA_ENTITY-CUST_NAME = WA_COMP-CUST_NAME.
WA_ENTITY-CUST_EID = WA_COMP-CUST_EID.
WA_ENTITY-C_GRP = WA_COMP-C_GRP.
WA_ENTITY-C_TYPE = WA_COMP-C_TYPE.
APPEND WA_ENTITY TO ET_ENTITYSET.
ENDLOOP.
Zcust_get_entity.
DATA: LS_KEY_TAB LIKE LINE OF IT_KEY_TAB,"TYPE /IWBEP/T_MGW_NAME_VALUE_PAIR,
LV_ID TYPE ZCUSTCC-CUST_ID,
WA_COMP TYPE ZCUSTCC.
*Get the key property values
READ TABLE IT_KEY_TAB WITH KEY NAME = 'CustId' INTO LS_KEY_TAB.
LV_ID = LS_KEY_TAB-VALUE.
*Get the single record from ZCAMPAIGN_DEMO and fill er_entity
SELECT SINGLE * FROM ZCUSTCC INTO WA_COMP WHERE CUST_ID = LV_ID.
IF SY-SUBRC = 0.
MOVE-CORRESPONDING WA_COMP TO ER_ENTITY.
ENDIF.
Zcust_create_entity.
DATA: LS_REQUEST_INPUT_DATA TYPE ZCL_ZNGS_104_CAMP_MPC=>TS_ZCUSTCC,
WA_COMP TYPE ZCUSTCC.
* Read Request Data
IO_DATA_PROVIDER->READ_ENTRY_DATA( IMPORTING ES_DATA = LS_REQUEST_INPUT_DATA ).
* Fill workarea to be inserted
MOVE-CORRESPONDING LS_REQUEST_INPUT_DATA TO WA_COMP.
* Insert Data in table ZUSERINFO
INSERT ZCUSTCC FROM WA_COMP.
IF SY-SUBRC = 0.
ER_ENTITY = LS_REQUEST_INPUT_DATA. "Fill Exporting parameter ER_ENTITY
ENDIF.
Zcust_update_entity.
DATA: LS_REQUEST_INPUT_DATA TYPE ZCL_ZNGS_104_CAMP_MPC=>TS_ZCUSTCC ,
LS_KEY_TAB LIKE LINE OF IT_KEY_TAB,
LV_ID TYPE ZCUSTCC-CUST_ID,
WA_COMP TYPE ZCUSTCC.
* Get key values
READ TABLE IT_KEY_TAB WITH KEY NAME = 'CustId' INTO LS_KEY_TAB.
LV_ID = LS_KEY_TAB-VALUE.
IF LV_ID IS NOT INITIAL.
* Read request data
IO_DATA_PROVIDER->READ_ENTRY_DATA( IMPORTING ES_DATA = LS_REQUEST_INPUT_DATA ).
* Update fields of table ZUSERINFO
UPDATE ZCUSTCC SET CUST_ID = LS_REQUEST_INPUT_DATA-CUST_ID
CUST_NAME = LS_REQUEST_INPUT_DATA-CUST_NAME
CUST_EID = LS_REQUEST_INPUT_DATA-CUST_EID
C_TYPE = LS_REQUEST_INPUT_DATA-C_TYPE
C_GRP = LS_REQUEST_INPUT_DATA-C_GRP
WHERE CUST_ID = LV_ID.
IF SY-SUBRC = 0.
ER_ENTITY = LS_REQUEST_INPUT_DATA. "Fill exporting parameter ER_ENTITY
ENDIF.
ENDIF.
Zcust_delete_entity.
DATA: LS_KEY_TAB LIKE LINE OF IT_KEY_TAB,
LV_ID TYPE ZCUSTCC-CUST_ID.
* Read key values
READ TABLE IT_KEY_TAB INTO LS_KEY_TAB WITH KEY NAME = 'CustId'.
LV_ID = LS_KEY_TAB-VALUE.
IF LV_ID IS NOT INITIAL.
* Delete record from table ZUSERINFO
DELETE FROM ZCUSTCC WHERE CUST_ID = LV_ID.
ENDIF.
For
Creating association :
Go
to association folder and right click and click create.
Fill
the following details as shown below.
If
your structure has same key fields then select the principal key and dependent
proprety as same field.
Click
next.
Select
Finish.
To
run this project go to gateway client.
The
Below URI will call Zcampccset_get_entityset method.
/sap/opu/odata/SAP/ZNGS_104_CAMP_SRV/zcampccSet
/sap/opu/odata/SAP/ZNGS_104_CAMP_SRV/zcustccSet
_________________________________________________________________________________
/sap/opu/odata/SAP/ZNGS_104_CAMP_SRV/zcampccSet?$expand=camptocustrel
The
above uri is for association.
The
Below image shows the final tree of the project ZNgs_104_camp.