Search code examples
javajsonsap-commerce-cloudpopulateconverters

How to populate a custom attribute in ProdctData JSON?


I am trying to fetch product data based on some request criteria. When I am converting the productmodel to productdata using convertors and populators i am getting all the product data in response.

I had tried to set the string value while converting & populating the productmodel data to productdata but its not helpful!!

{
     "products": [
         {
           //Getting from Product Model
           "name" : "ABC"
           "desc" : "abcde"
           "Quantity": 2"

           //Not from Product Model
           "matcode" : "001100"
         },
     ]
 }

is it possible to set one more string value(String matcode ="ABC") inside the same response?


Solution

  • Ideally, if you set matcode(a attribute) in ProductData correctly it gets reflected in the response

    Decare matcode attribute inside ProducctData by declaring it in your *beans.xml, something like.

    <bean class="de.hybris.platform.commercefacades.product.data.ProductData">
        <!-- other attributes -->
        <property name="matcode" type="java.util.Set&lt;java.lang.String>"/>
    </bean>
    

    Now inside populator, set the matcode attribute value and you are done. Debug your controller and see whether you custom attribute value is there in product data. If it's there then it'll get converted to JSON correctly.

    @Override
    public void populate(final SOURCE productModel, final TARGET productData) throws ConversionException
    {
        //... other codes
        productData.setMatcode("001100"); // add your logic to set this value
    }