I am trying to get article owner name and i'm getting this error when i'm not logged in as System Administrator. I tried changing permissions but it seems like there is no solution but, running code as System Admin. Only problem is that runAs is only permitted in Tests methods. is there any workaround?

public static String getCommunityFeedbackOnArticleApexController(String recordId, String description, String articleName, String articleUrl) {
    // if required fields are not null or empty string
    if(description == null || description.equals('') && recordId == null || recordId.equals('')){
        return 'false';

            User current = [SELECT Id, Contact.Name, Contact.Account.Name, Contact.AccountId FROM User WHERE Id =: UserInfo.getUserId()];

            Id articleId = Id.valueOf(recordId.rightpad(15, '0'));
            // gets sobject type __ka or __kav
            Schema.SObjectType objectToQuery = articleId.getSObjectType();
            String strObjectToQuery = String.valueOf(objectToQuery);
            // dynamically querying __ka or __kav object
            String strQuery = 'SELECT Article_Owner__r.Name FROM ' + strObjectToQuery + ' WHERE Id = \'' + recordId + '\' LIMIT 1'; 
            List<sObject> sObjectReturned  = Database.query(strQuery);

            String articleOwnerName;


                articleOwnerName = (String) sObjectReturned[0].getSobject('Article_Owner__r').get('Name');

                articleOwnerName = 'Unknown author';

            Article_Feedback__c articleFeedback = new Article_Feedback__c(

                Contact_Name__c = current.Contact.Name,
                Account_Name__c = current.Contact.Account.Name,
                Feedback_description__c = description,
                Article_Name__c = articleName,
                Article_Owner__c = articleOwnerName,
                Link_To_Article__c = articleUrl

            insert articleFeedback;
            return 'true';

        }catch(Exception e){
            return 'message = ' + e.getMessage() + ' cause = '+ e.getCause() + ' line = ' + e.getLineNumber();


message = Attempt to de-reference a null object, cause = null, line = 28


Check the Field Article_Owner__c on every object you might be querying (not sure which ones it is, you just call them __ka or __kav. Here, enable Read Access for all the profiles.

This is most definitely an error that happens because only your System Administrator can see the field and when running as another user sObjectReturned[0].getSobject('Article_Owner__r') returns null and this is where the NullPointerException happens.

  • 1
    Thanks for reply, i figured it out, the problem was in org wide defaults, community user did not have permission to access user details. – Nikola Naumovski Apr 25 '17 at 9:42

The problem was in OWD(Org wide defaults) where community user did not have permission to access User details. After changing permissions from private to public, the problem dissapeared.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.