+ Start a Discussion
Ram@sfdcRam@sfdc 

Knowledge Article data update using apex

Hi,

Is it possible to update Knowledge Article record using apex.

I am trying like this..
list<KnowledgeArticleVersion> kv=[SELECT Id,Title,UrlName,Summary,KnowledgeArticleId FROM KnowledgeArticleVersion WHERE Language =:'en_US' AND PublishStatus =:'Draft' WITH DATA CATEGORY NOWTVKnowledge__c AT Cancellations__c];
system.debug('@@@ '+kv[0].KnowledgeArticleId);
kv[0].UrlName='Test 1234';
update kv;

Like this I am getting error like urlname not writable.

Could you help me how to update article data using apex coding with small example...

Thanks in advance..
Subramani_SFDCSubramani_SFDC
SELECT Id,Title,UrlName,Summary,KnowledgeArticleId FROM Article__kav WHERE Language =:'en_US' AND PublishStatus =:'Draft' WITH DATA CATEGORY NOWTVKnowledge__c AT Cancellations__c];
system.debug('@@@ '+kv[0].KnowledgeArticleId);
kv[0].UrlName='Test 1234';
update kv;


Regards,
Subramani.J
TRINAY TECHNOLOGY SOLUTIONS
Ram@sfdcRam@sfdc
Hi Subramani,
Thanxs for ur reply,

There is no such object Article__Kav in my Org.

Could you suggest me how to get all Knowledge articles from Org. with a single Query.
Subramani_SFDCSubramani_SFDC
refer this doc it will help you...i am trying urs in my org..i got same error.....

http://www.salesforce.com/docs/developer/knowledge_devpre/index.htm
Piotr GajekPiotr Gajek
Hi guys, 

I had the same problem. 

More details on my blog here (https://salesforceprofs.com/updating-knowledge-articles-programmatically-by-apex/).

Here you can find a code snippet and example of use, which should resolve your problem:
public with sharing class ArticlesUtils {

    @AuraEnabled
    public static List<Knowledge__kav> getAllArticles(){
        return [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav ];
    }

    @AuraEnabled
    public static String createNewArticleAsADraft(String title, String urlName) {

        Knowledge__kav newArticle = new Knowledge__kav();
        newArticle.Title = title;
        newArticle.UrlName = urlName;
        insert newArticle;

        return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticle.Id].KnowledgeArticleId;
    }

    @AuraEnabled
    public static void publishArticle(String recordId) { //It need to be KnowledgeArticleId
        KbManagement.PublishingService.publishArticle(recordId, true);
    }

    @AuraEnabled
    public static String unPublishArticle(String recordId){ //It need to be KnowledgeArticleId
        String newArticleId = KbManagement.PublishingService.editOnlineArticle(recordId, true); //Method new version id use it to update 
        return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticleId].KnowledgeArticleId;
    }

    @AuraEnabled
    public static String updateDraftArticleWithoutPublish(String title, String urlName, Id recordId) {

        Knowledge__kav newArticle = [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE KnowledgeArticleId =: recordId ];   

        newArticle.Title = title;
        newArticle.UrlName = urlName;

        update newArticle;

        return newArticle.KnowledgeArticleId;
   }

    @AuraEnabled
    public static String updatetArticle(String title, String urlName, Id recordId) {

        String newVersionId = unPublishArticle(recordId);

        Knowledge__kav newArticle = [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE KnowledgeArticleId =: newVersionId ];   

        newArticle.Title = title;
        newArticle.UrlName = urlName;

        update newArticle;

        publishArticle(newVersionId);

        return newVersionId;
   }

}
List<Knowledge__kav> articles = ArticlesUtils.getAllArticles();
 
String newArticleKnowledgeId = ArticlesUtils.createNewArticleAsADraft('SalesforceProfs', 'salesforce-profs');
 
ArticlesUtils.publishArticle(newArticleKnowledgeId);
 
//unpublish, update, publish > separate actions
String newArticleVersionId = ArticlesUtils.unPublishArticle(newArticleKnowledgeId);
ArticlesUtils.updateDraftArticleWithoutPublish('SalesforceProfs Update', 'salesforce-profs-update', newArticleVersionId);
ArticlesUtils.publishArticle(newArticleVersionId);
 
//update - contain unpublish, update, publish
//ArticlesUtils.updatetArticle('SalesforceProfs Update', 'salesforce-profs-update', newArticleKnowledgeId);

Regards,
Piotrek