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
johncusackjrjohncusackjr 

Visualforce Getting SObject Field's Value

Hi,

I have two wrappers, one for SObject objects, one for field types. Based on the field type I need to retrieve the Sobject field value inside VisualForce page.

 

For example:

 <apex:repeat value="{!fieldsWrapper}" var="field" id="customFieldRepeater">
        <apex:column headerValue="{!field.label}" value="{!f.mySobject.get(field.id).value}"/>
        </apex:repeat>

 But this is giving Syntax error. Extra '.' error.

 

Is there a way to do this in VF page, or do i miss something?

 

Thanks,

John

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Yes for sure it will work but you have to set proper type of any object to SOBject and field that you add should belong to that object.

 

Your class would like this 

public class outerClass{

    public outerClass()
        {
            listWrapper = new list<innerWrapperClass>();
            listWrapper.add(new innerWrapperClass(new Account() , 'Name'));
        }
    
    Public list<innerWrapperClass> listWrapper {get;set;}
    
    public class innerWrapperClass{
         public SObject sObj {get;set;}
         public String field{get;set;}
        public innerWrapperClass(SObject sObj , String  field)
            {
                this.sObj = sObj;
                this.field = field;
            
            }
    }
}

 VFP

<apex:page controller="outerClass">
 
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection >
              <apex:repeat value="{!listWrapper}" var="f">
                  <apex:inputField value="{!f.sObj[f.field]}"/>
              </apex:repeat>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
  
</apex:page>

 I hope it is clear to you now you will see inner class uses SOBject ,  and its type is Account as I pass new account () in constructor.

All Answers

Shashikant SharmaShashikant Sharma

Your syntax should be like this

 

<apex:repeat value="{!fieldsWrapper}" var="field" id="customFieldRepeater">
        <apex:column headerValue="{!field.label}" value="{!mySobject[field]}"/>
        </apex:repeat>

 mySobject : Should be instance of your SObject 

field : Should be API name of the field

johncusackjrjohncusackjr

Thanks Shashikant,

 

But it is giving these errors when I try to save the class with your suggested settings:

ErrorError: Insufficient Privileges

ErrorError: Read access denied for {0}

 

John

aballardaballard

How is fieldsWrapper defined?

johncusackjrjohncusackjr

@aballard,

 

FieldWrapper is like below, and İ read the values into this wrapper over describe sobject and that portion works fine:

 

public class FieldWrapper
{
  private String id;
  private String label;
  
  public Boolean fieldAccessible;
    public Boolean fieldCreateable; 
    public Boolean fieldUpdateable;
    public Boolean fieldNillable;
  
  public FieldWrapper(){}
  
  ... //getter, setters etc.
}

 

SObjectWrapper has similar like below:

 

public class SObjectWrapper
{
    private SObject so = null;
    
    public SObjectWrapper(){}
    
    public SObjectWrapper(SObject a)
    {
        so = a;
    }

....
}

 

Do you have any idea about the problem?

 

Thanks,

John

aballardaballard

I don't understand your page.... What is the <repeat> supposed to do?  You seem to be repeating on a single object?

 

johncusackjrjohncusackjr

That is repeating some custom fields that user selects. For example if user selects 'CreatedBy, CreatedDate, Description or similar custom fields etc.' on a custom configuration tab these fields are repeated in the repeater and their header value is set using the fieldwrapper's related field label. That is working fine.

 

Problem is in the VALUE. This repeater is in a pageBlock and there is a SobjectWrapper that I loop over and get values. That SObject wrapper works fine for the values that are directly set on the controller over wrapper. But for that wrapper's constructor I also set the 'so' variable to an SObject. I need to read the field value of that sobject in visualforce somehow.

Shashikant SharmaShashikant Sharma

What is the salesforce api version of the page. It should be above 20. I would suggest to make it 22 the heighest.

 

in Your class MySOBject should be like this

 

Public SObject MySobject {get;set;}

 

do initialize it in constructor also like

see this example for more : http://forceschool.blogspot.com/2011/06/dynamic-binding-when-object-name-is.html

johncusackjrjohncusackjr

Yes Shashikant,

It should work in normal cases. But my SObject is inside a SObjectWrapper. So to access the SObject, I need to access the SObjectWrapper first.

 

So the value should be something 

 

{!sObjectWrapper.sObject[fieldWrapper.id]}

 

But this is giving those errors that I wrote in previous messages.

 

Thanks,

John

Shashikant SharmaShashikant Sharma

I have created an example for you hope it will help you

 

Controller

 

public class outerClass{

    public outerClass()
        {
            listWrapper = new list<innerWrapperClass>();
            listWrapper.add(new innerWrapperClass(new Account() , 'Name'));
        }
    
    Public list<innerWrapperClass> listWrapper {get;set;}
    
    public class innerWrapperClass{
         public Account acc {get;set;}
         public String field{get;set;}
        public innerWrapperClass(Account acc ,String  field)
            {
                this.acc = acc;
                this.field = field;
            
            }
    }
}

 

VFP

<apex:page controller="outerClass">
  
  <apex:form>
      <apex:pageBlock>
          <apex:pageBlockSection>
              <apex:repeat value="{!listWrapper}" var="f">
                  <apex:inputField value="{!f.acc[f.field]}"/>
              </apex:repeat>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
  
</apex:page>

 

save above and see results.

Bulent703Bulent703

Is it possible that this will work with the explicitly defined Account object but not with the generic SObject?

Shashikant SharmaShashikant Sharma

Yes for sure it will work but you have to set proper type of any object to SOBject and field that you add should belong to that object.

 

Your class would like this 

public class outerClass{

    public outerClass()
        {
            listWrapper = new list<innerWrapperClass>();
            listWrapper.add(new innerWrapperClass(new Account() , 'Name'));
        }
    
    Public list<innerWrapperClass> listWrapper {get;set;}
    
    public class innerWrapperClass{
         public SObject sObj {get;set;}
         public String field{get;set;}
        public innerWrapperClass(SObject sObj , String  field)
            {
                this.sObj = sObj;
                this.field = field;
            
            }
    }
}

 VFP

<apex:page controller="outerClass">
 
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection >
              <apex:repeat value="{!listWrapper}" var="f">
                  <apex:inputField value="{!f.sObj[f.field]}"/>
              </apex:repeat>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
  
</apex:page>

 I hope it is clear to you now you will see inner class uses SOBject ,  and its type is Account as I pass new account () in constructor.

This was selected as the best answer
johncusackjrjohncusackjr

Thanks Shashikant,

That solved my problem.

 

Regards,

John