You are here:
Group Quote Line Items by Selling Model Apex Example for Document Generation
This Apex class groups quote line items by selling model type and returns a structured token format. During document generation, each group is rendered in a separate section.
Required Editions
| Available in: Lightning Experience |
| Available in: Professional, Enterprise, Unlimited, and Developer Editions |
For example, when the document generates, the plug-in groups quote line items by selling model type and returns the transformed token data. The document then renders separate sections for each selling model, confirming that the transform plug-in is working as expected and simplifying the template design.
global class EnumGroupsTransformation implements ind_docgen_api.OpenInterface {
global boolean invokeMethod(String methodName, Map<String, Object> inputMap, Map<String, Object> outMap) {
if (methodName == null || String.isEmpty(methodName)) {
return false;
}
switch on methodName {
when 'transformTokenData' {
return handleTransform(inputMap, outMap);
}
when else {
System.debug('Unknown method: ' + methodName);
return false;
}
}
}
private boolean handleTransform(Map<String, Object> inMap, Map<String, Object> outMap) {
if (inMap == null || inMap.get('TokenData') == null) {
return false;
}
Map<String, Object> inputMap = (Map<String, Object>) JSON.deserializeUntyped((String) inMap.get('TokenData'));
Map<String, Object> temp = new Map<String, Object>();
// Extract QuoteLineItems from inputMap
List<Object> quoteLineItems = (List<Object>) inputMap.get('QuoteLineItem');
if (quoteLineItems == null || quoteLineItems.isEmpty()) {
temp.put('Groups', new List<Object>());
String finalString = JSON.serialize(temp);
outMap.put('TokenData', finalString);
return true;
}
// Group items by SellingModelType
Map<String, List<Map<String, Object>>> groupedItems = new Map<String, List<Map<String, Object>>>();
for (Object item : quoteLineItems) {
Map<String, Object> lineItem = (Map<String, Object>) item;
String name = (String) lineItem.get('Name');
Decimal unitPrice = (Decimal) lineItem.get('UnitPrice');
String sellingModelType = (String) lineItem.get('SellingModelType');
if (sellingModelType != null && name != null && unitPrice != null) {
if (!groupedItems.containsKey(sellingModelType)) {
groupedItems.put(sellingModelType, new List<Map<String, Object>>());
}
Map<String, Object> itemObj = new Map<String, Object>();
itemObj.put('Name', name);
itemObj.put('UnitPrice', unitPrice);
groupedItems.get(sellingModelType).add(itemObj);
}
}
// Build the output Groups array
List<Object> groups = new List<Object>();
for (String modelType : groupedItems.keySet()) {
Map<String, Object> grp = new Map<String, Object>();
grp.put('Type', modelType);
grp.put('Items', groupedItems.get(modelType));
groups.add(grp);
}
temp.put('Groups', groups);
temp.put('Name', inputMap.get('Name'));
String finalString = JSON.serialize(temp);
outMap.put('TokenData', finalString);
return true;
}
}
Sample JSON Before Transformation
{
"QuoteLineItem":[
{
"UnitPrice":1150.0,
"SellingModelType":"TermDefined",
"Name":"Laptop Pro Bundle"
},
{
"UnitPrice":1149.0,
"SellingModelType":"OneTime",
"Name":"Desktop"
},
{
"UnitPrice":1149.0,
"SellingModelType":"OneTime",
"Name":"Desktop"
}
],
"Name":"Laptop Purchase Quote 4/19/2024"
}
Sample JSON After Custom Class Enum Transformation
{
"Name":"Laptop Purchase Quote 4/19/2024"
"Groups":[
{
"Items":[
{
"UnitPrice":1150.0,
"Name":"Laptop Pro Bundle"
},
],
"Type":"TermDefined"
},
{
"Items":[
{
"UnitPrice":1149.0,
"Name":"Desktop"
},
{
"UnitPrice":1149.0,
"Name":"Desktop"
}
],
"Type":"OneTime"
}
]
}
Did this article solve your issue?
Let us know so we can improve!

