0

I'm creating an Apex Topic Assignment Trigger for our Community that simply does this:

After insert of a new topic to a knowledge article, create a post that simply says

"A new article called < has been posted to the topic and can be found here: urlofmycompany.community/blahblah/article/"

I do have a piece of this working. After a topic is assigned (single only right now, no multiple topic assignment at the moment, but that's in the future) to a knowledge article, the trigger activates and should create the post. However, I am having immense trouble finding the knowledge article associated with the TopicAssignment via EntityId.

Essentially, I need help figuring out how to grab the most recent article to have a topic assigned to it and grabbing its title and urlname. That's it!

My trigger code:

    trigger NewChatterTopicPost on TopicAssignment (after insert) {
    if(stoprecurssion.runonce()) {
            TopicAssignment topAs = [SELECT Topic.Name FROM TopicAssignment LIMIT 1];
            TopicAssignment topAs2 = [Select TopicId FROM TopicAssignment LIMIT 1];
            Knowledge__kav knoKa = [SELECT KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE Id IN (SELECT EntityId from TopicAssignment) LIMIT 1];
            String topicName = topAs.Topic.Name;
            String topicIdMine = topAs2.TopicId;
            String knoKaTitle = knoKa.Title;
            String knoKaURL = knoKa.UrlName;
            FeedItem post = new FeedItem();
            post.ParentId = topicIdMine; 
            System.debug(knoKaTitle);
            post.Body = 'A new article called ' + knoKaTitle + ' has been added to the ' + topicName + ' topic and can be found here: https://services-softdocs.cs8.force.com/community/s/article/' + knoKaURL;
            insert post;
     }
}

I also have a class that prevents infinite recursion as found on many popular sites such as found here: https://developer.salesforce.com/forums/?id=906F0000000DAT8IAO

The results of this query and trigger are NOT an error, but rather it returns a knowledge article title and url, just... not the right one most of the time. The tagging works great, but I'm fairly certain that it is also selecting the wrong one with the title and url name. The EntityId simply isn't pointing to where I think it is, and I can't get from the docs what it does point to other than the Entity that has had a topic added.

Please help! I'm still fairly new to this SOQL and Apex, but I could really use the assistance, as I feel I've worn Google out.

0

If you only ever want the most recently modified Knowledge Article you can order your results with an ORDER BY clause. Otherwise the result you get back may as well be random. The LIMIT 1 will ensure that only the first result out of that ordered list is returned.

SELECT KnowledgeArticleId
    ,Title
    ,UrlName 
FROM Knowledge__kav 
WHERE Id IN (SELECT EntityId from TopicAssignment) 
ORDER BY LastModifiedDate DESC
LIMIT 1

Note: DESC puts them in descending order, meaning the most recently modified is sorted to the top. ASC, which is the default order, will put the oldest at the top.


Additionally, since this is a trigger there are a number of things you need to fix to make this conform to best practices. Your trigger needs to be bulkified so it can handle the case where 200 Topics are inserted at once. I would encourage you to go through the Apex Trigger Trailhead to learn how to properly write triggers.

1
  • Thanks so much, I'll give these things a go! And yes, I'll definitely be making this more robust.
    – Mimsy Moon
    Oct 8 '20 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.