You are here:
Group Quote Line Items by Price Range Apex Example for Document Generation
This Apex class groups quote line items into predefined unit price ranges and returns a structured token format. During document generation, each defined price range 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 into predefined price ranges, such as prices less than 100, 100–500, and greater than 500. The document then renders separate sections for low-cost, mid-range, and high-value items.
global class PriceGroupsTransformation 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 unit price ranges
Map<String, List<Map<String, Object>>> groupedItems = new Map<String, List<Map<String, Object>>>();
// Initialize price range groups
groupedItems.put('< 100', new List<Map<String, Object>>());
groupedItems.put('100 - 500', new List<Map<String, Object>>());
groupedItems.put('> 500', new 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');
if (name != null && unitPrice != null) {
String priceRange;
if (unitPrice < 100) {
priceRange = '< 100';
} else if (unitPrice >= 100 && unitPrice <= 500) {
priceRange = '100 - 500';
} else {
priceRange = '> 500';
}
Map<String, Object> itemObj = new Map<String, Object>();
itemObj.put('Name', name);
itemObj.put('UnitPrice', unitPrice);
groupedItems.get(priceRange).add(itemObj);
}
}
// Build the output Groups array
List<Object> groups = new List<Object>();
for (String priceRange : groupedItems.keySet()) {
Map<String, Object> grp = new Map<String, Object>();
grp.put('Type', priceRange);
grp.put('Items', groupedItems.get(priceRange));
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"
}
]
}
Ratkaisiko tämä artikkeli ongelmasi?
Anna palautetta, jotta voimme kehittyä!

