Create a Knowledge Base Article from Apex Dynamically without knowing article type API name

Here is the apex code to to achieve the same. Run in dev console then use in your test class or anywhere else you want:

Map<String, Schema.SobjectType> gd = Schema.getGlobalDescribe();
String soobjecttype;
for(String g : gd.keySet()){
if(g.contains('__kav')){
soobjecttype = g;
system.debug(g);
break;
}
}
if(soobjecttype != null){
Schema.SObjectType sobjType = gd.get(soobjecttype);
sobject objInstance = sobjType.newSObject();
objInstance.put('Summary', 'testing');
objInstance.put('Title', 'Arvind is awesome!');

objInstance.put('UrlName', 'GrazitbtiianfffArvindisawesome');
insert objInstance;
String recid=objInstance.Id;
List recs = Database.query('select id, knowledgeArticleId from '+soobjecttype + ' where id=:recid and PublishStatus='+'\'draft\''+' and Language='+'\'en_US\'');
KbManagement.PublishingService.publishArticle(String.valueOf(recs[0].get('KnowledgeArticleId')), true);

}

10 thoughts on “Create a Knowledge Base Article from Apex Dynamically without knowing article type API name”

  1. constantly i used to read smaller posts which also clear their motive, and that is also happening with this post
    which I am reading here. games ps4 185413490784 games ps4

  2. Good day! I know this is kinda off topic but
    I was wondering which blog platform are you using
    for this website? I’m getting fed up of WordPress because
    I’ve had problems with hackers and I’m looking at options for another platform.

    I would be great if you could point me in the direction of a good platform.

  3. Great weblog here! Also your site lots up very fast!
    What web host are you the usage of? Can I am getting your
    affiliate link on your host? I wish my web site loaded up as quickly
    as yours lol

  4. Excellent post. I was checking constantly this blog and I am inspired!
    Very helpful info specifically the final part 🙂 I handle such info a lot.
    I was seeking this certain information for
    a very lengthy time. Thank you and good luck.
    0mniartist asmr

Comments are closed.