Trigger Considerations for Knowledge Articles
You can write triggers for KnowledgeArticleVersion objects. Learn when you can use triggers, and which actions don’t fire triggers, like archiving articles.
In general, KnowledgeArticleVersion (KAV) records can use these triggers:
- Creating a KAV record calls the
before insertandafter inserttriggers. This includes creating an article, and creating drafts from archived, published, and master-language articles using the Restore, Edit as Draft, and Submit for Translation actions. - Editing an existing KAV record calls the
before updateandafter updatetriggers. - Deleting a KAV record calls the
before deleteandafter deletetriggers. - Importing articles calls the
before insertandafter inserttriggers. Importing articles with translations also calls thebefore updateandafter updatetriggers.
Actions that change the publication status of a KAV record, such as Publish and Archive, do not fire Apex or flow triggers. However, sometimes publishing an article from the UI causes the article to be saved, and in these instances the before update and after update triggers are called.
Consider the following when writing Apex triggers for actions on KnowledgeArticleVersion:
-
Save, Save and Close: When an article is saved, the
before updateandafter updatetriggers are called. When a new article is saved for the first time, thebefore insertandafter inserttriggers work instead. -
Edit, Edit as Draft:
- When a draft translation is edited, you can use the
before updateandafter updatetriggers. - The Edit as Draft action creates a draft from a published article, so the
before insertandafter inserttriggers fire. - In Salesforce Classic, no triggers fire when a draft master-language article is edited.
- In Salesforce Classic, the
before insertandafter inserttriggers are called when editing an archived article from the Article Management tab. This creates a draft KAV record.
- When a draft translation is edited, you can use the
-
Cancel, Delete:
The
before deleteandafter deletetriggers are called in these cases:- When deleting a translation draft.
- From the Article Management or Knowledge tab in Salesforce Classic, after editing a published article and then clicking Cancel. This deletes the new draft.
-
Submit for Translation: This action creates a draft translation, so you can generally use the
before insertandafter inserttriggers. In Salesforce Classic, you can use thebefore updateandafter updatetriggers when you create a new article from the Knowledge tab, save it, and then submit for translation. Thebefore updateandafter updatetriggers fire when the master-language article is currently being edited, but not from list views or when viewing the article. -
Assign: The
before updateandafter updatetriggers are called only when doing so causes a record save first. This happens when the article is being edited before the Assign button is clicked.
These actions can’t fire Apex triggers:
- Undelete articles from the recycle bin.
- Preview and archive articles.
Migrating from Knowledge in Salesforce Classic to Lightning Knowledge affects Apex triggers. Writing an Apex trigger on KnowledgeArticleVersion objects creates dependencies and prevents the KAV object from being deleted. When you migrate an org with multiple article types to Lightning Knowledge, you must remove any Apex triggers that reference the KAV article types. During migration, admins see an error message if Apex triggers still reference the article type KAV objects that are deleted during migration. If you cancel Lightning Knowledge migration while Apex triggers exist that refer to the new KAV object, admins are notified and you must remove the Apex code.
For example, you can define a trigger that enters summary text when an article is created.