Implement the add/remove rate plan plugin
This task guides you through creating a custom Add/Remove Rate Plan Plugin for the SelectProduct component, including setup, implementation, and testing.
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.
Implementing the Add/Remove Rate Plan Plugin consists of three steps:
-
Set up rate plans and the association between the rate plans
-
Create the plugin class and register the new plugin for the SelectProduct component
-
Rest the newly added plugin:
- Set up rate plans and the association between the rate plans
- In your Zuora Product Catalog, add a product and a rate plan. Name the rate plan "SomeSampleRatePlan".
- Add another product and a rate plan. Name this rate plan "Basic".
- Perform a Product Catalog Sync.
- Create the plugin class and register the new plugin for the SelectProduct component.
- In Salesforce, create a new class, AddRemoveRatePlanPlugin, using the code below.
- Navigate to Zuora Config > Component Registration.
- Click Edit for the SelectProduct component.
- In the Component Plugins section, for the Add/Remove Rate Plan Plugin type, specify AddRemoveRatePlanPlugin in the Class Name field.
- Click Update.
- test the newly added plugin
- To test the new plug-in, add the Product and its rate plan, SomeSampleRatePlan, to a quote.
- When you click Save in the Product Selector, you see the Basic rate plan is also added to the quote.
- Remove the Basic rate plan from the quote, and click Save. You receive an error that the Basic rate plan cannot be removed.
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');
}
}