Populate value plugin example
This document provides examples of creating plugins to set default field values for quotes in Salesforce, including extending existing plugins and implementing new ones.
You can create your own plugin to set default field values of quotes using one of the following methods:
-
Extend the Populate Default Field Value plugin (
PopulateDefaultFieldValuePlugin) of the CreateQuote component. -
Implement a Default Field Values plugin (
DefaultValuesPluginV2). It requires that CPQ X is set up in you Salesforce org.
See the following sections for more information.
Extend Populate Default Field Value Plugin
You have an option to extend the component plugin Instead of overriding a plugin. You can only extend a plugin that is defined as virtual. When you extend a plugin, you enhance the behavior of a component by adding your custom logic on top of the existing logic.
When you extend a virtual class, use the super keyword to override constructors and methods from the parent class.
In this example, you will modify the IPopulateValuePlugin of the CreateQuote component to provide the default quote values that are relevant to your subscription model.
global class SampleDefaultValuesPluginV1 extends zqu.CreateQuoteController.PopulateDefaultFieldValuePlugin {
global override void populateDefaultFieldValue(SObject record, zqu.PropertyComponentController.ParentController pcc) {
// "pcc" object will be null when the pulgin is executed in the context of CPQ X.
if (pcc != null) {
super.populateDefaultFieldValue(record, pcc);
}
record.put('zqu__ValidUntil__c', Date.today().addDays(30));
record.put('zqu__StartDate__c', Date.today());
if (record.get('zqu__SubscriptionType__c') == 'New Subscription') {
// Populate Quote Attributes.
record.put('zqu__InitialTerm__c', 24);
record.put('zqu__RenewalTerm__c', 24);
record.put('zqu__PaymentMethod__c', 'Check');
record.put('zqu__AutoRenew__c', true);
record.put('zqu__ApplyCreditBalance__c', true);
record.put('zqu__GenerateInvoice__c', true);
// Fetch BillTo and SoldTo Contacts.
Id accountId = (Id) record.get('zqu__Account__c');
List<Contact> contacts = [SELECT Id, Name FROM Contact WHERE Account.Id = :accountId];
if (contacts != null && contacts.size() > 0) {
// Populate the BillTo & SoldTo contact info.
record.put('zqu__BillToContact__c', contacts[0].Id);
record.put('zqu__SoldToContact__c', contacts[0].Id);
// "pcc" object will be null when the pulgin is executed in the context of CPQ X.
if (pcc != null) {
super.setLookupOptions(pcc);
zqu.LookupComponentOptions billToOptions = super.getLookupOption('zqu__BillToContact__c');
billToOptions.targetId = contacts[0].Id;
billToOptions.targetName = contacts[0].Name;
zqu.LookupComponentOptions soldToOptions = super.getLookupOption('zqu__SoldToContact__c');
soldToOptions.targetId = contacts[0].Id;
soldToOptions.targetName = contacts[0].Name;
}
}
} else if (record.get('zqu__SubscriptionType__c') == 'Amend Subscription') {
record.put('zqu__Amendment_Name__c', 'Amendment ' + record.get('zqu__Subscription_Name__c'));
}
}
}
Test Class for Populate Value Plugin
The following is a APEX test class code sample for Populate Value Plugin. You can use the test class to achieve code coverage on the plugin in Salesforce.
@isTest
public class SampleDefaultValuesPluginV1Test {
public static zqu__Quote__c quote;
@isTest
static void testPopulateDefaultFieldValue() {
Account acc = new Account(name='Test Account');
insert acc;
Contact billTo = new Contact(FirstName = 'BillToFName', LastName =
'BillToLName');
billTo.accountId = acc.Id;
Contact soldTo = new Contact(FirstName = 'SoldToFName', LastName =
'SoldToLName');
soldTo.accountId = acc.Id;
Contact[] contacts = new List<Contact>{ billTo, soldTo };
insert contacts;
quote = new zqu__Quote__c();
quote.Name = 'test quote';
quote.zqu__Account__c = acc.Id;
quote.zqu__SubscriptionType__c = 'New Subscription';
insert quote;
SampleDefaultValuesPluginV1 sdvp1 = new SampleDefaultValuesPluginV1();
sdvp1.populateDefaultFieldValue(quote, null);
}
}
Implement Default Values Plugin
Alternative to extending the Populate Default Field Value plugin, if you are using CPQ X, you can use the following code sample to achieve the same result as the preceding plugin ( SampleDefaultValuesPluginV1 ):
global class SampleDefaultValuesPluginV2 implements zqu.DefaultValuesPluginV2 {
global void initialize(zqu__Quote__c quote) {
// Populate Quote Attributes.
quote.zqu__ValidUntil__c = Date.today().addDays(30);
quote.zqu__StartDate__c = Date.today();
if (quote.zqu__SubscriptionType__c == 'New Subscription') {
quote.zqu__InitialTerm__c = 24;
quote.zqu__RenewalTerm__c = 24;
quote.zqu__PaymentMethod__c = 'Check';
quote.zqu__AutoRenew__c = true;
quote.zqu__ApplyCreditBalance__c = true;
quote.zqu__GenerateInvoice__c = true;
// Fetch BillTo and SoldTo Contacts.
Id accountId = quote.zqu__Account__c;
List<Contact> contacts = [SELECT Id, Name FROM Contact WHERE Account.Id = :accountId];
if (contacts != null && contacts.size() > 0) {
// Populate the BillTo & SoldTo contact info.
Id contactId = contacts[0].Id;
quote.zqu__BillToContact__c = contactId;
quote.zqu__SoldToContact__c = contactId;
}
} else if (quote.zqu__SubscriptionType__c == 'Amend Subscription') {
quote.zqu__Amendment_Name__c = 'Amendment ' + quote.zqu__Subscription_Name__c;
}
}
}
.