Expand Data Sources for Return Insights
To start, extend the base class, commerce_ordermanagement.ProductExpandService.
public class ProductExpandServiceSample extends commerce_ordermanagement.ProductExpandService {
// Extension code.
}
Override the returnReasons method of commerce_ordermanagement.ProductExpandService, which is used to get the product’s return reasons. In this example, the entire method is replaced with custom logic to extract product data from ProductExpandRequest, set the return reasons, and process the ProductExpandResponse. You can also fetch the return reasons from an external service.
// Extract product data from the request with custom logic and get the product's return reasons from an external service.
public override commerce_ordermanagement.ProductExpandResponse returnReasons(commerce_ordermanagement.ProductExpandRequest productExpandRequest) {
// Create a product expand response without the default processing.
List<commerce_ordermanagement.ProductData> productList = new List<commerce_ordermanagement.ProductData>();
List<commerce_ordermanagement.Reason> returnReasons = new List<commerce_ordermanagement.Reason>();
commerce_ordermanagement.Reason reason1 = new commerce_ordermanagement.Reason();
commerce_ordermanagement.Reason reason2 = new commerce_ordermanagement.Reason();
//Set the reason in the response. You can also fetch the reasons from external systems and set it.
reason1.setReason('Size is big');
reason2.setReason('Too big');
returnReasons.add(reason1);
returnReasons.add(reason2);
for(commerce_ordermanagement.ProductData productData : productExpandRequest.getProducts()) {
// Set the return reasons for the products in the request
productData.setReturnReasons(returnReasons);
productList.add(productData);
}
response.setProductList(productList);
response.setSucceed(true);
// Process the product expand response with more custom logic.
// Return the processed product expand response.
return response;
}
Instead of replacing the entire method with custom logic, you can call the base method with the super() method. You can still insert custom logic before and after the super() call.
public override commerce_ordermanagement.ProductExpandResponse returnReasons(commerce_ordermanagement.ProductExpandRequest productExpandRequest) {
// Get the product expand response using the default implementation of the returnReasons method. Although returnReasons is called unmodified, we can supply it with a modified version of the request.
commerce_ordermanagement.ProductExpandResponse productExpandResponse = super.returnReasons(productExpandRequest);
// Modify the product return reasons response with custom logic.
// Return the modified transactional return reasons response.
return productExpandResponse;
}
To get the return reasons from an external service, define a custom private method.
private Map<String, ProductReturnReasonsDataFromExternalService> getReturnReasonsFromExternalService(Set<String> productIds){
// Custom logic.
}
You can create extension classes that can:
- Replace the entire default implementation of each base class method with custom logic.
- Add custom logic before calling the default implementation of a base class method.
- Add customer logic after calling the default implementation of a base class method.
- Add custom methods and classes.
To learn how to add your own Apex classes to a Salesforce org, see Adding an Apex Class.
Before you can fill an extension slot with a custom extension provider, create a web store. Register the extension class, and map the extension class to a B2C or D2C Commerce store. If you don’t have a web store, create a dummy web store in your Developer Console. Go to Debug, open Execute Anonymous Window, and paste this code.
WebStore store = new WebStore(
Name = 'SiteGenesis',
// Either NET or GROSS
DefaultTaxLocaleType = 'NET',
// Format expected is instanceId@siteName; example abc_123@SiteGenesis
ExternalReference = 'bblz_stg@SiteGenesis',
Type = 'B2CE'
);
insert store;
Click Execute.
To fetch the web store ID, execute this SOQL query
SELECT Id FROM WebStore LIMIT 1
and pick the web store ID from the returned values. Map your web store’s ID to the EPN.
To register and map extension classes, we recommend using Salesforce CLI and the Salesforce Commerce plug-in for sfdx. To manage extensions, use the latest version of Salesforce CLI. For detailed instructions, install Salesforce CLI and update Salesforce CLI.
To install the plug-in, run sfdx plugins:install @salesforce-commerce.
Example commands. The store-ID is the web store ID.
Example
# Register an extension class
sfdx commerce:extension:register --targetusername user@abc.com --apex-class-name ProductExpandServiceSample --extension-point-name Commerce_Domain_OrderManagement_Product --registered-extension-name ProductExpandServiceSample
# Map an extension class
sfdx commerce:extension:map --registered-extension-name ProductExpandServiceSample --store-id <store_id>
# Unmap an extension class
sfdx commerce:extension:unmap --registered-extension-name ProductExpandServiceSample --store-id <store_id>