Home » Commerce » Groovy : Converting all EmailIds of customers to lower case in hybris

Groovy : Converting all EmailIds of customers to lower case in hybris

I have many Email ids of CustomerModel in UPPER CASE in hybris Database. I need to convert all existing emailIds into lowercase. what would be the easiest way to perform such bulk entry action:

Manually changes are really hard and unprofessional way to do such changes. Basically with large numbers of entries like : Production , we must choose Groovy Script.

Here we have one small example to convert the such changes with easiest way.

import de.hybris.platform.core.Registry
import de.hybris.platform.core.model.user.CustomerModel
import de.hybris.platform.servicelayer.model.ModelService
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import groovy.transform.Field

@Field
FlexibleSearchService flexibleSearchService = Registry.getApplicationContext().getBean("flexibleSearchService")

@Field
ModelService modelService = Registry.getApplicationContext().getBean("modelService")


def customerQuery = new FlexibleSearchQuery("SELECT {PK} FROM {Customer}")
def customerResult = flexibleSearchService.search(customerQuery).result

customerResult.each {
    CustomerModel customer = it as CustomerModel
    if ( customer != null && customer.getLoginEmailAdress() != null){
            println("customer before: " + customer.getLoginEmailAdress())
            def email=customer.getLoginEmailAdress().toLowerCase()
            customer.setLoginEmailAdress(email.toLowerCase());
            modelService.save(customer)
         println("customer after: " + customer.getLoginEmailAdress())
}
}