# Slack Mass User Provisioning API

## Overview

The Slack Mass User Provisioning API allows bulk import and the ability to revoke user mappings between a Salesforce and Slack user. This feature is helpful for organizations with larger user lists.

## Prerequisites

1. Accept all Slack terms of service. ([Learn more](https://developer.salesforce.com/docs/platform/salesforce-slack-sdk/guide/enable-beta.md?q=enable))
2. A user who has the Connect Salesforce with Slack and the Manage Slack user mappings permissions enabled and assigned.
3. Access to a list of user mappings of Salesforce user id to Slack user id.

## Importing User Mappings

To import user mappings you must know the Salesforce and the Slack User IDs. Using the known IDs, create a list of user mappings with the Slack.UserMapping object.

```java
List<Slack.UserMapping> mappings = new List<Slack.UserMapping>();
mappings.add(new Slack.UserMapping(salesforceUserId, slackUserId));
```

After you have a list of mappings, use the Slack.UserProvisioningProvider to import the users for the workspace.

```java
Slack.UserProvisioningProvider.importUsers(mappings, workspaceId);
```

When the call to importUsers is complete, the API returns a result object that contains a list of successful mappings and a list of unsuccessful mappings.

```java
Slack.UserProvisioningResult result = Slack.UserProvisioningProvider.importUsers(mappings, workspaceId);
List<Slack.UserMappingResult> successfulMappings = result.getSuccessfulMappings();
List<Slack.UserMappingResult> failedMappings = result.getFailedMappings();
```

Each unsuccessful mapping has a failure code and message associated with it. Mappings can fail because of an invalid Salesforce ID, an invalid Slack ID, or a previous mapping.

```java
failedMapping.getError().getCode();
failedMapping.getError().getMessage();
```

Important: Use of Slack.UserProvisioningProvider is limited to Salesforce users with the Enable Slack User Pre-mapping API user permission. A user without this permission gets an exception during execution.

### Example - Import Users

This code sample uses the API to create a single user mapping. The Slack user ID and Salesforce user ID of the mapping is already known.

```java
// needed information - workspace id, salesforce user id, slack user id
String workspaceId = 'T0000000001';
String salesforceUserId = '005xx000001X7yjAAC';
String slackUserId = 'U000000001';

// create a list of user mappings between salesforce and slack ids
List<Slack.UserMapping> mappings = new List<Slack.UserMapping>();
mappings.add(new Slack.UserMapping(salesforceUserId, slackUserId));

// import the user mappings
Slack.UserProvisioningResult result =
    Slack.UserProvisioningProvider.importUsers(mappings, workspaceId);

// assert the results
List<Slack.UserMappingResult> successfulMappings = result.getSuccessfulMappings();
List<Slack.UserMappingResult> failedMappings = result.getFailedMappings();
System.debug('Number of successful mappings: ' + successfulMappings.size());
System.debug('Number of failed mappings: ' + failedMappings.size());
```

## Revoking User Mappings

Individual mappings can be revoked by providing the Salesforce user ID and the team or workspace that they’re associated with or the Slack user id. Using the known user id, you create a list of strings of either the Salesforce user id or the Slack user id.

```java
List<String> salesforceIdsToRevoke = new List<String>();
salesforceIdsToRevoke.add(salesforceUserId);
```

After you have a list of user ids, use the Slack.UserProvisioningProvider to revoke the users based on id type for the workspace.

```java
// revoke by salesforce id
Slack.UserProvisioningProvider.revokeUsersBySalesforceId(salesforceIdsToRevoke, workspaceId);
// revoke by slack id
Slack.UserProvisioningProvider.revokeUsersBySlackId(slackIdsToRevoke);
```

After the call to revoke users is complete, the API returns a result object containing a list of successful mappings and a list of unsuccessful mappings.

```java
Slack.UserProvisioningResult result = Slack.UserProvisioningProvider.revokeUsersBySalesforceId(salesforceIdsToRevoke, workspaceId);
List<Slack.UserMappingResult> successfulRevokedMappings = result.getSuccessfulMappings();
List<Slack.UserMappingResult> failedRevokedMappings = result.getFailedMappings();
```

Each unsuccessful revoked mapping has a failure code and message associated with it. Revoked mappings fail because of an invalid Salesforce ID, an invalid Slack ID, or a non-existent user mapping.

```java
failedMapping.getError().getCode();
failedMapping.getError().getMessage();
```

Important: Use of Slack.UserProvisioningProvider is limited to Salesforce users with the Enable Slack User Pre-mapping API user permission. A user without this permission gets an exception during execution.

### Example - Revoke by Salesforce User Id

```java
// needed information - workspace id, salesforce user id
String workspaceId = 'T0000000001';
String salesforceUserId = '005xx000001X7yjAAC';

// create a list of user mappings between salesforce and slack ids
List<String> salesforceIdsToRevoke = new List<String>();
salesforceIdsToRevoke.add(salesforceUserId);

// revoke the user mappings
Slack.UserProvisioningResult result =
Slack.UserProvisioningProvider.revokeUsersBySalesforceId(salesforceIdsToRevoke, workspaceId);

// assert the results
List<Slack.UserMappingResult> successfulRevokedMappings = result.getSuccessfulMappings();
List<Slack.UserMappingResult> failedRevokedMappings = result.getFailedMappings();
System.debug('Number of successful revoked mappings: ' + successfulRevokedMappings.size());
System.debug('Number of failed revoked mappings: ' + failedRevokedMappings.size());
```

### Example - Revoke by Slack User Id

```java
// needed information - slack user id
String slackUserId = 'U000000001';

// create a list of user mappings between salesforce and slack ids
List<String> slackIdsToRevoke = new List<String>();
slackIdsToRevoke.add(slackUserId);

// revoke the user mappings
Slack.UserProvisioningResult result =
Slack.UserProvisioningProvider.revokeUsersBySlackId(slackIdsToRevoke);

// assert the results
List<Slack.UserMappingResult> successfulRevokedMappings = result.getSuccessfulMappings();
List<Slack.UserMappingResult> failedRevokedMappings = result.getFailedMappings();
System.debug('Number of successful revoked mappings: ' + successfulRevokedMappings.size());
System.debug('Number of failed revoked mappings: ' + failedRevokedMappings.size());
```
