cancel
Showing results for 
Search instead for 
Did you mean: 

Drop down with fixed values for a column in SAP CAP

Ahmedkhan29789
Participant
0 Kudos

Hello experts

I want to give drop-down with fixed values like "a,b,c" for a column in my CAP application, What is the best way to do it.
My schema looks like this, Suppose for column Promotion_Type, I want to give hardcoded values in drop without any entity set in my Fiori application based on this entity, how to do it?

Accepted Solutions (0)

Answers (2)

Answers (2)

SchiwekM
Advisor
Advisor

Hi Ahmed,

for fixed values in CAP it is recommended to use Codelists (capire - Common Types & Aspects (cloud.sap)).

An implementation example is available in the SAP Fiori elements Feature showcase. The unit of measure property has fixed values with a drop-down:

Schema of UoM CodeList: fiori-elements-feature-showcase/common.cds at main · SAP-samples/fiori-elements-feature-showcase · G...

Usage of UoM: fiori-elements-feature-showcase/schema.cds at main · SAP-samples/fiori-elements-feature-showcase · G...

DropDown annotation: fiori-elements-feature-showcase/value-helps.cds at main · SAP-samples/fiori-elements-feature-showcas...

To display the name instead of the code, please use the @Common.Text and @Common.TextArrangement annotation.

If you implement the code list, you have to use the foreign key in the LineItem or FieldGroup annotation, so instead of PROMOTION_TYPE it would be PROMOTION_TYPE_code (code is here the name of the key from the code list).

When the uom example is not fitting enough, you could also use the criticality codelist as a reference. For dependent fields with value helps, the country & region example in the Feature showcase would be the reference.

Best regards,

Marten

Ahmedkhan29789
Participant
0 Kudos

Thanks, smarten, Thanks for the detailed answer

what I conclude is, These below steps i need to perform in my schema.cds

1. I have to create one more entity for PROMOTION_TYPE

entity sap.common.PROMOTION_TYPE_List : CodeList {

key PROMOTION_TYPE_code : String(30);

};

2. In my main entity Promotions I need to replace the type of PROMOTION_TYPE field to new entity key field i.e. PROMOTION_TYPE_code

3. I need to annotate the entity which i can also do it in schema.cds file itself, Not sure if i have to give main entity as collection path value or "PROMOTION_TYPE_List "

Even after this I don't understand where i have to give my fixed values?

SchiwekM
Advisor
Advisor

Hi Ahmed,

1. For the code list, please just use code as the key name. PROMOTIONS_TYPE_code is then the foreign key in Promotions. And sap.common can be removed, as it is just the namespace in the Feature showcase for the UoM. So you would add this following part to your schema:

entity PromotionTypeList : CodeList {
key code : String(30) @Common.Text : name @Common.TextArrangement: #TextFirst;
}
type PromotionTypes : Association to one PromotionTypeList;

As a best practice, please use a type, in this case PromotionType, which is the association to the Code list and will be used later. It is also important to note, that if your code values are shorter, you can reduce the maximum amount of characters the String can have (currently 30) and that PromotionTypeList uses the code list aspect, so it has also the property name. The Common.Text and Common.TextArrangement annotations are important so that the name of your code is shown and not only the cryptic code. If you want to completely hide the code, use #TextOnly instead of #TextFirst.

One other prerequisite is, that you have in your schema in the first lines a using {} from '@sap/cds/common'. Please add to this sap.common.CodeList, so the aspect can be used.

using {
   sap,
   ...,
   managed,
   sap.common.CodeList
} from '@sap/cds/common';

After that you are referencing the created type as the property type of PROMOTIONS_TYPE:

entity Promotions : managed {
   key ID : Integer;
STATUS : String;
PROMOTION_TYPE : PromotionTypes; //PromotionTypes is hiding the Association to one -> PROMOTION_TYPE_code is the foreign key ... }

With that you can add the needed UI annotations:

annotate Promotions with {
   PROMOTION_TYPE @Common.ValueListWithFixedValues : true @Common.Text: PROMOTION_TYPE.name @Common.TextArrangement: #TextFirst;
}

You should not need any more UI annotations in the default case, because the CodeList aspect used for your PromotionTypeList automatically adds the ValueList annotation and exposes the PromotionTypeList in your service, if it is used.

Common.ValueListWithFixedValues is the annotation which leads to the dropdown. The Common.Text and Common.TextArrangement annotations are here again used, so that the name of the type is shown and not only the cryptic code.

In your UI.LineItem annotation, you then use instead of only PROMOTION_TYPE, PROMOTION_TYPE_code:

{
$TYPE: 'UI.DataField',
Value: PROMOTION_TYPE_code,
}

Regarding your question where to insert the values:

Please create a folder in your db folder called "data" and create a csv file in it called <your namespace>-PromotionTypeList.csv. I do not know your namespace used in the schema, please look it up (it is the namespace <your namespace> codeline) and replace the placeholder <your namespace>. For example in the feature showcase the Countries are in the namespace sap.common, so the csv file is named: "sap.common-Countries.csv".

In the csv file then use the first line for the property names, each row afterwards is a new entity.

code;name
A;Promotions type A
B;Promotions type B
C;Promotions type C

CAP automatically fetches the csv files from the data folder and inserts the data into the correct entities, when the files are named correctly. However please be aware, that in the default case, these files are also part of the deployment and would overwrite existing entries on your db, so please only use them for fixed Codelists.

I hope that helps. If you are new to CAP, I also would like to point out the openSAP CAP course (Syllabus | Building Applications with SAP Cloud Application) which is a great starting point for newcomers.

Best regards,

Marten

aditi01
Advisor
Advisor
0 Kudos

Hi smarten

Is it also possible to show "All" in the default state in the dropdown?
In my case the dropdown is under the compact filters of Analytics List Page. I would like to display "All" in the dropdown, the first time the page is loaded.

Thanks!

SchiwekM
Advisor
Advisor
0 Kudos

Hi Aditi,

you would have to add All as an entity to the entity set for selection.

Default values itself can be added via @Common.FilterDefaultValue

BR, Marten

pramodu
Active Participant
0 Kudos

Hello,

I am not CAP expert 🙂 but wondering, one option can be create user defined annotation and then annotate the field. User defined annotation

User-Defined CDS Annotations - SAP Help Portal

Thanks,

Pramod

Ahmedkhan29789
Participant
0 Kudos

Hi pramodkumar.upadhyay2,

Thanks for looking into it, I am quite confused from this example which you shared as none of the field from the entity is used for creating user defined annotation, Also its not clear what annotation will be used to make it a drop down, If possible can you share one example please