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?
@AuraEnabled
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';
}
try{
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;
if(!sObjectReturned.isEmpty()){
// THE LINE BELLOW IS PRODUCING ERROR
articleOwnerName = (String) sObjectReturned[0].getSobject('Article_Owner__r').get('Name');
}
else{
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();
}
return'false';
}
message = Attempt to de-reference a null object, cause = null, line = 28