You need to sign in to do that
Don't have an account?
How to dynamically display SObject in a VF page? And iterate...
Hi all,
I am new to SalesForce so I appologise in advance for any newbie mistakes.
I'm trying to make a vf page to display the fields of the SObjects returned by an arbitrary query. I found a working solution for displaying the fields of one object in this thread: dynamicSObject. I have tried to take the accepted solution in the thread and iterate over a list of DynamicSObjects instead of showing just one.
When I try the original solution everything works fine.
The iterative code below uploads ok without any error messages.
When I try to view the page with the new iterative version I get the following runtime error:
System.Exception: Too many fields describes: 11
Class.queryTestController.DynamicSObject.getFields: line 61, column 46 External entry point
The position mentioned in the error message is in the line below at "getDescribe()"
for (Schema.SObjectField f : getDescribe().fields.getMap().values()) {
What am I doing wrong?
Thank you for your time!
/Ola
<apex:page controller="queryTestController">
<apex:form >
<apex:inputText value="{!dynamicQuery}" id="dynamicQuery"/>
</apex:form>
<apex:form id="content">
<apex:pageMessages />
<apex:repeat var="object" value="{!unknowns}">
<h1>{!object.sobjectType} [{!now()}]</h1><br/>
<apex:pageBlock title="{!object.id}">
<apex:pageBlockButtons location="top">
<apex:commandButton action="{!object.save}" value="Save" rerender="content"/>
</apex:pageBlockButtons>
<apex:pageBlockTable var="f" value="{!object.fields}">
<apex:column value="{!f.name}" headerValue="Name"/>
<apex:column headerValue="Value">
<apex:inputText value="{!f.value}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:repeat>
</apex:form>
</apex:page>
public class queryTestController {
private List<DynamicSObject> dynamicSObjects;
public string dynamicQuery { get; set;}
public queryTestController () {
dynamicQuery = 'select name,closedate from Opportunity';
}
public List<DynamicSObject> getUnknowns() {
dynamicSObjects = new List<DynamicSObject>();
ApexPages.StandardSetController setCon = new ApexPages.StandardSetController(Database.query(dynamicQuery));
List<SObject> base = (List<SObject>) setCon.getRecords();
for (SObject obj : base) {
dynamicSObjects.add(new DynamicSObject(obj));
}
return dynamicSObjects;
}
public class DynamicSObject {
public DynamicSObject(SObject delegate) {
this.delegate = delegate;
}
public ID getID() {
return delegate.id;
}
public void save() {
Database.upsert(delegate);
}
public String getSObjectType() {
return getDescribe().getName();
}
public List<Field> getFields() {
if (fields == null) {
fields = new List<Field>();
for (Schema.SObjectField f : getDescribe().fields.getMap().values()) {
if (f.getDescribe().getType() != DisplayType.ID) {
try {
Field field = new Field(delegate, f);
fields.add(field);
} catch (System.SObjectException x) {
// Do nothing - just means that the field was not selected in the select list
}
}
}
}
return fields;
}
private Schema.DescribeSObjectResult getDescribe() {
return delegate.getSObjectType().getDescribe();
}
private final SObject delegate;
private List<Field> fields;
}
public class Field {
Field(SObject delegate, Schema.SObjectField f) {
this.delegate = delegate;
this.f = f;
getValue();
}
public String getName() {
return f.getDescribe().getName();
}
public String getValue() {
Object value = delegate.get(f);
return value != null ? String.valueOf(value) : null;
}
public void setValue(String value) {
// TODO: Handle remaining data type conversions...
if (f.getDescribe().getType() == DisplayType.INTEGER) {
delegate.put(f, Integer.valueOf(value));
} else {
delegate.put(f, value);
}
}
private final SObject delegate;
private final Schema.SObjectField f;
}
}
A quick update, the code works as long as the query doesn't return more than 5 results.
Does anyone have a suggestion for how to increase that limit?
All Answers
A quick update, the code works as long as the query doesn't return more than 5 results.
Does anyone have a suggestion for how to increase that limit?
Hi,
Code works great , but i also need it for bulk record not limited to 5 .. anybody got the answer please post here..
Thanks ..