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
Reppin__505Reppin__505 

VisualForce Page Problem Returning Simple SObject Field

 

Hello,

I have a problem using the sObject return type in my visual force controller. I would like to be able to make a return statement which could return seperate objects.

For instance the following statement would return a Notes field.
	public sObject getBody()
	{
		if(titleID != null)
		{
		return [Select Body from Note Where Id = :titleID limit 1];
		}
		else
		{
		return null;
		}
	}
However i get an error when attempting to pull this field on my visual force page.
Error: Read access denied for {0}
          <apex:outputField id="theBodyMessage" value="{!Body.Body}"/>
I'm using sObject because i may want to pull fields from the Activity Object instead of the Notes object, but the decision would be made dynamically.
Any information on why i'm getting this error would be greatly appreciated.

 

bob_buzzardbob_buzzard

I think the page wouldn't be able to figure that the sobject has a field called Body, as that's not guaranteed to be present for all sobjects.

 

You should  may be able to get round this using dynamic bindings.  Try something like:

 

 

<apex:outputField id="theBodyMessage" value="{!Body['Body']}"/>

 

 

Reppin__505Reppin__505

Hi Bob, 

 

I tried using that, and what that allowed me to do is save the visual force page after making that edit, when previously it would not allow me to do so. However when refreshing the visual force page i got the same error on the page instead.

 

The core of my issue is looking for the ability to comine objects into a generic object on the fly. This generic object would need to keep the integrity of the field like date, text area, and number formats.

 

Now a salesforce instance of this kind of solution is the Activity History object. You can go to the Account or Contact object and see a related list called Activity History which is a combination of Events and Tasks. Granted, this is a an unexposed actual object that actually stores the values in the DB. But what about having something like that which does not commit data to the DB, but can be exposed as a collection on the visual force page.

 

Is it possible to created an sobject which mashes two objects together on the fly while maintaining field integrity?

 

Now i know you're always on these boards Bob, so if you say it's not possible then it simply is not possible. :)

 

Thanks.

bob_buzzardbob_buzzard

Thank you for those kind words.

 

Unfortunately, generic sobjects are a reference to a concrete instance of a standard/custom sobject.   There's no way to instantiate an "untyped" sobject.

 

There's a couple of techniques that I've used in the past to handle these situations.  Neither are a silver bullet, but they may be acceptable for your use case:

 

(1) Create a custom "carrier" sobject. that has a number of fields of each available type (e.g. 5 text area, 5 date, 5 number etc).  The downside is that you've burnt an object out of your edition allowance. The upside is that you get the additional functionality that comes with fields in apex components.  

 

(2) Use custom wrapper classes.  These allow a representation of a generic sobject, and you can add "fields" on demand assuming you've coded support for that.  The downside is that you aren't using sobject fields, so the additional functionality that comes for free (lookups, date pickers etc) isn't there, so you have to go without or reimplement.

rsoesemannrsoesemann

Doesn't this example exactely prove that this schould work?

http://forceschool.blogspot.com/2011/06/dynamic-binding-when-object-name-is.html

 

I found this post because I exactely can't get the same thing to run.

The funny thing is, the example works perfectly, but in my code. the Read Access error won't just go away