0

I have a simple Rest API that I've done in another environment but I have no idea why its not working. When I execute from Dev Console, it gives me: System.NullPointerException: Attempt to de-reference a null object

However, when I execute from workbench, it gives me everything I am looking for.

Here is the API Class:

@RestResource(urlMapping='/PublishedKBArticles/*')
Global with Sharing class KnowledgeAPI {
    @HttpGet    
    Global Static Void getPublishedKB() {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        List<Knowledge__kav> kbs = [SELECT Id, Title, Data_Group__c, Data_Category__c, Question__c,Answer__c FROM Knowledge__kav Where PublishStatus = 'Online']; 

        List<KnowledgeAPI_Datatype.Article> kbsList = new List<KnowledgeAPI_Datatype.Article>();

        For(Knowledge__kav kb : kbs){

            kbsList.add(new KnowledgeAPI_Datatype.Article(kb));
        }

        String kbResults = json.serialize(kbsList); 

        system.debug(blob.valueof(kbResults));

        res.responseBody = blob.valueof(kbResults); 
    }

}

and here is the custom data type class:

public with sharing class KnowledgeAPI_DataType {

     Public Class Article {

        public String Id {get;set;} 
        public String Title{get;set;}
        public String DataGroup{get;set;}
        public String DataCategory{get;set;}
        public String Question{get;set;}
        public String Answer{get;set;}

        Public Article(Knowledge__kav kb){

            Id = kb.Id;
            Title = kb.Title;
            DataGroup = kb.Data_Group__c;
            DataCategory = kb.Data_Category__c;
            Question = kb.Question__c;
            Answer = kb.Answer__c;

        }
    }


}

Edit: I have narrowed the issue to this line of code:

List<Knowledge__kav> kbs = [SELECT Id, Title, Data_Group__c, Data_Category__c, Question__c,Answer__c FROM Knowledge__kav Where PublishStatus = 'Online'];

When it tries to fill the list from Dev Console or validation/deployment page in production it gives the null pointer exception on that line.

Any reason on if the Knowledge__kav object can't be queried?

6
  • In the debug log - get the stack trace and share that. what line are you getting that exception? Also, do you have the permission enabled as a knowledge user? If i had to guess, the issue is that the article constructor is getting a null KB, so kb.id is throwing the NPE Commented Jan 25, 2020 at 7:26
  • How are you calling the method from workbench and dev console, my feeling is if you are using execute anon in dev console then RestContext.response; would be null Commented Jan 25, 2020 at 7:27
  • I'm calling it in the workbench from the rest explorer via /services/apexrest/PublishedKBArticles/ I also have Knowledge User permissions Commented Jan 25, 2020 at 7:48
  • Like @PranayJaiswal mentioned, how are you doing it in dev console? Through anonymous apex? Commented Jan 25, 2020 at 14:52
  • Yes, I am executing via dev_console anonymously. 'KnowledgeAPI.getPublishedKB();' and when I try to push it into the production environment, the test classes fail. Commented Jan 26, 2020 at 5:22

1 Answer 1

0

In the end the requirement was met by using the following code. I think what the problem is, is when you get a list of the Knowledge_kav object, there was a "wrapper" of attributes that the custom datatype did not take into account. (see picture) enter image description here

and here is the final code I used:

@RestResource(urlMapping='/PublishedKBArticles/*')
Global with Sharing class KnowledgeAPI {
    @HttpGet    
    Global Static list<Knowledge__kav> getPublishedKB() {

        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        List<Knowledge__kav> kbs = [SELECT Id, Title, Data_Group__c, Data_Category__c, Question__c,Answer__c,CreatedDate, LastModifiedDate FROM Knowledge__kav Where PublishStatus = 'Online']; 

        system.debug('List: ' + kbs);

        return kbs;
    }

}

You must log in to answer this question.

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