Knowledge Center

Knowledge Center > Commerce > Zuora for Salesforce Development Resources > Zuora for Salesforce Component Library > Zuora for Salesforce Component Library Samples > Product Catalog On-Demand Sync

Product Catalog On-Demand Sync

Overview

This article presents a sample implementation of the following scenario: 

  • Use the Order Builder to create a product in your Zuora tenant.
  • Invoke the Product Catalog On-demand Sync to bring over the product to Salesforce.

ProductCatalogManager

To create and execute the example code:

  • Create the ProductCatalogManager Apex class with the following code:
public class ProductCatalogManager
{
  private Zuora.zApi zapi; 
  
  public ProductCatalogManager ()
  {
    zapi = new Zuora.zapi();
    zapi.zlogin();
    
    ticker = String.valueOf( DateTime.now().getTime() );
    productName = 'Mobile Plan ' + ticker.substring( ticker.length() - 3, ticker.length() );
    
  }
  
  public String productName { get; set; }
  public String ticker { get; set; }
  
  public void createProduct()
  {
    try
    {
      String productId; 
      String ratePlanId; 
      String chargeId; 
      Zuora.zObject[] errors; 
      List<Zuora.zObject> products = new List<Zuora.zObject>();
      products.add( populateProduct() );
      List<Zuora.zapi.SaveResult> results = zapi.zcreate( products );
      Zuora.zapi.SaveResult result = results[0]; 
      if ( result.success )
      {
        productId = result.Id; 
      }
      else
      {
        errors = result.errors; 
        System.debug( LoggingLevel.ERROR, 'An error occurred while creating product: ' + errors );
        return;
      }
      
      results = zapi.zCreate( populateRatePlans( productId ) );
      result = results[0]; 
      if ( result.success ) 
      { 
        ratePlanId = result.Id; 
      }
      else 
      {
        errors = result.errors; 
        System.debug( LoggingLevel.ERROR, 'An error occurred while creating rate plan: ' + errors );
        return; 
      }
      
      results = zapi.zCreate( populateRatePlanCharges( ratePlanId ) ); 
      result = results[0]; 
      if ( !result.success ) 
      {
        errors = result.errors; 
        System.debug( LoggingLevel.ERROR, 'An error occurred while creating rete plan charges: ' + errors );
        return; 
      }
        
      syncProduct( productId );
    }
    catch( Zuora.zRemoteException remoteEx ) 
    {
      System.debug( LoggingLevel.ERROR, 'An error occurred: ' + remoteEx.getMessage() );
    }
    catch( Zuora.zApiException apiEx ) 
    {
      System.debug( LoggingLevel.ERROR, 'An error occurred: ' + apiEx.getMessage() );
    }
    catch( Zuora.zForceException forceEx)
    {
      System.debug( LoggingLevel.ERROR, 'An error occurred: ' + forceEx.getMessage() );
    }
  
  }
  
  private Zuora.zObject populateProduct()
  {
    Zuora.zObject product = new Zuora.zObject('Product');
    product.setValue( 'Name', productName);
    product.setValue( 'SKU', 'SKU-' + ticker );
    product.setValue( 'EffectiveStartDate', '2010-01-01T00:00:00-08:00' );
    product.setValue( 'EffectiveEndDate', '2014-01-01T00:00:00-08:00' ); 
    return product;
  }
  
  private List<Zuora.zObject> populateRatePlans( String productId )
  {
    Zuora.zObject ratePlan = new Zuora.zObject('ProductRatePlan');
    ratePlan.setValue( 'Name', 'Basic Package' );
    ratePlan.setValue( 'Description', 'Basic package' );
    ratePlan.setValue( 'ProductId', productId );
    ratePlan.setValue( 'EffectiveStartDate', '2010-01-01T00:00:00-08:00' );
    ratePlan.setValue( 'EffectiveEndDate', '2014-01-01T00:00:00-08:00' ); 
    return new List<Zuora.zObject>{ ratePlan };
  }
  
  private Zuora.zObject[] populateRatePlanCharges( String ratePlanId )
  {
    Zuora.zObject[] charges = new Zuora.zObject[]{}; 
    Zuora.zObject charge = new Zuora.zObject('ProductRatePlanCharge'); 
    charge.setValue( 'BillCycleType', 'DefaultFromCustomer' );
    charge.setValue( 'BillingPeriod', 'Month' );
    charge.setValue( 'ChargeModel', 'FlatFee' );
    charge.setValue( 'Name', 'Single-line Monthly Charge' );
    charge.setValue( 'ProductRatePlanId', ratePlanId );
    charge.setValue( 'TriggerEvent', 'ContractEffective' );
    charge.setValue( 'ChargeType', 'Recurring' );
    
    Zuora.zObject tierData = new Zuora.zObject('ProductRatePlanChargeTierData');
    Zuora.zObject[] tiers = new Zuora.zObject[]{}; 
    Zuora.zObject tier = new Zuora.zObject( 'ProductRatePlanChargeTier' );
    tier.setValue( 'Active', true );
    tier.setValue( 'Currency', 'USD' );
    tier.setValue( 'Price', '39.99' );
    tiers.add( tier ); 
    tierData.setValue( 'ProductRatePlanChargeTier', tiers );
    charge.setValue( 'ProductRatePlanChargeTierData', tierData );
    charges.add( charge );
    
    return charges; 
  }
  
  private void syncProduct( String productId )
  {
    Zuora.OnDemandSyncManager syncManager = new Zuora.OnDemandSyncManager();
    syncManager.syncZuoraObjectIdSet = new Set<String>{ productId };
    syncManager.syncObjectType = Zuora.OnDemandSyncManager.ObjectType.ZProduct;
    List<Zuora.SyncResult> syncResults = syncManager.sendRequest();
    System.debug( 'Sync results = ' + syncResults );
  }
 }

To test the sample code:

  • Execute the following Apex script in the Developer Console.
ProductCatalogManager mgr = new ProductCatalogManager();
mgr.createProduct();
Last modified
17:14, 12 Feb 2015

Tags

Classifications

(not set)