---
schema_version: "1"
id: d1a6bcf138b00bf80ecb4cd3b8aa9ec9939bbd757d3e84181f766e8aefcf2b39
title: Example Apex for Submitting Articles from Cases
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/service/264-0-0/knowledge_caseclose_apex.md
canonical_url: https://help.salesforce.com/s/articleView?id=service.knowledge_caseclose_apex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 9374026d02e9d7927fed5e4f70da1e883c243b4241106241ed65aa98e24cef05
description: 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.
products:
  - Service Cloud
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;">Available in Salesforce Classic and Lightning Experience. <a href="https://help.salesforce.com/s/articleView?id=service.service_editions_reference.htm&amp;language=en_US" target="_blank">View supported editions</a>.</td></tr></tbody></table>

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d122025e58">User
              Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d122025e58 ">To edit Salesforce Knowledge settings:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d122025e58 ">Customize Application</td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d122025e58 ">To create an Apex class:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d122025e58 ">Author Apex</td>

          </tr>
        </tbody>
      </table>

For more information on the syntax and use of Apex, see the [Lightning Platform Apex Code Developer's Guide](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/).

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.

1. [Create an article type](https://help.salesforce.com/s/articleView?id=service.knowledge_article_types_create.htm&release=264.0.0&type=5 "An article type controls how an article displays and what type of information of fields are included.") called FAQ.
2. [Create a text custom field](https://help.salesforce.com/s/articleView?id=service.knowledge_fields_custom_add.htm&release=264.0.0&type=5 "Create custom fields to store information that’s important to your articles. The only standard fields provided on article types are: Article Number, Summary, Title, and URL Name. At minimum, you want to create a field where authors can write the body of the article.") called Details.
3. [Create a category group](https://help.salesforce.com/s/articleView?id=service.categorygroup_create.htm&release=264.0.0&type=5 "Category groups are used by Salesforce Knowledge (articles), answers (questions), or ideas. In all cases, category groups are containers for individual data categories. For example, a Contracts category group sometimes contains Fixed Price, Cost Reimbursement, and Indefinite Delivery categories.") called Geography and [assign it a category](https://help.salesforce.com/s/articleView?id=service.category_create.htm&release=264.0.0&type=5 "Once you have category groups, you can add data categories to help support reps classify and find articles, questions, or ideas.") called USA.
4. [Create a category group](https://help.salesforce.com/s/articleView?id=service.categorygroup_create.htm&release=264.0.0&type=5 "Category groups are used by Salesforce Knowledge (articles), answers (questions), or ideas. In all cases, category groups are containers for individual data categories. For example, a Contracts category group sometimes contains Fixed Price, Cost Reimbursement, and Indefinite Delivery categories.") called Topics and [assign it a category](https://help.salesforce.com/s/articleView?id=service.category_create.htm&release=264.0.0&type=5 "Once you have category groups, you can add data categories to help support reps classify and find articles, questions, or ideas.") called Maintenance.
    
    Now, create and assign the Apex class.
5. From Setup, enter Apex Classes in the Quick Find box, then select **Apex Classes** and click **New**.
6. 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.
7. In the Apex Class text box enter the following script and click **Save**:
    
    ```markup
    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');                        
        }
    ```
8. From Setup, enter Knowledge Settings in the Quick Find box, then select **Knowledge Settings** and click **Edit**.
9. Verify the [case settings](https://help.salesforce.com/s/articleView?id=service.knowledge_settings.htm&release=264.0.0&type=5 "From the Knowledge Settings page, you can create a knowledge base experience for your support reps, partners, and customers."); using our example, the **Default article type** should be FAQ.
10. 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.

#### See Also

- [Build Your Knowledge Base in Salesforce Classic](https://help.salesforce.com/s/articleView?id=service.knowledge_setup.htm&release=264.0.0&type=5 "Create a library of knowledge articles in Salesforce Classic. Before you get started, assign Knowledge User licenses to yourself and your team.")
- [Salesforce Knowledge Help and Resources](https://help.salesforce.com/s/articleView?id=service.knowledge_map.htm&release=264.0.0&type=5 "Find the information you need about Salesforce Knowledge. Explore help documentation, developer documentation, and Trailhead modules.")