Relationship between Topics

At present, Topic + SubTopic feature is only supported in Communities. This can be achieved by Navigational Topics where you can specify SubTopics for the relevant Topics which helps a lot in finding the relevant data.

So, how to create Topic and SubTopic? – I will not be going in detail as it can be done with just few Point-N -Clicks :), anyhow here is the link.

But I will be sharing a code snippet which gives you list of sub-topics mapped against the respective topics (you can modify as you like)

In order to achieve this, Salesforce provides a set of classes (under ConnectAPI namespace) which can be used to identify the relationship between topics and their subtopics.

Here is a list of available classes present in ConnectApi Namespace. Out of this list I will be using “ManagedTopics” and “Topics” Class.

Pre-requisite: Community should be enabled and few topics and sub-topics are created using the UI as per the link shared above.

Step 1: Identifying the community Id


SELECT Id, Name, Description, Status FROM Network

Step 2: Pulling the list of available topics present for a specific community

public Map<String, ConnectAPI.Topic> getAllTopics(String communityId){
ConnectAPI.TopicPage tPage = ConnectAPI.Topics.getTopics(communityId);
Map<String, ConnectAPI.Topic> allTopicsMap =
          new Map<String, ConnectAPI.Topic>();

for(ConnectAPI.Topic topic : tPage.topics){
allTopicsMap.put(topic.Name, topic);
}
system.debug('topic key ' + allTopicsMap.keySet());
}

Step 3: Building the List of subtopics for respective Parent and Child topics for the respective community

<pre>public  void getParentChildSubTopics(String communityId, Integer depth){
ConnectApi.ManagedTopicCollection  managedTopic =
           ConnectApi.ManagedTopics.getManagedTopics(
                   communityId,ConnectApi.ManagedTopicType.Navigational,depth
               );
// you can set this map in constructor and can return this varible
Map<String, Map<String, List<String>>> pChildSubChildTopicsMap =
new Map<String, Map<String, List<String>>>();
Map<String, List<String>> childTopicsMap;
List<String> subChildTopicslist;

for(ConnectApi.ManagedTopic parentTopic : managedTopic.managedTopics){
childTopicsMap = new Map<String, List<String>>();
for(ConnectApi.ManagedTopic childTopic : parentTopic.children){
subChildTopicslist = new List<String>();
for(ConnectApi.ManagedTopic subChildTopic : childTopic.children){
subChildTopicslist.add(subChildTopic.Topic.Name);
}
childTopicsMap.put(childTopic.Topic.Name, subChildTopicslist);
}
pChildSubChildTopicsMap.put(parentTopic.Topic.Name, childTopicsMap);
}
//system.debug('parent ' + pChildSubChildTopicsMap.keyset());
//system.debug('Child ' + pChildSubChildTopicsMap.get('Google').keyset());
//system.debug('Sub-Child ' + pChildSubChildTopicsMap.get('Google').get('Gmail'));
}

Note:

1) Sub-Child will be empty list if Child doesn’t have any subtopics to it.

2) And from Step 2, you can access all properties of ConnectApi.Topic by passing the Topic Name.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s