Need of Initial Modifier in SAP Commerce(Hybris) Cloud 2211
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 modifiedwrite="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=falseb)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