You are here:
Example Apex for Submitting Articles from Cases
If your organization allows customer-support reps to create Salesforce Knowledge articles while closing a case, you can use Apex to pre-populate fields on draft articles. To do so, create an Apex class and assign it to the case article type using the example below as a guide.
Required Editions
| Available in Salesforce Classic and Lightning Experience. View supported editions. |
| User Permissions Needed | |
|---|---|
| To edit Salesforce Knowledge settings: | Customize Application |
| To create an Apex class: | Author Apex |
For more information on the syntax and use of Apex, see the Lightning Platform Apex Code Developer's Guide.
Set up the example by creating the following article type, field, and data categories. Do not change the default API Name assigned to each new object.
- Create an article type called FAQ.
- Create a text custom field called Details.
- Create a category group called Geography and assign it a category called USA.
-
Create
a category group called Topics and assign it a
category called Maintenance.
Now, create and assign the Apex class.
- From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes and click New.
-
To specify the version of Apex and the API used with this class, click
Version Settings.
If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this class. Use the default values for all versions. This associates the class with the most recent version of Apex and the API, as well as each managed package. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version. You can specify an older version of Apex and the API to maintain specific behavior.
-
In the Apex Class text box enter the following script and
click Save:
public class AgentContributionArticleController { // The constructor must take a ApexPages.KnowledgeArticleVersionStandardController as an argument public AgentContributionArticleController(ApexPages.KnowledgeArticleVersionStandardController ctl) { SObject article = ctl.getRecord(); //this is the SObject for the new article. //It can optionally be cast to the proper article type, e.g. FAQ__kav article = (FAQ__kav) ctl.getRecord(); String sourceId = ctl.getSourceId(); //this returns the id of the case that was closed. Case c = [select subject, description from Case where id=:sourceId]; article.put('title', 'From Case: '+c.subject); //this overrides the default behavior of pre-filling the title of the article with the subject of the closed case. article.put('Details__c',c.description); ctl.selectDataCategory('Geography','USA'); //Only one category per category group can be specified. ctl.selectDataCategory('Topics','Maintenance'); } - From Setup, enter Knowledge Settings in the Quick Find box, then select Knowledge Settings and click Edit.
- Verify the case settings; using our example, the Default article type should be FAQ.
- From the Use Apex Customization menu, select AgentContributionArticleController and click Save.
As a result of this example, when support reps create an article from the case-close screen:
- The data from the Description field on the case appears in the Details field of the article.
- The title of the article contains From Case: and the case subject.
- The article is automatically assigned to the USA data category and the Maintenance data category.
Did this article solve your issue?
Let us know so we can improve!

