0

How to follow the navigational topics available in a community using soql. I want a custom list of topics in the community and a custom button to follow all of them from one single page.

I tried:

To fetch topics :

Class name: LC_onBoardingController
 public static ConnectAPI.ManagedTopicCollection getNavigationTopics(){
        System.debug('RES***1');
        return ConnectAPI.ManagedTopics.getManagedTopics('0DB2200000000WlGAI',ConnectApi.ManagedTopicType.Navigational);//0DB0c0000008On7GAE
    }
Map<ID,Object> topicMap = new  Map<ID,Object>();
for(ConnectApi.ManagedTopic mtopic : LC_onBoardingController.getNavigationTopics().managedTopics){
topicMap.put(mtopic.topic.id,mtopic.topic)
}

To follow the topics i retreived from the above code I used this :

 List<EntitySubscription> listofTopics= new List<EntitySubscription>();
        for(String top : topicid){
            EntitySubscription Entity = new EntitySubscription();
            Entity.SubscriberId = userId;  
            Entity.parentId = top;
            listofTopics.add(Entity);
        }
        System.debug('RES***1'+listofTopics);
        Database.insert(listofTopics,false);

Even after successful DML on going to the Topics page in the community and that particular topic the "Follow" option is enabled and it did not turn to "Following".But in database record was created for the object EntitySubscription with that topic id.Is there some other method to follow it?

1 Answer 1

3

Following a topic is specific to a community. For example, you can follow a topic in community 1 but not follow it in community 2.

Entity Subscription object has network Id field which determines if the topic is followed in a specific community(Network Id = communityId) or internal salesforce(NetworkId = null)

Here is a sample code which solves your purpose

public with sharing class Topics {
    private Set<Id> topicIdsToFollow;
    private Id networkId;

    public Topics(Set<Id> topicIdsToFollow) {
        this.topicIdsToFollow = topicIdsToFollow;
        this.networkId = getNetworkId('my Community');
    }


    public void follow() {
        List<EntitySubscription> topicToFollow= new List<EntitySubscription>();
        Id userId = UserInfo.getUserId();

        for(Id topicId : topicIdsToFollow) {
            EntitySubscription subscription = new EntitySubscription();

            subscription.SubscriberId = userId; 
            subscription.NetworkId = networkId; 
            subscription.parentId = topicId;
            topicToFollow.add(subscription);
        }

         Database.insert(topicToFollow, false);
    }


    private Id getNetworkId(String communityName) {
        return [SELECT Id FROM Network WHERE Name = :communityName].Id;
    }
}

Then use it in your class like: new Topics(setOfTopicIdsToFollow).follow();

1
  • 1
    Yes,I just figured out that I missed to update the community Id.Thanks a lot.Its working as expected. Commented Feb 26, 2020 at 17:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .