Use the Templates class to retrieve a collection of CRM Analytics templates, describe a single template and template configuration.
The CRM Analytics Wave.Templates class provides access from Apex to CRM Analytics templates. The methods are annotated with @AuraEnabled for use in Lightning Web Components (LWC).
Wave.Templates collection methods
Map<String, Object> getTemplates()
Retrieves a collection of templates.
Map<String, Object> getTemplates(Wave.TemplatesSearchOptions searchOptions)
Retrieves a collection of templates using search options.
Apex Class Example
public with sharing class TemplatesController {
public TemplatesController() {
}
@AuraEnabled(cachable=true)
public static List<String, Object> getTemplateNames() {
// This has filterGroup, type, and options; all fields are optional/nullable
Wave.TemplatesSearchOptions options = new Wave.TemplatesSearchOptions();
options.type = 'app';
options.filterGroup = 'small';
options.options = 'ViewOnly';
// Pass null to get the default search options or leave it off completely to return
// a collection with no search options
Map<String, Object> templates = Wave.Templates.getTemplates(options);
// templates is the JSON response as an Apex Map (from JSON.deserializedUntyped), which
// you can pull fields from
List<Object> templateList = (List<Object>) templates.get('templates');
List<String> names = new List<String>();
for (Object templateObj : templateList) {
names.add((String) ((Map<String, Object>) templateObj).get('name'));
}
return names;
}
}
LWC Example
import {LightningElement, wire} from "lwc";
import getTemplates from "@salesforce/apex/Wave.Templates.getTemplates";
export default class Templates extends LightningElement {
results;
@wire(getTemplates, {
options: {
// All are optional
type = 'app'
}
})
// can also use these
// @wire(getTemplates, { options: {} })
// @wire(getTemplates, {})
// @wire(getTemplates)
// @wire(getTemplates, { options: {'$options'} }) // with a binding
onTemplates({ data, error }) {
if (error) {
this.results = "Error:\n" + JSON.stringify(error, undefined, 2);
} else if (data) {
// data is the TemplateCollectionRepresentation JSON object
this.results = 'Template names: ' + data.templates.map(l => ${l.name}).join(', ');
} else {
this.results = "No data";
}
}
}
Wave.Templates describe methods
Map<String, Object> getTemplate(String templateIdOrApiName)
Retrieves a template by ID or the API name.
Map<String, Object> getTemplateConfig(String templateIdOrApiName)
Retrieves a template configuration by ID or the API name.
Apex Class Example
public with sharing class TemplateController {
public TemplateController() {
}
@AuraEnabled(cacheable=true)
public static Map<String, Object> getTemplate(String idOrName) {
Map<String, Object> template = Wave.Templates.getTemplate(idOrName);
return template;
}
}
LWC Example
import {LightningElement, wire} from "lwc";
import getTemplate from "@salesforce/apex/Wave.Templates.getTemplate";
export default class Lens extends LightningElement {
templateIdOrApiName; // set this to the ID or name you want to retrieve
results;
@wire(getTemplate, {
templateIdOrApiName: '$templateIdOrApiName'
})
onTemplate({data, error}) {
if (error) {
this.results = "Error:\n" + JSON.stringify(error, undefined, 2);
} else if (data) {
// data is the TemplateRepresentation JSON object
this.results = 'Template: ${data.name} ${data.id}';
} else {
this.results = "No data";
}
}
}
For information on request parameters and the Template JSON responses, see the Analytics REST API Developer Guide: Templates List Resource. To see the full reference for the Templates Apex class, see the Apex Reference Guide: wave Namespace.