0

We have a remote action used to retrieve data and present it to external users. Among other things, we want User fields that are a lookup on a custom object. (User__c is a lookup field to User)

SELECT Id, User__c, User__r.Name, Active__c FROM Custom_Object__c

To return the value we have a wrapper object, with a constructor like this:

public WrapperObject(Custom_Object__c customObject){
    this.Id = customObject.Id;
    this.UserId = customObject.User__c;
    this.UserName = customObject.User__r != null ? customObject.User__r.Name : '';
    this.active = customObject.Active__c;
}

But here's the thing, customObject.User__r != null always resolves to false. It doesn't bring the drill down Name field on User.
But if I run this code as an internal user I do get the User field.
The code is running with sharing because of data security. The profile has read access to Custom_Object__c and all its fields.

EDIT:
After additional testing it appears that the external user cannot query any User.
SELECT Id, Name FROM User WHERE Id = :idParam returns null.

2 Answers 2

0

Wrapper class code will be like this:

public WrapperObject(Custom_Object__c customObject){
    this.Id = customObject.Id;

    Custom_Object__c obj = [SELECT Id, User__c, User__r.Name, Active__c 
                            FROM Custom_Object__c
    WHERE Id=:customObject.Id];

    this.UserId = obj.User__c;
    this.UserName = (obj.User__c != null && obj.User__r.Name !=null) ? obj.User__r.Name : '';
    this.active = customObject.Active__c;
}

To find the User__r.Name, you need to execute a separate SOQL query, otherwise Name will not get displayed.

8
  • That will cause null exception if no User__r is retrieved. The purpose of checking for null record is to make sure that I don't try to access the field of a nonexistent record or object in memory. Jul 26, 2018 at 11:39
  • That still doesn't solve the problem because the User__r.Name field is not being retrieved. It is being retrieved if an internal user initiates the query. Jul 26, 2018 at 11:42
  • you need to run a SOQL query to retrieve User__r.Name Jul 26, 2018 at 11:44
  • Like this? SELECT Id, User__c, User__r.Name, Active__c FROM Custom_Object__c ? Jul 26, 2018 at 11:44
  • Yes, and place this code in a class mentioning without sharing Jul 26, 2018 at 11:45
0

Organizational. Wide. Defaults.
The default User OWD setting is Private for External Useres. Setting it to Public Read Only allows the queries to return with data.

You must log in to answer this question.

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