Loading
Salesforce Document Generation
Build a Product Bundle Hierarchy from Token Data Apex Example for Document Generation

Build a Product Bundle Hierarchy from Token Data Apex Example for Document Generation

This Apex class transforms flat product token data into a parent-child hierarchy based on parent product relationships. During document generation, parent and child items are bundled together with nested child items.

Required Editions

Available in: Lightning Experience
Available in: Professional, Enterprise, Unlimited, and Developer Editions

For example, a quote includes a bundled product with multiple add-ons. Context service fetches all line items without hierarchy. The transform plug-in builds a bundle structure by attaching child items to their parent product. The template then shows the bundle with its nested child items, matching the expected layout.

Note
Note Make sure to include repeater tokens for each hierarchy level that you want to render. The transform plug-in can build deeper hierarchies, but the template controls which levels appear in the generated document.
public class CustomiseTokenData implements ind_docgen_api.OpenInterface{
public 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){
String tokenData = (String) inMap.get('TokenData');
if (inMap == null || inMap.get('TokenData') == null) {
return false;
}
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(tokenData);
Map<String,List<Object>> parentToChild = new Map<String,List<Object>>();

List<Object> products =(List<Object>) m.get('products');
List<Object> updatedProducts =new List<Object>();
for(Object product: products){
Map<String,Object> prodMap = (Map<String,Object>)product;
String id = (String)prodMap.get('Id');

String parentId = (String)prodMap.get('ParentId');
if(parentId!=null){
List<Object> children = (List<Object>)parentToChild.get(parentId);
if(children == null){
children = new List<Object>();
}
children.add(product);
parentToChild.put(parentId,children);
} else{
updatedProducts.add(product);
}
}
for(Object product:updatedProducts){
fetchChildren(product, parentToChild);
}

m.put('products', updatedProducts);


String finalString = JSON.serialize(m);
outMap.put('TokenData',finalString);
return true;
}


Object fetchChildren(Object product,Map<String,Object> parentToChild){
Map<String,Object> productMap = (Map<String,Object>)product;
String id = (String)productMap.get('Id');
Boolean keyExist = parentToChild.containsKey(id);
if(keyExist){
List<Object> children = (List<Object>)parentToChild.get(id);
List<Object> Existingchildren = (List<Object>)productMap.get('child');
if(Existingchildren == null){
Existingchildren = new List<Object>();
}
for(Object child:children){
Object updatedMap = fetchChildren(child, parentToChild);
Existingchildren.add(updatedMap);
}
productMap.put('child',Existingchildren);
}
return productMap;
}

}
 
Chargement
Salesforce Help | Article