CRM Analytics Apex Lens

Use the Lenses class to retrieve a collection of CRM Analytics lens assets and to describe a single lens asset.

The CRM Analytics Wave.Lenses class provides access from Apex to the CRM Analytics lens assets.

Wave.Lenses collection methods 

Map<String, Object> getLenses()

Retrieves a collection of lenses.

Map<String, Object> getLenses(String searchOptions)

Retrieves a collection of lenses using search options.

Apex Class Example 

1public with sharing class LensController {
2  public LensController() {
3  }
4
5  @AuraEnabled(cachable=true)
6  public static Map<String, Object> getLenses() {
7    // This has all fields available, plus filterGroup; all fields are optional/nullable
8    Wave.LensesSearchOptions options = new Wave.LensesSearchOptions();
9    options.q = 'widget';
10    options.filterGroup = 'supplemental';
11    options.scope = 'CreatedByMe';
12    options.page = null;
13    options.sortParam = 'Name';
14
15    // Pass null to get the default search options or leave it off completely to return
16    // a collection with no search options
17    Map<String, Object> lensesJson = Wave.Lenses.getLenses(options);
18
19    // lensesJson is the JSON response as an Apex Map (from JSON.deserializedUntyped), which
20    // you can pull fields from
21    return lensesJson;
22  }
23}

LWC Example 

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

Wave.Lenses describe methods 

Map<String, Object> getLens(String lensIdOrApiName)

Retrieves a lens by ID or the API name.

Map<String, Object> getLens(String lensIdOrApiName, String filterGroup)

Retrieves a lens by ID or the API name and a filterGroup parameter.

Apex Class Example 

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

LWC Example 

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

For information on request parameters and the Lens JSON responses, see the Analytics REST API Developer Guide: Lenses List Resource.