CRM Analytics Apex Templates

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 

1public with sharing class TemplatesController {
2    public TemplatesController() {
3    }
4
5    @AuraEnabled(cachable=true)
6    public static List<String, Object> getTemplateNames() {
7      // This has filterGroup, type, and options; all fields are optional/nullable
8      Wave.TemplatesSearchOptions options = new Wave.TemplatesSearchOptions();
9      options.type = 'app';
10      options.filterGroup = 'small';
11      options.options = 'ViewOnly';
12
13      // Pass null to get the default search options or leave it off completely to return
14      // a collection with no search options
15      Map<String, Object> templates = Wave.Templates.getTemplates(options);
16
17      // templates is the JSON response as an Apex Map (from JSON.deserializedUntyped), which
18      // you can pull fields from
19      List<Object> templateList = (List<Object>) templates.get('templates');
20      List<String> names = new List<String>();
21      for (Object templateObj : templateList) {
22        names.add((String) ((Map<String, Object>) templateObj).get('name'));
23      }
24      return names;
25    }
26}

LWC Example 

1import {LightningElement, wire} from "lwc";
2import getTemplates from "@salesforce/apex/Wave.Templates.getTemplates";
3
4export default class Templates extends LightningElement {
5  results;
6
7  @wire(getTemplates, {
8    options: {
9      // All are optional
10      type = 'app'
11    }
12  })
13
14  // can also use these
15  // @wire(getTemplates, { options: {} })
16  // @wire(getTemplates, {})
17  // @wire(getTemplates)
18  // @wire(getTemplates, { options: {'$options'} }) // with a binding
19  onTemplates({ data, error }) {
20    if (error) {
21      this.results = "Error:\n" + JSON.stringify(error, undefined, 2);
22    } else if (data) {
23      // data is the TemplateCollectionRepresentation JSON object
24      this.results = 'Template names: ' + data.templates.map(l => ${l.name}).join(', ');
25    } else {
26       this.results = "No data";
27    }
28  }
29}

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 

1public with sharing class TemplateController {
2    public TemplateController() {
3    }
4
5    @AuraEnabled(cacheable=true)
6    public static Map<String, Object> getTemplate(String idOrName) {
7      Map<String, Object> template = Wave.Templates.getTemplate(idOrName);
8      return template;
9    }
10}

LWC Example 

1import {LightningElement, wire} from "lwc";
2import getTemplate from "@salesforce/apex/Wave.Templates.getTemplate";
3
4export default class Lens extends LightningElement {
5    templateIdOrApiName; // set this to the ID or name you want to retrieve
6
7    results;
8
9    @wire(getTemplate, {
10      templateIdOrApiName: '$templateIdOrApiName'
11    })
12    onTemplate({data, error}) {
13      if (error) {
14        this.results = "Error:\n" + JSON.stringify(error, undefined, 2);
15      } else if (data) {
16        // data is the TemplateRepresentation JSON object
17        this.results = 'Template: ${data.name} ${data.id}';
18      } else {
19        this.results = "No data";
20      }
21    }
22}

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.