Skip to main content

ProductSyncUtil Class

Zuora

ProductSyncUtil Class

This article describes the ProductSyncUtil class, its global methods and the related class. You can use these classes and global methods to programmatically trigger product synchronization and cleanups from Salesforce to Zuora. The classes and global methods support the following operations in the single-entity and multi-entity environments:

  • Synchronizing Products, Features, UOMs, and related objects from Salesforce
  • Deleting Products, Features, UOMs, and related objects in the Zuora product catalog.
    When you mark the products and features to be deleted in the Product Sync UI in Salesforce, the syncProducts global method will delete the product in the Zuora product catalog.

ProductSyncUtil Class Properties 

The ProductSyncUtil class includes the following properties.

Property Type Required Description
FEATURE_TYPE Static Final String NA A constant that represents the SObject type of ZFeature__c.
PRODUCT_FEATURE_TYPE Static Final String NA A constant that represents the SObject type of ZProductFeature__c.
PRODUCT_RATE_PLAN_CHARGE_TYPE Static Final String NA A constant that represents the SObject type of ProductRatePlanCharge__c.
PRODUCT_RATE_PLAN_TYPE Static Final String NA A constant that represents the SObject type of ProductRatePlan__c.
PRODUCT_TYPE Static Final String NA

A constant that represents the SObject type of Product2.

batchSize Integer No (Deprecated) User-defined batch size
objectType String No (Deprecated) Specifies the type of the object to be synchronized.

ProductSyncUtil Global Methods

The ProductSyncUtil class provides the following global methods to synchronize or delete products and related objects.

Method Return Type Description

setBatchSize(Integer batchSize)

void

(Deprecated) Sets the batch size for synchronization jobs. You can use the method to reduce the number of products to be synchronized per batch for complicated products.

The default batch size is 50.

setSObjectType(String objectType) void (Deprecated) Sets the SObject type of the records to be synchronized.

syncProducts(List<Id> productIds)

Id[]

(Deprecated) Synchronizes a list of products specified by the list of product Ids. Returns an array of ApexSyncJob Ids that can be polled to check the progress of the SyncJobs.

In a multi-entity environment, the method returns the SyncJob Ids for each entity.

This method deletes the products that are marked as delete from Zuora Product Catalog.

syncRecords(ProductSyncConfiguration productSyncConfiguration) Id[]

Synchronizes the records specified in the ProductSyncConfiguration reference.

See ProductSyncConfiguration Class below for the details.

Returns an array of ApexSyncJob Ids that can be polled to check the progress of the SyncJobs.

ProductSyncConfiguration Class

The ProductSyncConfiguration class includes the following properties.

Property Type Required Description
batchSize Integer No User-defined batch size.
recordIds Set<Id> Yes Defines the Set of records to be synchronized.
recordType String Yes Specifies the type of SObject to be synchronized.

Code Sample

Synchronize all Products and Check Status of SyncJobs (Deprecated Approach)

List<Product2> products = [
    SELECT Id
    FROM Product2
];
List<Id> productIds = new List<Id>();
for(Product2 product : products) {
    productIds.add(product.Id);
}
zqu.ProductSyncUtil util = new zqu.ProductSyncUtil();
util.setSObjectType('Product2'); 
Id[] jobIds = util.syncProducts(productIds);
 
AsyncApexJob syncjobs = [
    SELECT Id, Status, JobItemsProcessed, TotalJobItems
    FROM AsyncApexJob
    WHERE Id IN :jobIds
];
 
for (AsyncApexJob job: syncJobs) {
    if(job.Status != 'Completed') {
        System.debug('Sync Progress: ' + (job.JobItemsProcessed / job.TotalJobItems));
    } else {
        System.debug('Job Complete!');
    }
}

Synchronize all Products and Check Status of SyncJobs

Map<Id, Product2> productObjects = new Map<Id, Product2>([
    SELECT Id
    FROM Product2
]);
 
zqu.ProductSyncUtil.ProductSyncConfiguration productSyncConfiguration = new zqu.ProductSyncUtil.ProductSyncConfiguration();
productSyncConfiguration.recordIds = productObjects.keySet();
productSyncConfiguration.recordType = zqu.ProductSyncUtil.PRODUCT_TYPE;
// Optionally apply batch size. If there are 10 Product records, 2 Jobs will be created. Each job will sync over 5 product records
productSyncConfiguration.batchSize = 5;
 
zqu.ProductSyncUtil util = new zqu.ProductSyncUtil();
Id[] productSyncHistoryIds = util.syncRecords(productSyncConfiguration);
 
AsyncApexJob syncjobs = [
    SELECT Id, Status, JobItemsProcessed, TotalJobItems
    FROM AsyncApexJob W
    HERE Id IN :productSyncHistoryIds
];
  
for (AsyncApexJob job: syncJobs) {
    if(job.Status != 'Completed') {
        System.debug('Sync Progress: ' + (job.JobItemsProcessed / job.TotalJobItems));
    } else {
        System.debug('Job Complete!');
    }
}

Synchronize all Features

Map<Id, zqu__ZFeature__c> featureObjects = new Map<Id, zqu__ZFeature__c>([
    SELECT Id
    FROM zqu__ZFeature__c
]);
 
zqu.ProductSyncUtil.ProductSyncConfiguration featureSyncConfiguration = new zqu.ProductSyncUtil.ProductSyncConfiguration();
featureSyncConfiguration.recordIds = featureObjects.keySet();
featureSyncConfiguration.recordType = zqu.ProductSyncUtil.FEATURE_TYPE;
 
zqu.ProductSyncUtil util = new zqu.ProductSyncUtil();
Id[] featureSyncHistoryIds = util.syncRecords(featureSyncConfiguration);

Synchronize all Product Features


Map<Id, zqu__ZProductFeature__c> productFeatureObjects = new Map<Id, zqu__ZProductFeature__c>([
    SELECT Id
    FROM zqu__ZProductFeature__c
]);
 
zqu.ProductSyncUtil.ProductSyncConfiguration productFeatureSyncConfiguration = new zqu.ProductSyncUtil.ProductSyncConfiguration();
productFeatureSyncConfiguration.recordIds = productFeatureObjects.keySet();
productFeatureSyncConfiguration.recordType = zqu.ProductSyncUtil.ProductSyncUtil.PRODUCT_FEATURE_TYPE;;
 
zqu.ProductSyncUtil util = new zqu.ProductSyncUtil();
Id[] productFeatureSyncHistoryIds = util.syncRecords(productFeatureSyncConfiguration);

Synchronize all Product Rate Plans

Map<Id, zqu__ProductRatePlan__c> productRatePlanObjects = new Map<Id, zqu__ProductRatePlan__c>([
    SELECT Id
    FROM zqu__ProductRatePlan__c
]);
 
zqu.ProductSyncUtil.ProductSyncConfiguration productRatePlanSyncConfiguration = new zqu.ProductSyncUtil.ProductSyncConfiguration();
productRatePlanSyncConfiguration.recordIds = productRatePlanObjects.keySet();
productRatePlanSyncConfiguration.recordType = zqu.ProductSyncUtil.PRODUCT_RATE_PLAN_TYPE;
 
ProductSyncUtil util = new ProductSyncUtil();
Id[] productRatePlanSyncHistoryIds = util.syncRecords(productRatePlanSyncConfiguration);

Synchronize all Product Rate Plan Charges

Map<Id, zqu__ProductRatePlanCharge__c> productRatePlanChargeObjects = new Map<Id, zqu__ProductRatePlanCharge__c>([
    SELECT Id
    zqu__FROM ProductRatePlanCharge__c
]);
 
zqu.ProductSyncUtil.ProductSyncConfiguration productRatePlanChargeSyncConfiguration = new zqu.ProductSyncUtil.ProductSyncConfiguration();
productRatePlanChargeSyncConfiguration.recordIds = productRatePlanChargeObjects.keySet();
productRatePlanChargeSyncConfiguration.recordType = zqu.ProductSyncUtil.PRODUCT_RATE_PLAN_CHARGE_TYPE;
 
zqu.ProductSyncUtil util = new zqu.ProductSyncUtil();
Id[] productRatePlanChargeSyncHistoryIds = util.syncRecords(productRatePlanChargeSyncConfiguration);