1

I have a list of sObject which I have created dynamically. This is how the list is populated.

(Contact:{OtherCountry=null, Division_Type__c=null}, Account:{AccountNumber=null})

I don't want to hard code any field. I need fields should be displayed dynamically. My controller code for list

public List<sObject> sobjlst {get; set;}
    sobjlst = new List<sObject>();
    for(sObject sobj : sObjectFieldMap.keyset()){

        SObject newobj = sobj;
        system.debug('object Name before loop ' + sobj);
        for(SObjectField sobjFd : sObjectFieldMap.get(sobj)){
            system.debug('object Name ' + sobj + ' Fields ' + sobjFd);
            //SObject newobj = sobj{sobjFd};

            newobj.put(sobjFd, '');
            system.debug('The fields added to object ' + newobj);

            //break;                
        }           
        sobjlst.add(newobj);
        system.debug('object details ' + sobj);
        //break;    
    }   

My VF page :

   <apex:pageBlock >

       <apex:pageBlockTable value="{!sobjlst}" var="sobj">

       </apex:pageBlockTable>

   </apex:pageBlock>

when I add <apex:inputFied value="{!sobjlst[sobj]}"/> between apex:pageBlockTable getting error :

Error Error: Read access denied for null

Please help me how can I get the solution. Or suggest me the best way to solve this issue.

1
  • 1
    You have more issues than that, even if you did get the sObject to display how the heck would you know which fields to display as it does not seem the field names will be all the same. And you cannot input field on the sObject, you will have to input field on the field like sObj.AccountName. Since sObj is the actual item at index X in the list
    – Eric
    Commented Sep 29, 2014 at 10:43

1 Answer 1

2

The apex:pageBlockTable is iteration over the sobjlst List and using the sobj variable to store each element in the list.

You can use sobj directly to access the fields you are interested in. See example below:

<apex:pageBlock title="My Content">
    <apex:pageBlockTable value="sobjlst" var="sobj">
        <apex:column value="{!sobj['MyField__c']}"/> 
    </apex:pageBlockTable> 
</apex:pageBlock> 

If you wanted to use apex:inputField you can do it as shown below

You will need to make sure that the field names you are using exist on both Contact and Account or else you will get an error.

See the documentation for apex:pageBlockTable and apex:inputField for examples of them being used.

0

You must log in to answer this question.

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