Loading
Prepare for Email to Become the Default Login ExperienceRead More
Get Started with Communications, Media, and Energy & Utilities (CME)...
SetCartItemExternalPriceService

SetCartItemExternalPriceService

The SetCartItemExternalPriceService is a VlocityOpenInterface implementation. You can use it only when the PricingElementServiceImplementation is the active pricing implementation associated with the PricingInterface. SetCartItemExternalPriceService sets prices that come from a process or service external to pricing elements. Setting recurring and one-time charges using this service enables the advanced pricing module to track and manage prices that do not come from a price list. The SetCartItemExternalPriceService is instantiated and invoked directly in the PricingElementServiceImplementation hook custom Apex code to set the external prices before the PricingElementServiceImplementation priceLineItems method is invoked.

Interface

VlocityOpenInterface

Input Parameters

parent

Type: sObject

The parent Opportunity, Quote, or Order sObject

itemList

Type: List<sObject>

The list of OpportunityLineItem, QuoteLineItem, or OrderItem objects

itemFieldsInfo

Type: Map<String, Object>

Map of the item external price field information to set on the line item. The map is keyed by line item ID. Each map entry that contains the information is also a map of string to object.

<field name>

Type: Map<String, Object>

An entry in the itemFieldsInfo map; the value is also a map of string to object.

value

Type: Decimal

The external price value of the field in the <field name> map

detail

Type: String

A display text for the external price that will appear in the price details for the price field

This example shows how to construct the input parameters in the SetCartItemExternalPriceService. The external prices in the sample should be calculated from another process or service, such as retrieving them from an external system or price matrix. The sample does not show how that process is done.

example
example

Input Parameters for SetCartItemexteralPriceService

Map<String, Object> externalPriceServiceInput = new Map<String, Object>();
Map<String, Object> externalPriceServiceOutput = new Map<String, Object>();
Map<String, Object> externalPriceServiceOptions = new Map<String, Object>();
Map<String, Object> itemFieldsInfo = new Map<String, Object>();  // map of field information for each line item. Keyed by line item id.
Map<String, Object> itemFieldDetails = new Map<String, Object>(); // map of details for each price field. Keyed by field name including namespace.
Map<String, Object> fieldVals = new Map<String, Object>(); // map of field value and detail
fieldVals.put('value', 5.55); // Set the external price to be $5.55. This should be retrieved dynamically from an external process.
fieldVals.put('detail', 'External price from matrix. Reference num 123.'); // Set the display text for the price. This should be generated dynamically based on information regarding the price.
itemFieldDetails.put('vlocity_cmt__RecurringCharge__c', fieldVals); // Set the field values to the vlocity_cmt__RecurringCharge__c field.
fieldVals = new Map<String, Object>(); // Create a new map of values for the next field
fieldVals.put('value', 999.99); // Set the external price. This should be retrieved dynamically from an external process.
fieldVals.put('detail', 'External price from matrix. Reference num 456.'); // Set the display text for the price. This should be generated dynamically based on information regarding the price.
itemFieldDetails.put('vlocity_cmt__OneTimeCharge__c', fieldVals); // Set the field values to the
vlocity_cmt__OneTimeCharge__c field.
itemFieldsInfo.put(String.valueOf(itemObj.Id), itemFieldDetails); // Set the itemFieldDetails to the line item id of the line item whose prices we are setting.
// Create an instance of the SetCartItemExternalPriceService
Type t = Type.forName('vlocity_cmt.SetCartItemExternalPriceService');
VlocityOpenInterface externalPriceService = (VlocityOpenInterface)t.newInstance();
// invoke the setItemPrices method of the SetCartItemExternalPriceService with the inputs.
externalPriceService.invokeMethod('setItemPrices', externalPriceServiceInput, externalPriceServiceOutput, externalPriceServiceOptions);

Sample Code

The SamplePricingElementServiceHookImpl class shows how to invoke the SetCartItemExternalPriceService to set prices for products when add-to-cart pricing is invoked.

/**
* This class is used as a sample hook implementation to the PricingElementServiceImplementation.
* To use this class, create a PricingElementServiceImplementationH interface under All Tabs > Interface Implementations
* and set SamplePricingElementServiceHookImpl as the default and active implementation class. The invokeMethod of this
* hook class will be called both before and after the invokeMethod of the PricingElementServiceImplementation.
* The methods will be suffixed with PreInvoke and PostInvoke respectively to indicate whether it is being called before
* or after the PricingElementServiceImplementation method.
*/
global with sharing class SamplePricingElementServiceHookImpl implements vlocity_cmt.VlocityOpenInterface{
   global Boolean invokeMethod(String methodName, Map<String, Object> input, Map<String, Object> output, Map<String, Object> options)  {
   try
   {
      System.debug('--- SamplePricingElementServiceHookImpl methodName: ' + methodName);
      if (methodName.equalsIgnoreCase('priceLineItems.PreInvoke'))
      {
         priceLineItemsPreInvoke(input, output,options);
      }
      return true;
   }
   catch (Exception ex)
   {
      System.debug('--- Exception: ' + ex.getMessage());
      System.debug('--- Stack Trace: ' + ex.getStackTraceString());
      throw ex;
   }
}
private void priceLineItemsPreInvoke(Map<String, Object> input, Map<String, Object> output, Map<String, Object> options) {
   SObject parent = (SObject)input.get('parent');
   List<SObject> itemList = (List<SObject>)input.get('itemList');
   //System.debug('--- priceLineItemsPreInvoke parent: ' + JSON.serialize(parent));
   //System.debug('--- priceLineItemsPreInvoke itemList: ' + JSON.serialize(itemList));
   String cartAction = (String)input.get('cartAction');
   // Only execute when cartAction is adding items to the cart
   if (cartAction != 'AddCartItem' && cartAction != 'AddCartPromotionItem') return;
   // collect the items by product names we want to set external prices for
   Set<String> prodNames = new Set<String>{'Test Prod 2'};
   Map<String, List<SObject>> productNameToItemObjs = new Map<String, List<SObject>>();
   for (SObject item : itemList)
   {
      tring prodName = (String)item.getSObject('PricebookEntry').getSObject('Product2').get('Name');
      if (prodNames.contains(prodName))
      {
         List<SObject> itemObjList = productNameToItemObjs.get(prodName);
         if (itemObjList == null)
         {
            itemObjList = new List<SObject>();
            productNameToItemObjs.put(prodName, itemObjList);
         }
         itemObjList.add(item);
      }
   }
   // simulate external prices
   Type t = Type.forName('vlocity_cmt.SetCartItemExternalPriceService');
   if (t != null)
   {
      VlocityOpenInterface externalPriceService = (VlocityOpenInterface)t.newInstance();
         Map<String, Object> externalPriceServiceInput = new Map<String, Object>();
         Map<String, Object> externalPriceServiceOutput = new Map<String, Object>();
         Map<String, Object> externalPriceServiceOptions = new Map<String, Object>();
         Map<String, Object> itemFields = new Map<String, Object>();
         for (String productName : productNameToItemObjs.keySet())
         {
            List<SObject> itemObjList = productNameToItemObjs.get(productName);
            if (itemObjList != null)
            {
               for (SObject itemObj : itemObjList)
               {
                  Map<String, Object> itemFieldDetails = new Map<String, Object>();
                  Map<String, Object> fieldVals = new Map<String, Object>();
                  fieldVals.put('value', 5.55);
               fieldVals.put('detail', 'External price from matrix. Reference num 123.');
               itemFieldDetails.put('vlocity_cmt__RecurringCharge__c', fieldVals);
               fieldVals = new Map<String, Object>();
               fieldVals.put('value', 999.99);
               fieldVals.put('detail', 'External price from matrix. Reference num 456.');
               itemFieldDetails.put('vlocity_cmt__OneTimeCharge__c', fieldVals);
               itemFields.put(String.valueOf(itemObj.Id), itemFieldDetails);
            }
         }
      }
      if (!itemFields.isEmpty())
      {
         externalPriceServiceInput.put('parent', parent);
         externalPriceServiceInput.put('itemList', itemList);
         externalPriceServiceInput.put('itemFieldsInfo', itemFields);
         //System.debug('--- setExternalPrices externalPriceServiceInput: ' + JSON.serialize(externalPriceServiceInput));
         externalPriceService.invokeMethod('setItemPrices', externalPriceServiceInput, externalPriceServiceOutput, externalPriceServiceOptions);
      }
   }
   }
}

Related Implementations

PricingElementServiceImplementationPricingElementServiceImplementation

 
Loading
Salesforce Help | Article