+ Start a Discussion
Nic 121Nic 121 

How to I assign a topic to a question in APEX?

I was wondering if anyone has had any experience assigning a Topic to a record from the Question object before.

Some background
We are currently buiding a custom site using VisualForce and custom JS to essentially provide our own interface to the standard SF community functionality.

The page I'm currently working on is our Q&A page - specifically the page through which customers can post their own Questions to our customer community.

The Goal
The issue is, we need to be able to filter our questions by topic - not just by data category.  This must also be achieved purely in APEX without any standard apex components on the VF page.

What has been done so far
So far, I have enabled topics for the question object (under Topics for Objects in SF setup).  I have enabled system permissions for the appropriate profiles to create, edit, and assign topics.  Yet I keep running into an apex exception when I try to assign the topic to the question.

Attempt 1: Manually insert a record into the TopicAssignment object
In the code, I create and insert a question (and topic if required).  This works without any problems.  Then I run the following:
TopicAssignment a = new TopicAssignment(TopicId=t.Id, EntityId=q.Id, NetworkId=n.Id);
insert a;
This line will throw the following error:
Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Entity ID: id value of incorrect type: 906N00000004EF7IAM: [EntityId]
Attempt 2:  Use the ConnectApi
I then found a new class in the ConnectApi called Topics which does not yet appear to be documented.
ConnectApi.Topics.assignTopic(networkId question.Id, topic.Id);
Using this instead yields the following error:
Failed to assign topic to 906N00000004EFC. Please ensure you have permissions to assign topics for this record.


Both of the above errors still occur if the class has sharing off - e.g.
public without sharing class ClassName {
}



I am hoping to achieve this without having to create a new object as a workaround.

So, I was wondering if anyone else has had this issue?  Has anyone succesfully assigned a topic to a question in pure APEX?  Can anyone help?

Thank you very much,

Nic



 
Best Answer chosen by Nic 121
Nic 121Nic 121
Hey there Nirdesh, if your still having issues with this, then I'm guessing SF hasn't extended topics functionality to questions since I last looked.  If that is the case, then Questions inexplicably cannot be assigned topics.

Also unfortunately, the solution for a missing SF feature is always the same.  Spend money and waste time.  In our implementation we ended up adding new indexed string fields on the objects we needed, and used a completely custom end user interface - as the built in functionality didn't come close to what we needed.

So essentially, we had to implement our own Q&A page, without relying on the standard components.  This was a pretty big and unnecessary undertaking, but it was the only way.  And that of course is only the beginning if you want the topics assigned to integrate with chatter, et al.  I'm sorry that I don't have any better news for you.

All Answers

pconpcon
I tried to reproduce this, but I'm unable to enable topics for Questions.  After enabling Chatter Answers, I get the Question object in my metadata, but via the webUI I cannot enable topics.  Is there an additional step that you have to do be able to enable it?  The reason you are getting that FIELD_ITEGRITY_EXCEPTION is that the EntityId does not support the Question Object.  I was hoping to enable topics on Questions in my sandbox and then look at the metadata [1] to see if it shows up under the valid references

User-added image

[1] https://developer.salesforce.com/forums/?id=906F0000000MJlTIAW
Nic 121Nic 121
Hey pcon,
Thanks for the reply.  I'm not too sure what extra steps there may be.  If your profile has the system permissions required, then you should be able to add topics to an object by typing in 'Topics for Objects' in the quick search box.  I don't recall any extra step.

Does the question object not appear on that page? 

I have just realized that there is a Question__c object present added by some managed package, and that may be what I have enabled under topics.  If so, this may not be possible without a new object.

I was just wondering, is that UI pictured above standard in SF?  If so, how do you access it - it would be great to see which Question object is enabled.

Cheers
pconpcon
The UI above is from the link that I totally botched :(  [1] above is suppose to be.  I did all the steps that you mentioned and it is not available for me.  I would take a look at the post below to get the UI above to see what objects it's actually available for.

[1] https://developer.salesforce.com/blogs/developer-relations/2015/02/using-metadata-api-describe-objects.html
Nic 121Nic 121
Hey,

Cheers for the link.  I guess that answers it, it's not possible to assign topics to a Question.  Also, that metadata page is really useful.  Cheers.
Nirdesh KumarNirdesh Kumar
Nic - Did you find any workaround? we also need to assign the topic while creating the question.
Nic 121Nic 121
Hey there Nirdesh, if your still having issues with this, then I'm guessing SF hasn't extended topics functionality to questions since I last looked.  If that is the case, then Questions inexplicably cannot be assigned topics.

Also unfortunately, the solution for a missing SF feature is always the same.  Spend money and waste time.  In our implementation we ended up adding new indexed string fields on the objects we needed, and used a completely custom end user interface - as the built in functionality didn't come close to what we needed.

So essentially, we had to implement our own Q&A page, without relying on the standard components.  This was a pretty big and unnecessary undertaking, but it was the only way.  And that of course is only the beginning if you want the topics assigned to integrate with chatter, et al.  I'm sorry that I don't have any better news for you.
This was selected as the best answer