Tags API

The DE Apex API includes methods to create and update tag collections, assign / unassign tags and get user's tag assignments.

Create or Update Tags

global static void createOrUpdateTags(String tagCollectionName, Set<String> tagNames)

Creates a new Tag Collection or updates an existing Tag Collection with new Tags. If a Tag Collection already exists with the given name, only Tags that do not already exist will be added to the collection.

Example

Add a new Tag Collection called 'Languages' containing 'Spanish', 'French', 'German' and 'English' Tags

n2de.GlobalAPITags.createOrUpdateTags('Languages', new Set<String> {'Spanish', 'French', 'German', 'English'});
  • A Tag Collection name cannot be blank or longer than 30 characters
  • The number of Tags provided cannot exceed 1000
  • The length of each Tag cannot exceed 50 characters
  • The set of Tags provided must contain case-insensitive, unique values e.g. abc and ABC are considered to be non-unique tag names

 Assign User Tags

global static void assignUserTags(Map<String, Set<TagCollectionDTO>> usersWithTags)

Assign Tags from one or more collections to users. The parameter passed to this method is a map of User Ids and a Set of TagCollectionDTOs. Each TagCollectionDTO describes a Tag Collection and the Tags within that collection. Each Tag referenced in the TagCollectionDTO will be assigned to the user.

  • The total number of Tags can not exceed 5000
  • The users referenced in the input parameters must be active and part of a Distribution Team
  • The Tag Collections and Tags must already exist
Example

In the example below, Tags from two Tag Collections ('Languages' and 'Fuel Types') are assigned to two users: Andy and Barclay. Andy and Barclay are members of a Distribution Team and each underlying User object is active.

// Active User Ids that are members of a Distribution Team
String andyId = // Andy's User.Id;
String barclayId = // Barclay's User.Id;
// Tag assignments for Andy
n2de.TagCollectionDTO andyLanguages = new n2de.TagCollectionDTO('Languages', new Set<String> {'Spanish', 'German'});
n2de.TagCollectionDTO andyFuelTypes = new n2de.TagCollectionDTO('Fuel Types', new Set<String> {'Diesel', 'Petrol'});
// Tag assignments for Barclay
n2de.TagCollectionDTO barclayLanguages = new n2de.TagCollectionDTO('Languages', new Set<String> {'English', 'Portuguese'});
n2de.TagCollectionDTO barclayFuelTypes = new n2de.TagCollectionDTO('Fuel Types', new Set<String> {'Solar'});
// User Ids to TagCollectionDTOs map
Map<String, Set<n2de.TagCollectionDTO>> userTagsMap = new Map<String, Set<n2de.TagCollectionDTO>> {
  andyId => new Set<n2de.TagCollectionDTO> { andyLanguages, andyFuelTypes},
    barclayId => new Set<n2de.TagCollectionDTO> {barclayLanguages, barclayFuelTypes}
};
// Assign
n2de.GlobalAPITags.assignUserTags(userTagsMap);

 Unassign User Tags

global static void unassignUserTags(Map<String, Set<TagCollectionDTO>> usersWithTags)

Unassign Tags from one or more users. The parameter passed to this method is a map of User Ids and a Set of TagCollectionDTOs. Each TagCollectionDTO describes a Tag Collection and the Tags within that collection. Each Tag referenced in the TagCollectionDTO will be unassigned from users.

  • The total number of Tags to unassign can not exceed 5000
  • The users referenced in the input parameters must be active and part of a Distribution Team
  • The Tag Collections and Tags must already exist
Example

In the example below, Tags from two Tag Collections ('Languages' and 'Fuel Types') will be unassigned from two users: Andy and Barclay. Andy and Barclay are members of a Distribution Team and each underlying User object is active.

// Active User Ids that are members of a Distribution Team
String andyId = // Andy's User.Id;
String barclayId = // Barclay's User.Id;
// Tags to unassign from Andy
n2de.TagCollectionDTO andyLanguages = new n2de.TagCollectionDTO('Languages', new Set<String> {'Spanish'});
n2de.TagCollectionDTO andyFuelTypes = new n2de.TagCollectionDTO('Fuel Types', new Set<String> {'Diesel', 'Petrol'});
// Tags to unassign from Barclay
n2de.TagCollectionDTO barclayLanguages = new n2de.TagCollectionDTO('Languages', new Set<String> {'Portuguese'});
n2de.TagCollectionDTO barclayFuelTypes = new n2de.TagCollectionDTO('Fuel Types', new Set<String> {'Solar'});
// User Ids to TagCollectionDTOs map
Map<String, Set<n2de.TagCollectionDTO>> userTagsMap = new Map<String, Set<n2de.TagCollectionDTO>> {
  andyId => new Set<n2de.TagCollectionDTO> { andyLanguages, andyFuelTypes},
    barclayId => new Set<n2de.TagCollectionDTO> {barclayLanguages, barclayFuelTypes}
};
// Unassign
n2de.GlobalAPITags.unassignUserTags(userTagsMap);

 Get Assigned User Tags

global static Map<String, Set<TagCollectionDTO>> getAssignedUserTags()

Get the Tags assigned to users who are members of a Distribution Team. The getAssignedUserTags method returns a map of user Ids and a set of TagCollectionDTO. Each TagCollectionDTO describes a single Tag Collection and the Tags that are assigned to the user from that collection. Only assignments for members of active Distribution Teams, whose underlying User objects are also active, will be returned.

Example
// Get the assigned user tags
Map<String, Set<n2de.TagCollectionDTO>> userTags = n2de.GlobalAPITags.getAssignedUserTags();
// Iterate over users and display their assigned tags
for (String userId : userTags.keySet()) {
    Set<n2de.TagCollectionDTO> tagCollectionDTOS = userTags.get(userId);   
    System.debug('User Id : ' + userId + ' has the following tags assigned: ');
    for (n2de.TagCollectionDTO dto : tagCollectionDTOS) {
        System.debug('Tag Collection : ' + dto.TagCollectionName + ' ' + dto.TagNames);   
    }
}

How did we do?

Tags API Parameters

Contact