What is the enum type, How to use and define in Hybris ?

What is the enum in Hybris ?

SAP Commerce enables us to use attributes with pre-defined values, called enumeration. Enum types are used for defining constant values. but Dynamic in Enum is different from Dynamic attributes, can add dynamic value at run time for dynamic Enum type.

Type of enum and How to define in item.xml?

There are two type of enum type Static and Dynamic in Hybris 

A. Declare the static enum type

<enumtype code="Color" autocreate="true" generate="true">
            <value code="BLACK"/>
            <value code="RED"/>
</enumtype>

B. Declare the Dynamic enum type 

<enumtype code="Color" autocreate="true" generate="true">
            <value code="BLACK"/>
            <value code="RED"/>
</enumtype>

Need to add autocreate and generate as true to generate the Enum type and Enum class. and "dynamic=true" to generate the dynamic enum type.

How to use enum in hybris code?

We can use the enum in code as per below. 

ProductModel productModel = new ProductModel();
product.setColor(Color.BLACK);

How to set the value for dynamic enum. we can use script to add new Color type.

import de.hybris.platform.core.model.enumeration.EnumerationValueModel;
 
 EnumerationValueModel model = (EnumerationValueModel)modelService.create("TestForEnum");
 model.setCode("WHITE");
 model.setName("White");
 modelService.save(model);

Query to get detail for particular enum 

SELECT * FROM {enumerationvalue} WHERE {code}='White'

Leave a Comment

Your email address will not be published. Required fields are marked *