function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cpetersoncpeterson 

visualforce object relationships not working in sites, but working in full salesforce

So I have an interesting issue that seems to only manifest on sites/customer portal. As I'm only using portal for authentication and this code is all being run by custom controllers under sites this seemed like the appropriate place to post it.

 

I have a custom controller that looks something like this: 

public without sharing class Controller {


[irrelevant code cut out]

public List<Service_Availability_Link__c> getServiceList(){
List<Service_Availability_Link__c> ServiceList;
try {

ServiceList = [Select Name, Service__r.Name From Service_Availability_Link__c r
where Department__c = :myContact.Department__c and Service__c != null];

} catch (Exception e) {
System.debug('******Cought an exception! '+e);
}
System.debug('**# Service list looks something like this: '+ServiceList[0].Service__r.Name);
return ServiceList;
}

 And here's a especially simple VF page that I made to help illustrate the issue:

 

<apex:page controller="Controller" showHeader="false">
<apex:repeat value="{!serviceList}" var="s">
<apex:outputText >Data: {!s.Service__r.Name}<br /></apex:outputText>
</apex:repeat>
</apex:page>

 

This is the interesting part, the custom controller is running without sharing (to help debug this issue, I plan to enable sharing rules), and when I turn on debug logs I can see the "System.debug('**# Service list looks something like this: '+ServiceList[0].Service__r.Name);"  statement printing the data I want, but the data is not accessible on the VF page.

 

Here's what I get when I load the VF page.

In full salesforce (not using sites):

 

Data: Salesforce Account

Data: CEO Laptop Service

Data: New Laptop

Data: InSite Monitoring Seat License

 And when I run it under sites with a customer portal user I get this output:

 

Data:

Data:

Data:

Data:

 

Even stranger, if I change the VF page to print  {!s.Name} instead of {!s.Service__r.Name} it prints data (just not the data I want)! The issue seems to be only with traversing object relationships, and from the debugging statements I can see the data is being pulled correctly.

 

 

 

Is this a bug in visualforce under sites? I can't figure out any reason why it would behave like this. 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BulentBulent
You need to check your site access settings and make sure your site has read access to the objects (and the related objects), field level visibility to the related fields. It seems like field level visibility is not set properly

All Answers

BulentBulent
You need to check your site access settings and make sure your site has read access to the objects (and the related objects), field level visibility to the related fields. It seems like field level visibility is not set properly
This was selected as the best answer
cpetersoncpeterson

That was exactly the issue. I assume that when I set the object as viewable/editable by the profile that that would set the fields to default visible, which I now realise might not be desirable behavior depending on how your org uses salesforce.

 

For anybody else who hits the issue:

Go to setup > create > objects > {whatever object}

Click a field, and hit the "View Field Accessibility" button. From there you can change the fields associated the a profile to visible.

 

My only question is if there's a quicker way to set this? It could be time consuming for larger apps to go through this for ever object and every profile. 

BulentBulent

what you suggested 

>>Go to setup > create > objects > {whatever object}

would work for portal and any other profiles. But for sites you will have to go to sites>public access settings

 

You might not want some users creating new fields on some objects and these fields start showing up on your public site.