Salesforce Knowledge Rest API

Unboxing Salesforce Knowledge Rest APIs

SF knowledge rest APIs come in 4 different flavours (Types) based on the polymorphic nature of the data model and each different type (as below) has a set of actions contained in it

1. sObject Type Knowledge__kav API: To create, and query knowledge articles

  • /services/data/v54.0/sobjects/Knowledge__kav/

2. KnowledgeManagement type APITo Manage articles, versions and it’s translations, you would use this class of API

  • /services/data/v54.0/knowledgeManagement

3. Support Knowledge API: To query Article list, article details and articles based on the Data category they are associated with, you would use this, example using, Data Category GroupsData Category DetailArticles ListArticles Details

  • /services/data/v54.0/support/knowledgeArticles

4. Invocable Actions API: a service to mass publish knowledge articles, generally you would use this API & its actions to manage your articles and article versions.

  • /services/data/v54.0/actions/standard/publishKnowledgeArticles

For the SOAP version of the API, please check this - https://developer.salesforce.com/docs/atlas.en-us.knowledge_dev.meta/knowledge_dev/knowledge_development_soap_intro.htm

Small note: You can use these APIs in a simple 1.1 rest API calls or you can also put them together in a composite resource, we will talk about this also but there are some limitations from the above list which the composite resource supports, please keep this in mind

Now, let's look at some of the examples to see how we can create an article, assign them to data categories and publish them programmatically (using API)

1. Create a knowledge article in the draft status

note: you cannot set the “publishstatus” in the payload (data object) using this call
POST
/services/data/v54.0/sobjects/Knowledge__kav/

{
    “summary”:”Summary for the Article 1",
    “title”:”hello world”,
    “urlName”:”hello-world1"
}

You can also set the ‘recordtypeid’ above if you are in Salesforce lightning.

Small note: You can not use/set the publish status in the JSON data object as it does not support it at the moment and articles can only be created in draft status (default)

Raw Response

HTTP/1.1 201 Created
Raw Response

HTTP/1.1 201 Created
Date: Sun, 14 Aug 2022 10:02:14 GMT
Set-Cookie: CookieConsentPolicy=0:1; path=/; expires=Mon, 14-Aug-2023 10:02:14 GMT; Max-Age=31536000
Set-Cookie: LSKey-c$CookieConsentPolicy=0:1; path=/; expires=Mon, 14-Aug-2023 10:02:14 GMT; Max-Age=31536000
Location: /services/data/v54.0/sobjects/Knowledge__kav/ka15e0000011oBdAAI
Content-Type: application/json;charset=UTF-8
Vary: Accept-Encoding
Content-Encoding: gzip
Transfer-Encoding: chunked
{
    "id" : "ka15e0000011oBdAAI",
    "success" : true,
    "errors" : [ ]
}

dont miss out iconDon't forget to check out: How to Build a Basic Salesforce REST API Integration

2. Assign/link them to data categories

where “Knowledge__DataCategorySelection” is the object which stores the article relation with its data categories. Each assigned data category is mapped as a record in this Object :

POST
/services/data/v54.0/sobjects/Knowledge__DataCategorySelection

{
    “ParentId”:”ka15e0000011oBiAAI”,
    “DataCategoryGroupName”:”Product”,
    “DataCategoryName”:”XP80"
}

Raw Response

HTTP/1.1 201 Created
Date: Sun, 14 Aug 2022 10:23:05 GMT
Set-Cookie: CookieConsentPolicy=0:1; path=/; expires=Mon, 14-Aug-2023 10:23:05 GMT; Max-Age=31536000
Location: /services/data/v54.0/sobjects/Knowledge__DataCategorySelection/02o5e00000224tfAAA
Content-Type: application/json;charset=UTF-8
Vary: Accept-Encoding
Content-Encoding: gzip
Transfer-Encoding: chunked
{
    "id" : "02o5e00000224tfAAA",
    "success" : true,
    "errors" : [ ]
}

Knowledge articles

3. Publish the articles programmatically

where “publishKnowledgeArticles” is the resource available from the 4th type of knowledge resource which is invocable API actions, the “articleVersionIdlist” parameter from below accepts Knowledge article version Ids created from creating API in a comma-separated format

POST
/services/data/v54.0/actions/standard/publishKnowledgeArticles

{
    “inputs” : [{
        “articleVersionIdList” : [ “ka15e0000011oBiAAI” ],
        “pubAction” : “PUBLISH_ARTICLE”
    }
}]

Raw Response

Question:

You might wonder why we are talking about the composite resources here, Yes — I didn’t share the context earlier.

What happens normally in an enterprise business model is you don’t have just one system responsible for knowledge articles creation and at times there comes the need to integrate various types of content (FAQ, Manuals, instructions etc.) from multiple sources and bring them to Salesforce as a single repository for operational usage (e.g for your Sales/support agents or customers etc.)

And this is where using a composite resource at your integration layer (dell Boomi/Mulesoft) who are orchestrating the data/ integration flow is helpful to reduce many API calls, hence the need for the composite resource so that they don’t have to repeat the process again and again.

Approach 1:

Here we will use the combination of Composite 1) (sobject Tree) resource in combination with 2) Knowledge Invokable action from above, thereby creating multiple records with 1 or fewer API call(s) to process all records together (given: maximum limit 200 total records across all trees / per callout)

The idea here is to bulkify and process multiple records together in just one or few API calls rather than serially processing per record with ↔ 1 API call, what it means is we map all the records from an external system into the request payload and pass it over as part of the same API call avoiding the need to make as many API calls as the number of records

You can even create a structure for processing the parent-child relationship (as we need in our case with knowledge article version and data categories) in the sample payload and send multiple records keeping in mind we don’t go over the maximum 200 total records limit per call.

dont miss out iconCheck out an amazing Salesforce video tutorial here: Salesforce To Salesforce Integration Using REST API

Concluding again, here is the benefit:

  1. As said already, we can create multiple records of the same type of knowledge articles (knowledge__kav) k1, k2, and k3 together in 1 API calls which avoids jumping back and forth and avoiding multiple resource calls.
  • + You can create child records (nested parent-child) for knowledge__kav with data categories as well k1+ dc1, k2 +dc2, k3+dc3 (where dc means data category and k1: Knowledge article) combining 2 operations in 1 plus offering the ability to do it for multiple records.

For all records from the external system, create a single big payload (remember max: 200 records) containing few too many records and do both operations,

1. Create articles and assign them data categories using 1 single api call

2. Mass publish them with 1 API call

Method : POST
/services/data/v54.0/composite/tree/Knowledge__kav

{
"records" :[{
    "attributes" : {"type" : "Knowledge__kav", "referenceId" : "ref1"},
     "summary":"Summary for the Article",
     "title":"hello world 13",
     "urlName":"hello-world13",   
     "DataCategorySelections" : {
      "records" : [{
         "attributes" : {"type" : "Knowledge__DataCategorySelection", "referenceId" : "ref2"},
          "DataCategoryGroupName":"Product",
           "DataCategoryName":"XP80"
         },{
         "attributes" : {"type" : "Knowledge__DataCategorySelection", "referenceId" : "ref3"},
           "DataCategoryGroupName":"Product",
           "DataCategoryName":"X21"
         }]
      }
    },{
    "attributes" : {"type" : "Knowledge__kav", "referenceId" : "ref4"},
     "summary":"Summary for the Article ",
     "title":"hello world 14",
     "urlName":"hello-world14",
     "DataCategorySelections" : {
      "records" : [{
        "attributes" : {"type" : "Knowledge__DataCategorySelection", "referenceId" : "ref5"},
          "DataCategoryGroupName":"Product",
          "DataCategoryName":"XP80"
        }]
      }   
  }]
}
Raw Response

HTTP/1.1 201 Created
Date: Sun, 14 Aug 2022 19:36:36 GMT
Content-Encoding: gzip
Transfer-Encoding: chunked
{
    "hasErrors" : false,
    "results" : [ {
        "referenceId" : "ref1",
        "id" : "ka15e0000011oCbAAI"
  }, {
        "referenceId" : "ref4",
        "id" : "ka15e0000011oCcAAI"
  }, {
        "referenceId" : "ref2",
        "id" : "02o5e00000224utAAA"
  }, {
        "referenceId" : "ref3",
        "id" : "02o5e00000224uuAAA"
  }, {
        "referenceId" : "ref5",
        "id" : "02o5e00000224uxAAA"
  } ]
}

After this they need to collect the knowledge articles versions id from the response above and pass it into the method we already talked about for mass publishing. (vXX.x/actions/standard/publishKnowledgeArticles/”) to publish them all together

Last Point, in order to use the composite resource, prior check the sobject and operations it supports as there are some minor limitations to it but not blockers:). An example “invocable Action API” to mass publish articles is NOT supported in the composite resources but only in batch composite type resource, here are the details - https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite_batch.htm

Reference links:

Knowledge APIs
https://developer.salesforce.com/docs/atlas.en-us.knowledge_dev.meta/knowledge_dev/resources_knowledge_support.htm

Composite API
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_composite.htm

Knowledge Data model
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_guidelines_knowledge.htm

That’s all for today, hope you enjoyed and learned something.,

write me your feedback! And for more learning follow me at https://itsmemohit.medium.com/

Cheers - Mohit

Responses

Popular Salesforce Blogs