Update product prices, descriptions, or categories in bulk with validation and rollback support
Bulk Product Update
intermediate medium risk Data Management
Description
Prerequisites
- Product catalog access
- Backup of current data
- Sufficient system resources for bulk operations
Parameters
catalogId
String*Catalog ID to update products in
updateType
String*Type of update: price, description, or category
dryRun
BooleanPerform a dry run without making changes
Default: true
Script Code
import de.hybris.platform.catalog.model.CatalogModel
import de.hybris.platform.catalog.model.CatalogVersionModel
import de.hybris.platform.core.model.product.ProductModel
import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.servicelayer.model.ModelService
def catalogId = parameters.catalogId
def updateType = parameters.updateType
def dryRun = parameters.dryRun ?: true
def errors = []
def updatedCount = 0
try {
def catalog = flexibleSearchService.search("SELECT {pk} FROM {Catalog} WHERE {id} = ?catalogId",
[catalogId: catalogId]).result[0]
if (!catalog) {
errors.add("Catalog with ID $catalogId not found")
return
}
def catalogVersion = catalog.activeCatalogVersion
def products = flexibleSearchService.search("SELECT {pk} FROM {Product} WHERE {catalogVersion} = ?catalogVersion",
[catalogVersion: catalogVersion]).result
products.each { product ->
try {
switch (updateType) {
case 'price':
// Update price logic here
if (!dryRun) {
// modelService.save(product)
}
break
case 'description':
// Update description logic here
if (!dryRun) {
// modelService.save(product)
}
break
case 'category':
// Update category logic here
if (!dryRun) {
// modelService.save(product)
}
break
}
updatedCount++
} catch (Exception e) {
errors.add("Error updating product $product.code: $e.message")
}
}
println "Updated $updatedCount products successfully"
if (errors) {
println "Errors encountered:"
errors.each { println " - $it" }
}
} catch (Exception e) {
errors.add("Script execution failed: $e.message")
}Script Information
Author:SAP Commerce Team
Execution Time:2-5 minutes
Rating:4.5/5
Downloads:1250
Last Updated:2024-01-15
Version:1.2.0
Tags
tools.groovyScriptLibrary.ui.title
tools.groovyScriptLibrary.ui.subtitle