2022 Mar 07 1:12 PM
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
2022 Oct 12 9:46 AM
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.
2022 Mar 07 2:16 PM
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
2022 Mar 07 3:24 PM
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
2022 Mar 08 5:47 AM
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.
2022 Mar 08 11:33 AM
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.
regards
Hassin
2022 Mar 08 12:00 PM
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.
2022 Oct 12 9:46 AM
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.