CDS view with input parameters consumption via OData
I decided to write this blog after I spent a lot of time in figuring out how to consume the CDS view with input parameter via OData.Hope this helps
CDS view with input parameters require a special syntax when you are making a call via OData. The steps to be followed in the CDS View are:
1) Annotations: Odata.publish:true
2) If you have Date field as your input parameter with datatype as Dats ,it might create an issue so cast it to Char. You will get the following error in that case:
“In the context of the Data services an unknown internal server error occured”.
You might also get that error if you are projecting any fields with dats as the datatype and trying to consume in OData
I will now create a basic CDS view with input parameter.
@AbapCatalog.sqlViewName: 'ZICMCALDATECHNG'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'ZI_CM_CALENDAR_DATE'
@OData.publish: true
define view ZI_CM_CALENDAR_DATE with parameters
P_CutDate : char08,
P_cutofftype : zreport_type
as select from scal_tt_date
{
key cast (scal_tt_date.calendardate as abap.char( 08 )) as NewDate,
scal_tt_date.yearmonth as YearMonth,
scal_tt_date.yearquarter as YearQuarter,
scal_tt_date.yearweek as YearWeek
}
where scal_tt_date.calendardate = $parameters.P_CutDate
I am trying to get the calendar date,year month and week from the calendar table with input parameter as P_Cutdate with char08 as datatype.
Now you can proceed to gateway /n/IWFND/MAINT_SERVICE to activate your service.After activating your service the metadata should look like this:
Look closely in the metadata.
There are two entityset in the entitycontainer
1) ZI_CM_CALENDAR_DATESet
2) ZI_CM_CALENDAR_DATE
Take the second entityset and prepare your url
The url will look like this:
/sap/opu/odata/sap/ZI_CM_CALENDAR_DATE_CDS/ZI_CM_CALENDAR_DATE(P_CutDate='20170217',P_cutofftype='EOD')/Set
Remember to put the Set at the end. You can also get Results in the 1
st entity depending upon your service generation in the gateway. Then you have to type /Results after the url to get the data
The data will look like this:
Hope this helps