Manage Knowledge Articles Using Apex

Create new article:

Knowledge__kav ka = new Knowledge__kav();
ka.Title = 'Salesforce CRM';
ka.UrlName = 'salesforce-crm';
ka.Summary = 'Salesforce Cloud CRM';
ka.Language = 'en_US';
insert ka;

Publish a draft article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);

Unpublish a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);

Schedule archive of a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
Datetime scheduledDate = System.now().addMonths(2); //Add date to schedule the archive
KbManagement.PublishingService.archiveOnlineArticle(knowledgeArticleId, scheduledDate);

Note: If the specified scheduledDate is null, the article is archived immediately.

Cancel a scheduled archive published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.cancelScheduledArchivingOfArticle(knowledgeArticleId);

Delete an archived article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.deleteArchivedArticle(knowledgeArticleId);

Delete a draft article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.deleteDraftArticle(knowledgeArticleId);

Create a draft article from the archived article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.editArchivedArticle(knowledgeArticleId);

Unpublish a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.editOnlineArticle(knowledgeArticleId, true);

  • Eva K.

    Hi Biswajeet! Is there a way to cancel an article that is scheduled to be published using a Flow? Thank you!

    Eva

  • DataMan

    Sub KnowledgeArticleScripts()

    ‘ Paste KnowledgeArticleId in Column A2

    Range(“B1”).Select
    ActiveCell.FormulaR1C1 = “Unpublish a published article 1:” & Chr(10) & “”
    Range(“C1”).Select
    ActiveCell.FormulaR1C1 = “Unpublish a published article 2:”
    Range(“D1”).Select
    ActiveCell.FormulaR1C1 = “Create a draft article from the archived article:” & Chr(10) & “”
    Range(“E1”).Select
    ActiveCell.FormulaR1C1 = “Delete a draft article:”
    Range(“F1”).Select
    ActiveCell.FormulaR1C1 = “Delete an archived article:” & Chr(10) & “”
    Range(“F2”).Select
    Range(“B2”).Select
    ActiveCell.FormulaR1C1 = _
    “=CONCAT(“”{String knowledgeArticleId = ‘””,RC[-1],””‘; //Add knowledge article record id” & Chr(10) & “KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);}””)”
    Range(“C2”).Select
    ActiveCell.FormulaR1C1 = _
    “=CONCAT(“”{String knowledgeArticleId = ‘””,RC[-2],””‘; //Add knowledge article record id” & Chr(10) & “KbManagement.PublishingService.editOnlineArticle(knowledgeArticleId, true);}””)”
    Range(“D2”).Select
    ActiveCell.FormulaR1C1 = _
    “=CONCAT(“”{String knowledgeArticleId = ‘””,RC[-3],””‘; //Add knowledge article record id” & Chr(10) & “KbManagement.PublishingService.editArchivedArticle(knowledgeArticleId);}””)”
    Range(“E2”).Select
    ActiveCell.FormulaR1C1 = _
    “=CONCAT(“”{String knowledgeArticleId = ‘””,RC[-4],””‘; //Add knowledge article record id” & Chr(10) & “KbManagement.PublishingService.deleteDraftArticle(knowledgeArticleId);}””)”
    Range(“F2”).Select
    ActiveCell.FormulaR1C1 = _
    “=CONCAT(“”{String knowledgeArticleId = ‘””,RC[-5],””‘; //Add knowledge article record id” & Chr(10) & “KbManagement.PublishingService.deleteArchivedArticle(knowledgeArticleId);}””)”
    Range(“F3”).Select
    Rows(“1:1”).Select
    Selection.Font.Bold = False
    Selection.Font.Underline = xlUnderlineStyleSingle
    With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlCenter
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    End With
    With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlTop
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    End With
    End Sub