You are here:
DiscountActivation
Activates cart-added discounts on checkout, activation of a frame contract, or both. Changes the value of the discount’s Status__c field to Activated.
Loosely typed
This interface is triggered upon cart checkout if discounts are present in the cart or if a frame contract specifies discounts.
This interface is called by the cart’s checkout APICheck Status of the Cart or the frame contract's OmniScript.
Access |
Signature |
|---|---|
global |
|
DefaultDiscountActivationImplementationDefaultDiscountActivationImplementation
None
cartId
Required
Type: Id
The ID of the cart being checked out. For a contract, this is the ID of the cart that created the frame contract.
discountIds
Required
Type: List<Id>
The IDs of the cart-added discounts that need to be activated. Overrides the discountTypes parameter.
discountTypes
Required
Type: List<String>
A list of discount types from the cart that need to be activated. Possible values are Contract, Account, and Order.
None
global class SampleDiscountActivationImplementation implements VlocityOpenInterface {
public static final String CART_ID_KEY = 'cartId';
public static final String DISCOUNT_TYPES_KEY = 'discountTypes';
public static final String DISCOUNT_IDS_KEY = 'discountIds';
global Boolean invokeMethod(String methodName,
Map<String, Object> input,
Map<String, Object> output,
Map<String, Object> options) {
try {
if (methodName == 'executeActivation') {
executeActivation(input, output, options);
}
return True;
} catch (Exception ex) {
System.debug(LoggingLevel.ERROR, ex);
System.debug(LoggingLevel.ERROR, ex.getStackTraceString());
output.put('errors', true);
output.put('errorResult', ex.getMessage());
return false;
}
}
private void executeActivation(Map<String, Object> input, Map<String, Object> output, Map<String, Object> options) {
// Get cart Id, discountIds, and discountTypes
Id cartId = (Id) input.get(CART_ID_KEY);
List<Id> discountIds = (List<Id>) input.get(DISCOUNT_IDS_KEY);
LIST<String> discountTypes = (List<String>) input.get(DISCOUNT_TYPES_KEY);
if (cartId == null) {
return;
}
//
String query = getDefaultDiscountsQuery(cartId);
if (discountIds != null && !discountIds.isEmpty()) {
//
query += ' AND Id IN :discountIds';
} else if (discountTypes != null && !discountTypes.isEmpty()) {
//
query += ' AND DiscountType__c IN :discountTypes';
}
List<SObject> discountSObjList = Database.query(query);
if (discountSObjList != null && !discountSObjList.isEmpty()) {
updateDiscounts(discountSObjList, 'Activated');
update discountSObjList;
}
}
private void updateDiscounts(List<SObject> discountList, String status) {
if (discountList != null) {
for (SObject oneDiscount : discountList) {
oneDiscount.put('Status__c', status);
}
}
}
private String getDefaultDiscountsQuery(Id cartId) {
String cartObjName = String.valueOf(cartId.getSobjectType());
String discountObjName = cartObjName + 'Discount__c';
List<String> fields = new List<String> {'Id', 'Name', 'Action__c', 'DiscountType__c', 'Status__c'};
String query = 'SELECT ' + String.join(fields, ',') + ' FROM ' + discountObjName;
query += ' WHERE ' + cartObjName + 'Id__c = :cartId AND Status__c = \'Not Activated\'';
return query;
}
}
