function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joe BrodarJoe Brodar 

TopicAssignment object permissions for Community User

I am working on writing a custom Lightning component for use in a Community. What I need it to do is basically recreate the "Articles with this Topic" default component that goes on the Topic Detail page. The default component doesn't allow for custom sorting or custom messaging based on criteria such as the number of articles in the list or if the Topic is linked to another object record, so I am writing my own that will allow for these things.

I am getting stuck on querying the TopicAssignment object in the Apex controller, which I think must be a permissions issue. When I view the component as myself (System Administrator profile) it appears to work, and I get the list of Knowledge articles. When I log in as a Community User (Customer Community User profile), the Apex method returns an empty list. The code for that Apex method is listed below:

public static List<Knowledge__kav> getArticlesForTopic(String topicId)
	{
		try
		{ 
			return [SELECT Id, Title, Subtitle__c, Article_Body__c FROM Knowledge__kav WHERE Id IN (SELECT EntityId FROM TopicAssignment WHERE TopicId = :topicId) AND PublishStatus = 'Online' ORDER BY Sequence__c ASC, LastPublishedDate DESC]; 
		}
		catch(QueryException qe)
		{
			return null;
		}
		catch(Exception e)
		{
			System.debug(JRBRepo.handleError(e,TRUE));
			return null;
		}
	}
I cannot find the Topic or TopicAssignment objects through the Object Manager UI, so I cannot see a way to grant permissions to these objects for the Community User profile. I see under the System Permissions section that there are several permissions related to managing Topics, but not one for just reading Topics. I don't want the Community Users to have control over Topics or the assignment of Topics, I just need them to be able to read them.

Is there a permission that I am missing, or are those objects named something different in the Object Manager UI? 
Joe BrodarJoe Brodar
Just in case anyone else runs into the same problem, I did find a solution; it was only tangentially related to permissions settings. To allow access by a non-admin user, you need to add one of two things to the TopicAssignment query: either add a LIMIT clause of 1000 or less, or filter on the Id or EntityId.

Here is the working solution:
public static List<Knowledge__kav> getArticlesForTopic(String topicId)
	{
		try
		{ 
			TopicAssignment[] assignmentList = [SELECT EntityId FROM TopicAssignment WHERE TopicId = :topicId LIMIT 1000];
			List<Id> assignmentListIds = new List<Id>();
			for (TopicAssignment ta : assignmentList) assignmentListIds.add(ta.EntityId);
			return [SELECT Id, Title, Subtitle__c, Article_Body__c FROM Knowledge__kav WHERE Id IN :assignmentListIds AND PublishStatus = 'Online' ORDER BY Sequence__c ASC, LastPublishedDate DESC LIMIT 1000]; 
		}
		catch(QueryException qe)
		{
			return null;
		}
		catch(Exception e)
		{
			System.debug(JRBRepo.handleError(e,TRUE));
			return null;
		}
	}

- Joe
Shruti Mathur 59Shruti Mathur 59
Hey,I have added the limit of 1000 in my query, but still the list returned is empty, when run by Portal user. While for System Admin there actually are records coming up. Did it wotk for you for Portal user as well?
vivek kasiviswanathanvivek kasiviswanathan
I have the same issue. Does not work for the portal user. any luck?
Jordan VasquezJordan Vasquez
I too am hitting the same issue. Has anyone been able to figure this out for portal users???
vivek kasiviswanathanvivek kasiviswanathan
If you're class uses with sharing, you could try without sharing. This worked for me.. however, its a compromise on the sharing setting which you can find the right sharing settings after this works!
Jordan VasquezJordan Vasquez
Removing the with sharing still did not work for me. What I eventually had to do was make a REST callout to my org and pull it in that way.