Skip to Content
Technical Articles
Author's profile photo deepthi polipilli

Groovy script for changing the existing attribute type to new attribute type

Hi all,

In this blog,

I would like to share the Technic, which is useful for changing any item type attributes values into the required attribute values.

In my case, I have requirement like:

I have a customer item type in hybris having an attribute of type Boolean -stores the data like,

from which form the customer is coming from.

For an example:

  1. From registration form
  2. From Login form etc.,

In items.xml file,

So the above attributes of type Boolean is looks like below in hmc:

Now, I want to change all these boolean attribute values into a dropdown values.

For that I have defined an enum type in items.xml file.

In the above image I have a code as “SourceStatus”.

Now I want to display these values under customer item type. So, I defined an attribute having the type name as enum code.

And I want to see these enum values in hmc as a Source.

So, I have given the qualifier as “Source”.

In hmc,

Note:

After adding new attributes in items.xml file, do ant clean all, server up and update running system.

So, I have added the required attributes. Now, the main requirement is to change the existing customer having Boolean values into drop down values.

For that I have written a groovy script.

/**
 * @author deepthip
 *
 */

import de.hybris.platform.cronjob.enums.*
import de.hybris.platform.servicelayer.cronjob.PerformResult
import de.hybris.platform.servicelayer.search.*
import de.hybris.platform.servicelayer.model.*
import de.hybris.platform.core.model.user.CustomerModel;
import com.core.enums.SourceStatus;



flexibleSearchService = spring.getBean("flexibleSearchService")
modelService = spring.getBean("modelService")
query = "SELECT {item.PK} FROM {Customer AS item} WHERE {item.fromconfigregistration}= 1 or {item.fromdriveform}= 1 or {item.fromnewsletterform}= 1 or {item.fromcontactusform}= 1 ";
result = flexibleSearchService.search(query);
itempk = "";

for (item in result.getResult()) {
println item.getFromnewsletterform();
println item.getFromcontactusform();
println item.getFromconfigregistration();
println item.getFromdriveform();


if (item.getFromnewsletterform() == true)

{
item.getSource();
println item.getSource();
item.setSource(SourceStatus.WEBSITE_NEWSLETTER);
item.setFromnewsletterform(false);
println item; 
}

if (item.getFromcontactusform() == true)

{
item.getSource();
println item.getSource();
item.setSource(SourceStatus.WEBSITE_CONTACTFORM);
item.setFromcontactusform(false);
println item; 
}

if (item.getFromconfigregistration() == true)

{
item.getSource();
println item.getSource();
item.setSource(SourceStatus.WEBSITE_CONFIGURATION);
item.setFromconfigregistration(false);
println item; 
}


if (item.getFromdriveform() == true)

{
item.getSource();
println item.getSource();
item.setSource(SourceStatus.WEBSITE_DRIVEFORM);
item.setFromdriveform(false);
println item; 
}

modelService.save(item)
}

Now, you need to run this groovy script.

For that go to hac – Console – Scripting Languages – And select groovy and then place your groovy script in the edit place and Execute.

Now, you can able to see every customer source value in a drop down rather than Boolean values.

After every saving of drop down values, we need to un-check the Boolean values for that you need to make the values as false after every iteration.

Now, you can go to hmc and check an existing customer values.

There you can find the customer source value in a drop down and previous Boolean value in an un-check mode.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.