Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CDS with optional parameters

hassin
Explorer
0 Kudos
6,056

Hallo,

I'm using a CDS View (mara) with parameters.

I want to make the parameters optional and not mandatory.

I have read, that I have to give the parameters a default value to solve this problem.

How can I input the the needed syntax in the code below?

regards

Hassin

@AbapCatalog.sqlViewName: 'ZCS_MARA_PARA'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'VDS View with Parameter'


define view ZCS_MARA_Test1_PARA
    with parameters 
        p_matnr : matnr, 
        p_ernam : ernam  
as select from ZCP_MARAView_Test1 


{
    //ZCP_MARAView_Test1
    
    matnr               as Materialnummer,
    ersda               as Angelegt_Am,
    ernam               as Angelegt_Von,
    laeda               as Geaedert_Am,
    
    case 
    when aenam = 'STZI' then 'S. Zinn'
    else aenam               
    end as Geaendert_Von
    
}
where matnr = :p_matnr
or    ernam = :p_ernam
1 ACCEPTED SOLUTION

hassin
Explorer
0 Kudos
4,997

As per current design, you are not able to make any of the CDS input parameter as optional

expect few like date, user and client.

6 REPLIES 6

subhajit
Active Participant
0 Kudos
4,997

Hello,

You can use annotation @Consumption.filter.mandatory: True for this.

More in this blog https://blogs.sap.com/2021/06/03/abap-cds-views-working-with-optional-parameters/

You also refer to the below blog for another way

https://blogs.sap.com/2018/01/06/optional-parameters-in-cds-views/

Thanks

0 Kudos
4,997

Hallo,

thanks for the links!

I tried both option and it didn't work.

I added following annotation:

@Consumption.filter.mandatory:false

@Consumption.filter.selectionType: #SINGLE

Without a positive result.

And in the second link you can coos the default values for the fields

#CLIENT, #SYSTEM_DATE, #SYSTEM_LANGUAGE, #SYSTEM_TIME and #USER.

I don't have those fields.

Regards

Hassin

keremkoseoglu
Contributor
0 Kudos
4,997

This quick & dirty solution may work for you.

First, create a CDS without parameters.

define view ZCS_MARA_Test1_Vanilla
as select from ZCP_MARAView_Test1 
{
    matnr               as Materialnummer,
    ersda               as Angelegt_Am,
    ernam               as Angelegt_Von,
    laeda               as Geaedert_Am,
    
    case 
    when aenam = 'STZI' then 'S. Zinn'
    else aenam               
    end as Geaendert_Von
}

Then, make your CDS with parameters read data from that one.

define view ZCS_MARA_Test1_PARA
    with parameters 
        p_matnr : matnr, 
        p_ernam : ernam  
as select from ZCS_MARA_Test1_Vanilla
{
    Materialnummer,
Angelegt_Am,
Angelent_Von,
Geaedert_Am,
Geaedert_Von
}
where Materialnummer = :p_matnr or Angelent_Von = :p_ernam

When you need to read without parameters, access ZCS_MARA_TEST1_VANILLA. When you need to read with parameters, access ZCS_MARA_TEST1_PARA.

0 Kudos
4,997

Hallo,

thanks for the solution!

My CDS View are build in a similar structure.

I have a basic view, a composite view and a consumption view.

In the last view (consumption) I use the parameters as you told.

The problem hear is, that you have to enter something for both parameters to revive a result / move forward.

I want to have the possibility to enter one parameter and receive an result.

image.png

regards

Hassin

0 Kudos
4,997

hassin in that case, I suppose you should steer towards AMDP methods. You can import (optional) parameters or even ranges to an AMDP method, and execute a query accordingly.

hassin
Explorer
0 Kudos
4,998

As per current design, you are not able to make any of the CDS input parameter as optional

expect few like date, user and client.