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

public with sharing class LensController {
  public LensController() {
  }

  @AuraEnabled(cachable=true)
  public static Map<String, Object> getLenses() {
    // This has all fields available, plus filterGroup; all fields are optional/nullable
    Wave.LensesSearchOptions options = new Wave.LensesSearchOptions();
    options.q = 'widget';
    options.filterGroup = 'supplemental';
    options.scope = 'CreatedByMe';
    options.page = null;
    options.sortParam = 'Name';

    // Pass null to get the default search options or leave it off completely to return
    // a collection with no search options
    Map<String, Object> lensesJson = Wave.Lenses.getLenses(options);

    // lensesJson is the JSON response as an Apex Map (from JSON.deserializedUntyped), which
    // you can pull fields from
    return lensesJson;
  }
}

LWC Example

import {LightningElement, wire} from "lwc";
import getLenses from "@salesforce/apex/Wave.Lenses.getLenses";

export default class Lenses extends LightningElement {
  results;

  @wire(getLenses, {
    options: {
      // All are optional
      filterGroup = "Supplemental",
      sortParam = "Name"
    }
  })
  // can also use these
  // @wire(getLenses, { options: {} })
  // @wire(getLenses, {})
  // @wire(getLenses)
  // @wire(getLenses, { options: {'$options'} }) // with a binding
  onLenses({data, error}) {
    if (error) {
      this.results = "Error:\n" + JSON.stringify(error, undefined, 2);
    } else if (data) {
      // data is the LensCollectionRepresentation JSON object
      this.results = "Lenses: " + data.lenses.map(l => `${l.name} {$l.id}`).join(", ");
    } else {
      this.results = "No data";
    }
  }
}

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

public with sharing class LensController {
  public LensController() {
  }

  @AuraEnabled(cacheable=true)
  public static Map<String, Object> getLens(String idOrName) {
    Map<String, Object> lens = Wave.Lenses.getLens(idOrName);
    return lens;
  }
}

LWC Example

import {LightningElement, wire} from "lwc";
import getLenses from "@salesforce/apex/Wave.Lenses.getLens";

export default class Lens extends LightningElement {
  lensIdOrApiName; // set this to the ID or name you want to retrieve

  results;

  @wire(getLens, {
    lensIdOrApiName: '$lensIdOrApiName'
  })
  onLens({data, error}) {
    if (error) {
      this.results = "Error:\n" + JSON.stringify(error, undefined, 2);
    } else if (data) {
      // data is the LensRepresentation JSON object
      this.results = `Lens: ${data.name} ${data.id})`;
    } else {
      this.results = "No data";
    }
  }
}

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