ODATA SERVICE SETUP+CRUDs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

ODATA SERVICE 1.

TABLE NAME = ZAS_DEPT

CONTENTS
CREATING THE SERVICE

TCODE= SEGW

CREATE A PROJECT, GIVE NAME AS "ZAS_CRUD1"

NOW IMPORT A TABLE, OR A DDIC STRUCTURE,


NOW SELECT ALL FIELDS,
SELECT THE KEY PARAMETER

CLICK ON FINISH,

NOW ALL THESE ARE CREATED,


ENTITY TYPE, PROPERTIES AND ENTITY SET IS
CREATED AUTOMATICALLY BY THE SYSTEM.

The name of the fields from the EntitySet has to be


taken from under the Properties.
The same names have to be used on the Abap
Code as well as in the
SAP Gateway Client URI Section.

Else, it will lead to error.

CLICK ON PROPERTIES,

CLICK ON SERVICE IMPLEMENTATION


THESE CAN BE USED FOR CRUD OPS

NOW CHECK AND CLICK ON GENERATE.

AND IT WILL GENERATE 6 CLASSES, ON WHICH 2


BASE CLASSES ARE THERE.

DPC AND MPC are Super classes,


Nothing can be done in these 2 classes at all.

DPC_EXT & MPC_EXT are derived classes,


If any thing is to be done, it can be done XXX_EXT
Classes (EXIT Classes)

Now register the service.

Can be done in 2 ways,,

Double click on GW_HUB and register,

Or go to SE93 and put this tcode=


/IWFND/MAINT_SERVICE
AND then execute or F8.
After it is done, click on SAP Gateway Client.

This screen will come

At the top, click on Add URI Option, select


metadata and then click on Execute,

A couple of file permissions will come out, allow


them (for the first time).

And an xml type data will be shown


Now to implement the methods, of the DPC_EXT
class,

Now go to se24 for this class =


ZCL_ZAS_CRUD1_DPC_EXT
scroll down to get these methods,

Select one and click on Redefine,


Now write code,

Fetch one record using get,

method ETEMPSET_GET_ENTITY.

data(LV_KEY_TAB) = VALUE #( IT_KEY_


TAB[ Name = 'empno' ]-
VALUE OPTIONAL ).

SELECT Single * from ZAS_DEPT into


CORRESPONDING FIELDS OF @ER_ENTITY
WHERE EMP_NO = @LV_KEY_TAB.

endmethod.

Save code.

Go to sap gateway client,


Select the entityset
Now give the key value, from the table
Like this,

And execute,
ERROR 1

Please be concerned about the data type


used in the records of the table.
The data in the URI entered in the URI
should match the data of the table.
Else, errors like this occurs when string is
used in case of int type,
Make sure the datatype is proper to the
request.

ERROR 2
Method not implemented,
If we go for a full entity set query or try to
read the entire table using GET,
As the ETEMPSET_GET_ENTITYSET from
this ZCL_ZAS_CRUD1_DPC_EXT isnt
implemented
Or it has no definition. So this request will
not work.
The method has to be implemented first,
then activated and then used.

Use json format,

The json format syntax can be found in Add URI


Options
Select $format=json or add ?$format=json at the
end, manually.

Or by adding ?$format=json at the end, of the URI


This is done for a single record.

1. Get whole table


For this we have to implement the get_entityset
method.

From here or in the se24 class builder


The code for the get entityset method is

method ETEMPSET_GET_ENTITYSET.

select * from ZAS_DEPT into CO


RRESPONDING FIELDS OF table ET_E
NTITYSET.

endmethod.

Now just give the name of the entity in the URI

And execute,
The table ZAS_DEPT has 10 records, all 10 records
are fetched.
Also in json format, if needed

Get the total no of records using the $count URI


option.
Selective URI options, using select
We can get one field from the table via the select,
like this,

The URI will be


/sap/opu/odata/SAP/ZAS_CRUD1_SRV/ETE
MPSet?$select=EmpName

This will only fetch the Employee name.

Get in JSON
/sap/opu/odata/SAP/ZAS_CRUD1_SRV/ETE
MPSet?$select=EmpName&$format=json
We can also get more than 1 field, like Employee
name and Employee Designation .

Just add the field name of the table, from the


Entity properties,

/sap/opu/odata/SAP/ZAS_CRUD1_SRV/ETE
MPSet?$select=EmpName,EmpDesig&$form
at=json
Now, write code for the CREATE Operation.

We have to use parameters in the code, via the


exporting parameter from the create_entity

So, go to the class


Redefine it , now,

We have to this type, for getting the data from the


payload and then inserting into the table.

For this, we have to match the structure,


So declare a data variable,

DATA: ls_employee TYPE ZCL_


Zas_crud1_mpc=>TS_ETEMP.

This type is available above, marked in green


Import the data,

IO_DATA_PROVIDER-
>READ_ENTRY_DATA( IMPORTING ES_
DATA = ls_employee ).

Ls_employee contains the data from the payload


now,

Insert this data to the table,


INSERT INTO ZAS_DEPT VALUES ls_e
mployee.

Return the structure, if needed to show on the


Response,

ER_ENTITY = ls_employee.

Now, activate the method,


Go to SAP gateway client,

This is 1 single Read record result from the table,


click on Use as Request

You will see a payload is created on the left hand


side window under HTTP Request,
And it matches, the format too.
Now we have to send this payload using the Post
HTTP Method, which will send this payload along
with the data and
Insert it into the table and also retrieve it to show
on the HTTP Response window, in default xml
format.

The data to be entered into the table,


Now click on Execute, and this is output

Go to the table and look for emp_no = 13


Update OPERATION.

We need to use the put HTTP Method operation


for this,

We have to use a payload same as for create ops.

First, write the definition of the


ETEMPSET_UPDATE_ENTITY
Redefine it with the code,

method ETEMPSET_UPDATE_ENTITY.

*finding the matching record


data(LV_KEY_TAB) = VALUE #( IT_KEY_T
AB[ NAME = 'EmpNo' ]-
VALUE OPTIONAL ).

*declaring the structure type


data: ls_empupdate type ZCL_ZAS_CRUD
1_MPC=>TS_ETEMP.

*importing from payload


IO_DATA_PROVIDER-
>READ_ENTRY_DATA( IMPORTING ES_DATA
= ls_empupdate ).

*updating
UPDATE ZAS_DEPT from ls_empupdate.

endmethod.

Key is required only when update is to be done


using any WHERE condition.

Now save and activate,


Go to SAP Gateway client.

We need to use the Put

And also pass the Key value, as generally required


for update, here key is the EmpId.
It will update the 14th record, with the data
below,
Now execute,

It will not show any output value.


Now, let us do Delete

Find the ETEMPSET_DELETE_ENTITY method

Redefine,

method ETEMPSET_DELETE_ENTITY.
data(del_employee) = value #(
IT_KEY_TAB[ NAME = 'EmpNo' ]-
VALUE OPTIONAL ).

DELETE FROM ZAS_DEPT WHERE emp


_no = @del_employee.

endmethod.

Activate, and go to SAP Gateway Client,

Pass the key parameter only, here EmpNo=2 will


be deleted from the table,

Go check the table


Record with emp_no = 2 is not there, which is
deleted.

Response status for CRUD ops

Get Entity on READ = 200


Get EntitySet on QUERY = 200
UPDATE Entity = 204
CREATE Entity = 201
DELETE Entity = 204

You might also like