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)
  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.

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

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

1Slack.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.

1Slack.UserProvisioningResult result = Slack.UserProvisioningProvider.importUsers(mappings, workspaceId);
2List<Slack.UserMappingResult> successfulMappings = result.getSuccessfulMappings();
3List<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.

1failedMapping.getError().getCode();
2failedMapping.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.

1// needed information - workspace id, salesforce user id, slack user id
2String workspaceId = 'T0000000001';
3String salesforceUserId = '005xx000001X7yjAAC';
4String slackUserId = 'U000000001';
5
6// create a list of user mappings between salesforce and slack ids
7List<Slack.UserMapping> mappings = new List<Slack.UserMapping>();
8mappings.add(new Slack.UserMapping(salesforceUserId, slackUserId));
9
10// import the user mappings
11Slack.UserProvisioningResult result =
12    Slack.UserProvisioningProvider.importUsers(mappings, workspaceId);
13
14// assert the results
15List<Slack.UserMappingResult> successfulMappings = result.getSuccessfulMappings();
16List<Slack.UserMappingResult> failedMappings = result.getFailedMappings();
17System.debug('Number of successful mappings: ' + successfulMappings.size());
18System.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.

1List<String> salesforceIdsToRevoke = new List<String>();
2salesforceIdsToRevoke.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.

1// revoke by salesforce id
2Slack.UserProvisioningProvider.revokeUsersBySalesforceId(salesforceIdsToRevoke, workspaceId);
3// revoke by slack id
4Slack.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.

1Slack.UserProvisioningResult result = Slack.UserProvisioningProvider.revokeUsersBySalesforceId(salesforceIdsToRevoke, workspaceId);
2List<Slack.UserMappingResult> successfulRevokedMappings = result.getSuccessfulMappings();
3List<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.

1failedMapping.getError().getCode();
2failedMapping.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 

1// needed information - workspace id, salesforce user id
2String workspaceId = 'T0000000001';
3String salesforceUserId = '005xx000001X7yjAAC';
4
5// create a list of user mappings between salesforce and slack ids
6List<String> salesforceIdsToRevoke = new List<String>();
7salesforceIdsToRevoke.add(salesforceUserId);
8
9// revoke the user mappings
10Slack.UserProvisioningResult result =
11Slack.UserProvisioningProvider.revokeUsersBySalesforceId(salesforceIdsToRevoke, workspaceId);
12
13// assert the results
14List<Slack.UserMappingResult> successfulRevokedMappings = result.getSuccessfulMappings();
15List<Slack.UserMappingResult> failedRevokedMappings = result.getFailedMappings();
16System.debug('Number of successful revoked mappings: ' + successfulRevokedMappings.size());
17System.debug('Number of failed revoked mappings: ' + failedRevokedMappings.size());

Example - Revoke by Slack User Id 

1// needed information - slack user id
2String slackUserId = 'U000000001';
3
4// create a list of user mappings between salesforce and slack ids
5List<String> slackIdsToRevoke = new List<String>();
6slackIdsToRevoke.add(slackUserId);
7
8// revoke the user mappings
9Slack.UserProvisioningResult result =
10Slack.UserProvisioningProvider.revokeUsersBySlackId(slackIdsToRevoke);
11
12// assert the results
13List<Slack.UserMappingResult> successfulRevokedMappings = result.getSuccessfulMappings();
14List<Slack.UserMappingResult> failedRevokedMappings = result.getFailedMappings();
15System.debug('Number of successful revoked mappings: ' + successfulRevokedMappings.size());
16System.debug('Number of failed revoked mappings: ' + failedRevokedMappings.size());

Beta Feature

This feature is not generally available. It is not part of your purchased Services. This feature is subject to change, may be discontinued with no notice at any time in SFDC’s sole discretion, and SFDC may never make this feature generally available. Make your purchase decisions only on the basis of generally available products and features. This feature is made available on an AS IS basis and use of this feature is at your sole risk.