CDS with Optional Parameters

Index

In the previous Tutorial, we saw how a CDS view can be made input enabled. This opens a new set of Code Push Down possibilities in AS ABAP.

SAP provides a limited way(till release 7.51) for CDS view to accept optional parameters. Optional parameters here mean that parameters are optional in nature during CDS consumption. This is possible with Environment System fields made available by CDS framework.

Let’s directly go to an example and check how to get this done!

CDS Source Code:

@AbapCatalog.sqlViewName: ‘ZTEST_CDS123’

@AbapCatalog.compiler.compareFilter: true

@AccessControl.authorizationCheck: #CHECK

@EndUserText.label: ‘Example optional Input in CDS…’

// …Using Environment System field

// Use ofsuch optional parameter in CDS is limited

define view z_cds_default_input

  with parameters

  @Environment.systemField: #CLIENT

  p1 : mandt,

  p2 : uname @<Environment.systemField: #USER,

  p3 : erdat @<Environment.systemField: #SYSTEM_DATE

as select from vbak as a

{

  :p1 as Client,

  :p2 as Ordered_By, // a.bname

  :p3 as OrderDate// a.erdat

  a.vbeln as OrderNumber

}

where

  a.mandt = :p1     // sy-mandt

  and a.bname = :p2 // sy-uname

  and a.erdat = :p3 // sy-datum


ABAP Source code to consume CDS:

REPORT zcds_optinal_input.

 

” Use case 1

SELECT *

FROM z_cds_default_input

INTO TABLE @DATA(lt_data1).

 

” Use case 2

SELECT *

FROM z_cds_default_input( ” p1 = @symandt – NOT ALLOWED

                          p2 = @syuname )

INTO TABLE @DATA(lt_data2).

 

” Use case 3

SELECT *

FROM z_cds_default_input( p2 = @syuname ,

                          p3 = @sydatum )

INTO TABLE @DATA(lt_data3).


Conclusion:

Limited System fields can be made optional in a CDS view or Table function. This can be achieved only with Environment System fields with two syntax possibilities.

Check the code by yourself for both CDS and ABAP report on GitHub.

Index

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s