lunes, 12 de junio de 2017

Hybris: How to set default values to new mandatory attributes on types with existing information

Hi guys, 

Many times we have requirements to create a new mandatory attribute for an existing type. Most of the time we have already existing information for that type. And, how can we update this new mandatory attribute for those existing rows?

Well, we can do it easily with this Impex script : 

UPDATE Title[batchmode=true,impex.legacy.mode=true]; 
code[unique=true,allownull=true];code
;;DefVal

Where in this case, there are some codes with null values for Title type and we want to update them with a default value 'DefVal' but, let's go in deep and see how it works.

First, we are using two modifiers 'batchmode' and 'impex.legacy.mode'. The first one lets us update more than on row with one Impex line, in this case, we want to update all rows when the code is null to a default value. And the second one let us use the 'Allownull' modifier at attribute level. 

Next, we need to tell hybris what is the base attribute to perform the update, I mean the where clause. It is identified with the unique modifier, and, cause it is mandatory and for that reason it don't accept null we need to add the allownull modifier. But way we need it null allower? In hybris if you don't specified a value for a column explicitly hybris set it as null cause this is the default value and as we want to find all null codes we need this modifier. 

Finally, we need an additional column 'code' to set the value that is going to replace all null field with our default value

Tell me if this article was useful for you, or if you have comments about it.