Globals Class

The Globals class allows external Apex code to access some Coveo for Salesforce features.

generatePlatformToken

Generates a platform token that can be used to invoke any Coveo API except the Search API. Under the hood, this function calls the Coveo Platform API /rest/organizations/{organizationId}/tokens endpoint.

String token = CoveoV2.Globals.generatePlatformToken(<PARAMS>);

Where the <PARAMS> (Object, Optional) parameter lets you pass additional content and request the privileges necessary for the services you will be invoking with the platform token.

The following example shows an Apex implementation of the API call to generate a platform token with the corresponding options. For more information, see the Platform Tokens Swagger documentation.

Example
CoveoV2.PlatformToken.SearchTokenOptions searchOptions = new CoveoV2.PlatformToken.SearchTokenOptions();
searchOptions.userIds.add(new CoveoV2.UserIdentity('myAdditionalIdentity'));

Map<String, Object> tokenOptions = new Map<String, Object> {
    'search' => searchOptions,
    'privileges' => new List<CoveoV2.PlatformToken.TokenPrivilege> {
        new CoveoV2.PlatformToken.TokenPrivilege('CUSTOMER_SERVICE', 'USE_CASE_ASSIST', 'ENABLE'),
        new CoveoV2.PlatformToken.TokenPrivilege('USAGE_ANALYTICS', 'ANALYTICS_DATA  ', 'EDIT')
    }
};
String myToken = CoveoV2.Globals.generatePlatformToken(tokenOptions);

generateSearchToken

Generates a search token that can then be used to invoke the Coveo Search REST API in Coveo. Under the hood, this calls the Coveo Search API /rest/search/v2/token endpoint.

String token = CoveoV2.Globals.generateSearchToken(<PARAMS>);

Where the <PARAMS> (Object, Optional) parameter allows you to pass additional information that should be added to the search token.

Example:

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'filter' => '@source==KnowledgeBase',
    'pipeline' => 'InternalSearch'
});

additionalUserIdentity (Optional)

Specifies a list of identities to add to the token. For example, this can be used when you have a specific identity that has access to documents that everyone should be able to see.

Example:

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'additionalUserIdentities' => new List<CoveoV2.UserIdentity>{
        new CoveoV2.UserIdentity('john@doe.com'),
        new CoveoV2.UserIdentity('jane@doe.com','Email Security Provider'),
        new CoveoV2.UserIdentity('jane@doe.com','Email Security Provider','User')
    }
});

The UserIdentity class used to add user identities uses the following constructor:

UserIdentity(<NAME>, <PROVIDER>, <TYPE>)

Where:

  • <NAME>: (String, Required) The identity provider to add.

  • <PROVIDER>: (String, Optional) The security provider of your user identity.

  • <TYPE>: (String, Optional) The virtual group your identity belongs to.

Note

For more information on user identities, see Coveo management of security identities and item permissions.

filter (Optional)

The filter query expression to apply when authenticating a query with this search token.

This expression will be merged with the constant part of the query expression (cq) using an AND operator.

The filter can also be used in query pipeline condition statements (e.g.,when $constantQuery contains \"@source==KnowledgeBase\").

Example: @source==KnowledgeBase

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'filter' => '@source==KnowledgeBase'
});

pipeline (Optional)

The name of the query pipeline to use when authenticating a query with this search token.

This query pipeline will take precedence over the possible output of all other query pipeline routing mechanisms when using this search token (see Query Pipeline Routing Mechanisms and Rules).

Example: InternalSearch

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'pipeline' => 'InternalSearch'
});

searchHub (Optional)

The name of the search hub to enforce when authenticating a query with this search token.

This value will override the searchhub parameter of the query itself, and will be passed as the originLevel1 property value when logging usage analytics search events.

The search hub can also be used in query pipeline condition statements (e.g., when $searchhub is \"CommunityHub\").

Example: SupportHub

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'searchHub' => 'SupportHub'
});

userDisplayName (Optional)

The userDisplayName to pass when logging usage analytics search events.

This information is leveraged in the Analytics section of the Coveo Cloud administration console.

Example: Alice Smith

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'userDisplayName' => 'Alice Smith'
});

userGroups (Optional)

The userGroups to pass when logging usage analytics search events.

This information is leveraged in the Analytics section of the Coveo Cloud administration console.

User groups can be also be used in query pipeline condition statements (e.g., when $groups contains \"Employees\").

Example: ["Tech support agents", "Employees"]

By default, the service uses the user profile name inside Salesforce.

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'userGroups' => new List<String>{ "Tech support agents","Employees" }
});

validFor (Optional)

The number of milliseconds the search token will remain valid for once it has been created.

Minimum value: 900000 (i.e., 15 minutes)

Maximum/default: 86400000 (i.e., 24 hours)

String token = CoveoV2.Globals.generateSearchToken(new Map<String, Object> {
    'validFor' => '3600000'
});