4

I have the API Name of a custom field on a knowledge article stored a string

kav.articleType = 'Frequently_Asked_Question__kav';

where kav is a KnowledgeArticleVersion object.

How can I get the label name from kav.articleType?

I was thinking I could do something like

Schema.SObjectType.KnowledgeArticleVersion.fields.(kav.ArticleType).getLabel();

But this is not working. Any ideas?

Here is some more context:

What I am trying to do is update some fields on a custom Article Feedback object whenever a record of this object is created. A user only needs to manually enter the Article Number and the remaining fields like Article Type, Article Title, etc. should be automatically added using this trigger.

I got most of the fields to work except the Article Type is showing as its api name and not the label. I tried it a couple different ways. I commented these failed attempts in the code below

trigger CreateExternalFeedback on aft__Article_Feedback__c (before insert) {

    for (aft__Article_Feedback__c f : trigger.new) {

        if (f.aft__Feedback_Source__c == 'External') { 

            //Retrieve KnowledgeArticleVersion 
            String q = 'select KnowledgeArticleId, articleNumber, title, ArticleType, VersionNumber, Language, LastPublishedDate, CreatedDate from KnowledgeArticleVersion where PublishStatus = \'online\' and Language = \'en_US\'';
            q += 'and articleNumber = \''+ f.aft__Article_Number__c +'\' limit 1';

            KnowledgeArticleVersion kav = (KnowledgeArticleVersion) Database.query(q);

            f.OwnerId = '00Gc0000001ENiJ';
            f.aft__Article_Title__c = kav.Title;     
            f.aft__Article_Created_Date__c = kav.CreatedDate;

            //f.aft__Article_Type__c = kav.ArticleType;
            //f.aft__Article_Type__c = Schema.kav.KnowledgeArticleVersion.fields.ArticleType.getLabel();

            f.aft__Article_Version__c = kav.VersionNumber;
            f.aft__Feedback_Status__c = 'New';
            f.Knowledge_Article_Link__c = 'https://cs14.salesforce.com/knowledge/publishing/articleOnlineDetail.apexp?id='+kav.KnowledgeArticleId;
            f.aft__Language__c = kav.Language;
            f.aft__Last_Published_Date__c = kav.LastPublishedDate;

        }
    }
}

1 Answer 1

8

If you want to take the API Name of a field and get its label, use the getLabel method on DescribeFieldResult.

SObjectField field = Frequently_Asked_Question__kav.ArticleType;
DescribeFieldResult describe = field.getDescribe();
system.debug(describe.getLabel()); // e.g. 'Article Type'

If you want to take the API Name of an SObject and get its label, use the getLabel method on DescribeSObjectResult.

SObjectType sObjectType = Frequently_Asked_Question__kav.sObjectType;
DescribeSObjectResult describe = sObjectType.getDescribe;
// alternatively replace the above two lines with the one below
// DescribeSObjectResult = SObjectType.Frequently_Asked_Question__kav;
system.debug(describe.getLabel()); // e.g. 'Frequently Asked Question'

If you are trying to get an SObjectType from a String, you would use:

SObjectType faq = Schema.getGlobalDescribe().get('Frequently_Asked_Question__kav');

Or in your case, get(kav.ArticleType).

3
  • sorry, I don't really understand. I'm new to apex and am still learning. I won't always know the article type, that is a variable I represented as a string for simplicity. I added the full code for my trigger above for context.
    – SfAdmin87
    Dec 17, 2015 at 19:46
  • 3
    I just laid out how to get the label from either a SObjectField or SObjectType. You need to use their describes as explained above.
    – Adrian Larson
    Dec 17, 2015 at 19:54
  • Adrian, thanks so much for your help. I got it to work using the following: Schema.SObjectType s = Schema.getGlobalDescribe().get(kav.ArticleType); DescribeSObjectResult artType = s.getDescribe(); the complication was that I needed to convert the string kav.articleType to the appropriate article type object
    – SfAdmin87
    Dec 17, 2015 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .