1

I have an Apex class TopicController that has a method that returns a list of Navigational Managed Topics:

public with sharing class TopicController {

  // Returns a list of all Navigational Managed Topics

  @AuraEnabled(cacheable=true)
  public static List<ConnectApi.ManagedTopic> getManagedTopics() {
    ConnectAPI.ManagedTopicCollection mTopics = ConnectAPI.ManagedTopics.getManagedTopics(
      Network.getNetworkId(),
      ConnectApi.ManagedTopicType.Navigational,
      2
    );

    return mTopics.managedTopics;
  }
}

I'm trying to create a test for this method:

@isTest
private class TopicControllerTest {


  @isTest
  static void validateGetManagedTopics() {

    String topicName = 'Test topic';

    Topic topic = new Topic(Name = topicName);

    insert topic;

    ConnectApi.ManagedTopic managedTopic = ConnectApi.ManagedTopics.createManagedTopic(
      null,
      topic.Id,
      ConnectApi.ManagedTopicType.Navigational
    );

    List<ConnectApi.ManagedTopic> managedTopics = LotusTopicCatalogController.getManagedTopics();

    System.assertEquals(1, managedTopics.size());
  }
}

But when running this test in a scratch org, I'm getting the following error:

ConnectApi.ConnectApiException: Managed Topics is not available in internal community

As I understand, there is no way to create a Community dynamically in the test method.

What is the correct way to write this test?

1 Answer 1

0

Can you try the below code and see if it works for you. Basically added a try-catch block so that the test passes and allows to import code from Sandbox into Production

@isTest
private class TopicControllerTest {
  @isTest
  static void validateGetManagedTopics() {
    String communityId;
    String topicName ='Test topic';
    String topicId='123456789';
 try {
// Try to get the Community ID for Live environment:
communityId = [Select Id from Network where Name = 'customerPortal'].Id;
} catch (Exception e) {
//If we get an Exception, get the Community ID for the Sandbox environment:
communityId = [Select Id from Network where Name = 'customerservice'].Id;
    }

   //Using ConnectApi.ManagedTopics.createManagedTopicByName to create the managed topics as they cannot be created via standard insert operations
    ConnectApi.ManagedTopics.createManagedTopicByName(communityId,TopicName, ConnectApi.ManagedTopicType.Navigational) ;
    List<ConnectApi.ManagedTopic> managedTopics = LotusTopicCatalogController.getManagedTopics();
    System.assertEquals(1, managedTopics.size());
      }
    }
2
  • Swetha, the issue is that the test is running during the 2GP package creation. During this process, a new scratch org is automatically created, and there is no Community out of the box. So we can't use SOQL as in your example. Dec 4, 2020 at 15:38
  • @AndreyMetelsky Thanks for clarifying. Not sure if it is possible to test the managed Topics without having a community. The documentation(developer.salesforce.com/docs/atlas.en-us.api_meta.meta/…) says "The related network must exist before you deploy managed topics."The network here represents a community. Dec 23, 2020 at 10:53

You must log in to answer this question.

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