Synchronize product data between different catalog versions with conflict resolution
Product Catalog Sync
advanced medium risk Product Management
Description
Prerequisites
- Source and target catalog versions
- Conflict resolution strategy
- Product data mapping rules
Parameters
sourceCatalogVersion
String*Source catalog version ID
targetCatalogVersion
String*Target catalog version ID
conflictResolution
StringConflict resolution strategy: source, target, or manual
Default: source
Script Code
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 sourceCatalogVersionId = parameters.sourceCatalogVersion
def targetCatalogVersionId = parameters.targetCatalogVersion
def conflictResolution = parameters.conflictResolution ?: 'source'
def errors = []
def syncedCount = 0
def conflictCount = 0
try {
// Get catalog versions
def sourceCatalogVersion = flexibleSearchService.search("SELECT {pk} FROM {CatalogVersion} WHERE {version} = ?version",
[version: sourceCatalogVersionId]).result[0]
def targetCatalogVersion = flexibleSearchService.search("SELECT {pk} FROM {CatalogVersion} WHERE {version} = ?version",
[version: targetCatalogVersionId]).result[0]
if (!sourceCatalogVersion) {
errors.add("Source catalog version '$sourceCatalogVersionId' not found")
return
}
if (!targetCatalogVersion) {
errors.add("Target catalog version '$targetCatalogVersionId' not found")
return
}
// Get products from source catalog
def sourceProducts = flexibleSearchService.search("SELECT {pk} FROM {Product} WHERE {catalogVersion} = ?catalogVersion",
[catalogVersion: sourceCatalogVersion]).result
sourceProducts.each { sourceProduct ->
try {
// Check if product exists in target catalog
def targetProduct = flexibleSearchService.search("SELECT {pk} FROM {Product} WHERE {code} = ?code AND {catalogVersion} = ?catalogVersion",
[code: sourceProduct.code, catalogVersion: targetCatalogVersion]).result[0]
if (targetProduct) {
// Handle conflict resolution
conflictCount++
switch (conflictResolution) {
case 'source':
// Update target with source data
copyProductData(sourceProduct, targetProduct)
modelService.save(targetProduct)
break
case 'target':
// Keep target data, skip update
println "Keeping target data for product $sourceProduct.code"
break
case 'manual':
// Log for manual review
println "Manual review required for product $sourceProduct.code"
break
}
} else {
// Create new product in target catalog
def newProduct = modelService.create(ProductModel.class)
newProduct.setCode(sourceProduct.code)
newProduct.setName(sourceProduct.name)
newProduct.setCatalogVersion(targetCatalogVersion)
copyProductData(sourceProduct, newProduct)
modelService.save(newProduct)
}
syncedCount++
} catch (Exception e) {
errors.add("Error syncing product '$sourceProduct.code': $e.message")
}
}
println "Synced $syncedCount products successfully"
println "Conflicts resolved: $conflictCount"
if (errors) {
println "Errors encountered:"
errors.each { println " - $it" }
}
} catch (Exception e) {
errors.add("Product catalog sync failed: $e.message")
}
def copyProductData(sourceProduct, targetProduct) {
// Copy product attributes
targetProduct.setName(sourceProduct.name)
targetProduct.setDescription(sourceProduct.description)
// Add more attribute copying as needed
}Script Information
Author:SAP Commerce Team
Execution Time:5-12 minutes
Rating:4.6/5
Downloads:650
Last Updated:2024-01-14
Version:2.0.0
Tags
tools.groovyScriptLibrary.ui.title
tools.groovyScriptLibrary.ui.subtitle