Need of Initial Modifier in SAP Commerce(Hybris) Cloud 2211

Initial Modifer in item-type is important because it allows us to control over the behaviour of fields during life cycle of item(like in creation time). In this Post we are going to explore initial modifier in detail.

What is the initial Modifier

Attribute : initial If 'true', the attribute will only be writable during the item creation. Setting this to 'true' is only useful in combination with write='false'. Default is 'false'.

Data Type : boolean
Enumerated Values :
- true
- false

When we should go for initial=true

Attributes like unique codes or Ids which should never be change/modify after being creation.

Note : This initial=true Modifier will work with combination of write="false" ,then only the field is immutable after the item is created

initial="true" it specifies the attribute can be set only onces,during creation of item type after item is created ,its cannot be modified
write="false" it specifies the attribute is not writable onces the item is saved in database.
This is how initial=true modifier make attribute immutable after the creation

Below is an example of an item type using intial=true

Important points from above example

a)storeId qualifier is defined with intial=true and write=false
b)It can be set only during creation of SampleB2CStore
c)After item is created ,the field storeId become read only i.e immutable

After creating an SampleB2CStore an item type in samplecore-items.xml , do ant clean all ,make your hybris server up and update Hac.
Let us test initial=true modifier in our item type ,we can add storeId through Backoffice or Impex
Currently Im adding value through Impex

INSERT_UPDATE SampleB2CStore ; storeId[unique=true]; storeName ; storeLocation ; storeOwner ; storeGstNumber
; PS001 ; ABCSTORE ; Hyderabad ; DummyOwner ; GST001002
After importing impex ,run flexible query select * from {sampleb2cstore} ,we can see our just imported values
Now try to Update storeId PS001 --> PS0011 keeping other values same but we get error no existing item found for updat
Open BackOffice -->go to Types-->Search For Sampleb2cStore
Here in below screen shot we can see that storeId is immutable after creation but we can edit other attributes like GstNumber,storeLocation,storeOwner and storeOwner
Note:- we cannot modify storeId after creation

Advantages of using initial modifier in Sap Commerce(hybris)
--> Useful for maintaining Data Integrity for critical attributes like unique codes or primarykeys etc
--> Cleaner Code logic

Important Points
--> If Both initial=true and write=true are used then it is editable during creation and after creation of type. We can change value after creation because write=true will overide this behaviour

-->Both together initial=true and write=true ,attributes will be editable at all the times because write=true takes higher precedence

For example like below we have UserRight item type with qualifier code where initail=true and write=true.

<itemtype code="UserRight" extends="GenericItem" jaloclass="de.hybris.platform.jalo.security.UserRight" autocreate="true" generate="true">
    <deployment table="UserRights" typecode="29"/>
    <attributes>
        <attribute autocreate="true" qualifier="code" type="java.lang.String">
            <persistence type="property"/>
            <modifiers read="true" write="true" search="true" initial="true" optional="false" unique="true"/>
            <custom-properties>
                <property name="hmcIndexField">
                    <value>"thefield"</value>
                </property>
            </custom-properties>
        </attribute>
        <attribute autocreate="true" qualifier="name" type="localized:java.lang.String">
            <modifiers read="true" write="true" search="true" removable="true" optional="true"/>
            <persistence type="property"/>
            <custom-properties>
                <property name="hmcIndexField">
                    <value>"thefield"</value>
                </property>
            </custom-properties>
        </attribute>
    </attributes>
</itemtype>


Comments

Ayushi said…
Very knowledgeable creative concept.
Anonymous said…
Great job ,please continue same
Anonymous said…
Useful content
Anonymous said…
Good work ,keep it up
Anonymous said…
Superb work bro,very helpful.
Anonymous said…
That's interesting
Anonymous said…
Excellent

Popular posts from this blog

Latest SAP Commerce (Hybris) Interview Questions

Steps to Install SAP Commerce Cloud 2211/Install SAP Hybris 2105 to 2211

OCC Webservices in SAP Commerce(hybris) 2211 using YOCC template – Part 1