Skip to main content

Add/Remove Rate Plan Plugin Example

Zuora

Add/Remove Rate Plan Plugin Example

This article describes the process of creating a sample custom Add/Remove Rate Plan Plugin on the SelectProduct component.

For additional sample plugin and a test class example, see  Zuora for Salesforce Sample Code Suite.

The sample plugin in this article does the following:

  • When the named rate plan is added to a quote, the plug-in automatically adds the associated rate plan.
  • Prevents the named rate plan from being removed alone from a quote when another rate plan in the quote is dependent on it. 

Implement the Add/Remove Rate Plan Plugin

Follow the steps given in this section to set up the product catalog, create a plugin, and test the plugin.

To set up rate plans and the association between the rate plans:

  1. In your Zuora Product Catalog, add a product and a rate plan. Name the rate plan "SomeSampleRatePlan".
  2. Add another product and a rate plan. Name this rate plan "Basic".
  3. Perform a Product Catalog Sync.

To create the plugin class and register the new plugin for the SelectProduct component:

  1. In Salesforce, create a new class, AddRemoveRatePlanPlugin, using the code below.
  2. Navigate to Zuora Config > Component Registration.
  3. Click Edit for the SelectProduct component.
  4. In the Component Plugins section, for the Add/Remove Rate Plan Plugin type, specify AddRemoveRatePlanPlugin in the Class Name field.
  5. Click Update.

To test the newly added plugin:

  1. To test the new plug-in, add the Product and its rate plan, SomeSampleRatePlan, to a quote.
  2. When you click Save in the Product Selector, you see the Basic rate plan is also added to the quote.
  3. Remove the Basic rate plan from the quote, and click Save. You receive an error that the Basic rate plan cannot be removed.

The following is a code for the sample plugin: 

global class AddRemoveRatePlanPlugin implements zqu.SelectProductComponentOptions.IAddRemoveRatePlanPlugin{
   public void onAdd(String quoteId, List<String> addedRatePlans){
      List<zqu__ProductRatePlan__c> prp = 
         [select Id,name from zqu__ProductRatePlan__c 
            where id in :addedRatePlans and Name = 'SomeSampleRatePlan'];
      if(prp.size() > 0){
         //If SomeSampleRatePlan is added, add Basic rate plan, as well.
         zqu__ProductRatePlan__c prp1 = 
            [select Id,name from zqu__ProductRatePlan__c where Name = 'Basic'];
         zqu.zChargeGroup zcg = zqu.zQuoteUtil.getChargeGroup
            ((ID) quoteId, (ID)prp1.Id);
         zqu.zQuoteUtil.addChargeGroup(zcg);
      }
   }
    
   public void onRemove(String quoteId, List<String> removedRatePlans){
      List<zqu__ProductRatePlan__c> prp1 = [select Id,name 
         from zqu__ProductRatePlan__c 
         where id in :removedRatePlans and Name = 'SomeSampleRatePlan'];
      if(prp1.size() == 0){ 
      //If the SomeSampleRatePlan is not in the list of rate plans to be removed      
         List<zqu__ProductRatePlan__c> prp = 
            [select Id, Name from zqu__ProductRatePlan__c 
               where id in :removedRatePlans and Name = 'Basic'];
         if(prp.size() > 0){
            //You cannot remove the Basic rate plan 
            //without removing the SomeSampleRatePlan.
            throw new zqu.PropertyComponentController.PropertyComponentPluginException
               ('The Rate Plan, '+prp[0].Name+', cannot be removed from the quote.');
         }
      }
   }
   public class AddRemoveException extends Exception {}
}

Test Class for Add/Remove Rate Plan Plugin

The following is a APEX test class code sample for the Add/Remove Rate Plan Plugin. You can use the test class to achieve code coverage on the plugin in Salesforce.

@isTest(SeeAllData=true)
public class AddRemoveRatePlanPluginTest1 { 
    public static final String baseRateplanId = '23456789012345678901234567890121';
    
    @isTest static void testAddBase() {
        zqu__Quote__c quote = createTestQuote(null, null, null, null, true);
       
        AddRemoveRatePlanPlugin plugin = new AddRemoveRatePlanPlugin();

        Boolean success = false;

        try {
            plugin.onAdd (quote.Id, new List<Id>{baseRateplanId});
            success = true;
        } catch (AddRemoveRatePlanPlugin.AddRemoveException ex) {
            System.assert(true, 
               'AddRemoveRatePlanPlugin.AddRemoveException did not occur when adding a rate plan');
        }

        System.assert(success == false, 
           'AddRemoveRatePlanPlugin.AddRemoveException did not occur when adding a rate plan');
    }
}