Creating a customized Configure, Price, Quote (CPQ) solution that dynamically selects a specific commerce process based on conditions like country selection in Salesforce requires careful planning and implementation. This article outlines the steps and provides sample code to set up such a system, ensuring that users are directed to the appropriate CPQ commerce page based on their selections in an opportunity’s field(s). This guide assumes you are working with CPQ version 24A patch 1 and Salesforce Managed Package version 8.2.
Overview
The goal is to create a seamless experience where, upon selecting a specific country (e.g., Country A or Country B) in a Salesforce opportunity, the system automatically launches the corresponding CPQ commerce process (Commerce Process 1 or Commerce Process 2). This involves customizing Salesforce to add a button that dynamically redirects users to the correct CPQ process based on the opportunity’s country field.
Prerequisites
- Salesforce with CPQ 24A patch 1 installed.
- Salesforce Managed Package ver 8.2.
- Basic knowledge of Salesforce configuration and Apex programming.
Steps
1. Create Custom Fields
First, ensure that your Salesforce Opportunity object has a custom field to capture the country selection (e.g., Country__c
). This field will be used to determine which commerce process to launch.
2. Add a Custom Button
- Navigate to Setup → Object Manager → Opportunity.
- Select Buttons, Links, and Actions.
- Click New Button or Link.
- Create a new button (e.g.,
New Quote
) with behavior set to Execute JavaScript. - In the formula editor, you’ll write JavaScript that calls a Visualforce page or Apex class, depending on the country selected.
3. Create a Visualforce Page
This Visualforce page will act as a dispatcher, deciding which CPQ process to launch based on the opportunity’s country field.
<apex:page standardController="Opportunity" extensions="CPQDispatcherController">
<apex:outputText value="{!redirectScript}" escape="false"/>
</apex:page>
4. Implement the Apex Controller
The CPQDispatcherController
Apex class will contain the logic to select the correct CPQ process URL based on the opportunity’s country.
public class CPQDispatcherController {
private final Opportunity opp;
public CPQDispatcherController(ApexPages.StandardController stdController) {
this.opp = (Opportunity)stdController.getRecord();
this.opp = [SELECT Country__c FROM Opportunity WHERE Id = :this.opp.Id];
}
public String getRedirectScript() {
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
String cpqUrl;
// Determine the CPQ URL based on the country
if ('Country A'.equals(opp.Country__c)) {
cpqUrl = baseUrl + '/apex/CPQCommerceProcess1?oppId=' + opp.Id;
} else if ('Country B'.equals(opp.Country__c)) {
cpqUrl = baseUrl + '/apex/CPQCommerceProcess2?oppId=' + opp.Id;
} else {
// Default action or error handling
cpqUrl = baseUrl + '/apex/CPQDefaultProcess?oppId=' + opp.Id;
}
// Return JavaScript for redirection
return 'window.top.location.href = "' + cpqUrl + '";';
}
}
This controller retrieves the opportunity’s country field and decides which CPQ process URL to generate. The getRedirectScript
method returns JavaScript that redirects the user to the appropriate CPQ page.
5. Test Your Configuration
- Create or edit an opportunity and select a country.
- Click the
New Quote
button you added. - Verify that you’re redirected to the correct CPQ commerce process based on the country selected.
Conclusion
This setup allows for a dynamic selection of CPQ commerce processes based on conditions within Salesforce opportunities, enhancing the user experience by providing a seamless transition to the appropriate CPQ page. By leveraging custom fields, a custom button, a Visualforce page, and an Apex controller, you can achieve a flexible and dynamic CPQ process selection mechanism tailored to your business needs. Always ensure to test thoroughly in a sandbox environment before deploying to production to avoid any disruptions to your sales processes.