Skip to Content
Author's profile photo Prabuddha Raj

CDS View with input parameters consumption via OData

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 1st 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

Assigned Tags

      17 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Awesome.  Thanks.  It helps !!!

      Author's profile photo Ruter Williams
      Ruter Williams

      Hi Guys,

      Thanks for the valuable information.I tried providing the parameters with /Set but getting the error in O Data feed.

      The URL i provided is

      /sap/opu/odata/sap/C_SALESANALYTICSQRY_1_CDS/C_SALESANALYTICSQRY_1(P_ExchangeRateType='M',P_DisplayCurrency='USD')/Set

      Iam getting error 400 bad request.

      Here the source is CDS View .Please correct the URL if its not correct.

       

      Regards,

      Pradeep

      Author's profile photo Ajith Cheruvally
      Ajith Cheruvally

      Thanks

      Author's profile photo Dayakar Padibandla
      Dayakar Padibandla

      Nice blog..

       

      Thank you.

      Author's profile photo Vladislav Shchur
      Vladislav Shchur

      Thanks man, you made my day! 🙂

      Author's profile photo Sebastiano Marchesini
      Sebastiano Marchesini

      Thank you for blog ! ?

      Author's profile photo Ramprabhu Sukumaran
      Ramprabhu Sukumaran

      Thanks for sharing the knowledge

      Author's profile photo Vijay Chintarlapalli
      Vijay Chintarlapalli

      Hello Prabuddha Raj ,

      Thanks for sharing Very Informative But how to get the results alone with /Set I see the result along with the other details. .As per below :

      I tried "Remember to put the Set at the end. You can also get Results in the 1st entity depending upon your service generation in the gateway. Then you have to type /Results after the url to get the data".

      I tried with the /Results but it does not work getting an error message

      "The request URI is not valid. Since the segment 'Set' refers to a collection, this must be the last segment in the request URI. All intermediate segments must refer to a single resource."

      Is there any other way to get the results alone as I have to read this service in ABAP using Http classes.

      Author's profile photo Prabuddha Raj
      Prabuddha Raj
      Blog Post Author

      HI Vijaya,
      Whatever is there in the entitycontainer, you should prepare your URL according to that. I dont understand your statement: "how to get the results alone with /Set". you will get the full entity when you use /Set or /Results: depends on your gateway settings which term to use. open the sap gateway client and see the entity sets generated in the metadata of the service.

      regards,
      Prabuddha Raj

      Author's profile photo Jitendra Soni
      Jitendra Soni

      Hello Prabuddha Raj

      Could you please explain why we need to /Set in below url ? I tried without Set and it is giving error.

      /sap/opu/odata/sap/ZI_CM_CALENDAR_DATE_CDS/ZI_CM_CALENDAR_DATE(P_CutDate=’20170217′,P_cutofftype=’EOD’)/Set

      Author's profile photo Prabuddha Raj
      Prabuddha Raj
      Blog Post Author

      Hi jitendra,
      You need /Set as it is part of the entityset in the cds view. This /Set is determined through your gateway settings and how the service is generated out of the CDS view. So it is a standard way to call the genrated service out of a CDS view

      Author's profile photo Christer Hedstrom
      Christer Hedstrom

      Hi,

      I have this "..sap/opu/odata/sap/YY1_TEST_CDS/YY1_TEST(P_StartDate=2012-10-10T12:34:56,P_EndDate=2019-10-10T12:34:56)/Set" and the dates are of type Edm.DateTime. Is the date syntax correct? Is the overall syntax correct?

      I get, when run with Postman: "Malformed URI literal syntax"

      I have this:

      <EntityType Name="YY1_TESTParameters" sap:semantics="parameters" sap:content-version="1">
      <Key>
      <PropertyRef Name="P_StartDate"/>
      <PropertyRef Name="P_EndDate"/>
      </Key>

      ....

      And

      <EntitySet Name="YY1_TEST" EntityType="YY1_TEST_CDS.YY1_TESTParameters"

      Author's profile photo Jyotiprakash Singh
      Jyotiprakash Singh

      try

      (P_FromDate=datetime'2020-09-10T00:00:00',P_TODATE=datetime'2020-09-10T00:00:00')/Results

      Author's profile photo Christer Hedstrom
      Christer Hedstrom

      Hi,

      I'm testing at sandbox two with "https://gdm-es-sandb-two-api.demo.hybris.com/sap/opu/odata/sap/YY1_CH_SIMPLE_TEST_CDS" but I don't know how to perform the below:

      "Now you can proceed to gateway /n/IWFND/MAINT_SERVICE to activate your service.".

      Where do I activate the service in my case?

      Author's profile photo Mridulika Mittal
      Mridulika Mittal

      This article is such a gem! Thankyou so much!

      Author's profile photo JOHNNY MUNOZ
      JOHNNY MUNOZ

      Hi Prabuddha Raj

      Nice blog, thx. I implemented the gateway and the service works, but one of the URI's doesn't work, What do I need to configure?

      /sap/opu/odata/sap/ZCDS_BPDATA01_CDS/ZCDS_BPDATA01(rut='19722304-9')/Set - Works

      /sap/opu/odata/sap/ZCDS_BPDATA01Set(rut='19722304-9',partner='1006673208') - Don't works.

      Best regards,

      Johnny

      Author's profile photo Prabuddha Raj
      Prabuddha Raj
      Blog Post Author

      Hi Johnny,
      it depends on the metadata of the service. if the gateway is implemented with ZCDS_BPDATA01Set then you have to use /Set . in somecases the enitityset in the DPC classes is generated as ZCDS_BPDATA01Results, then you have to use /Results to get the data .
      regards

      Prabuddha Raj