---
schema_version: "1"
id: 85efdaa29e9c2d179ef2156335e80d2e1b588bc2be945b2b1d4dbb3f10b05f6e
title: CRM Analytics Apex Lens
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/analytics/bi_dev_guide_sdk/guides/bi-sdk-apex-lens.md
canonical_url: https://developer.salesforce.com/docs/analytics/bi-dev-guide-sdk/guide/bi-sdk-apex-lens.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 25374afeb771a9525be5b920107c70675cdda0b090198d1e413ed46268225e27
products:
  - Analytics SDK Developer Guide
  - analytics
---

# 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

```nolang
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

```nolang
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

```nolang
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

```nolang
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](https://developer.salesforce.com/docs/atlas.en-us.bi_dev_guide_rest.meta/bi_dev_guide_rest/bi_resources_lenses.htm).
