+ Start a Discussion
Rahul Gupta 166Rahul Gupta 166 

Issue to make dynamic table header on basis of picklist

Hi, i have a two picklist value one has a list of object and other is a list of the fields related to object pick list(more than one field can be selected)
If a object is selected and related field are selected it should create a table with records for a particular object.
This is now working for one field in the Particular object. 
when I select the multiple fields for an object it shows error 
Exception: Invalid field name for SObject Account 

Below is vf page  for same :
<apex:page controller="objectController1"  showHeader="false" sidebar="false" > 
    <head>
        <apex:stylesheet value="{!URLFOR($Resource.LIGHTNING_RESOURCE_NAME, 'assets/styles/salesforce-lightning-design-system-vf.css')}" />
    </head>
    <apex:form > 
        <apex:pageBlock > 
            <apex:pageBlockSection > 
                <apex:pageBlockSectionItem > 
                    <apex:outputlabel value="Object Names :"/> 
                    <apex:actionRegion > 
                        <apex:selectList value="{!selectedObject}" size="1"> 
                            <apex:selectOptions value="{!ObjectNames}"/> 
                            <apex:actionSupport event="onchange" rerender="myFields"/> 
                        </apex:selectList> 
                    </apex:actionRegion> 
                </apex:pageBlockSectionItem> 
                <apex:pageBlockSectionItem > 
                    <apex:outputlabel value="Field Names :"/> 
                    <apex:outputPanel id="myFields"> 
                        <apex:actionRegion > 
                            <apex:selectList value="{!selectedField}"  size="10" multiselect="true"> 
                                <apex:selectOptions value="{!ObjectFields}"/> 
                            </apex:selectList> 
                        </apex:actionRegion> 
                    </apex:outputPanel> 
                </apex:pageBlockSectionItem> 
                <Center>
                    <apex:commandButton value="Fetch Fields" action="{!Fetch}"/>
                </Center>
            </apex:pageBlockSection> 
        </apex:pageBlock> 
           <apex:outputPanel layout="block">
                <apex:pageBlock >
                    <apex:pageBlockSection >
                        <apex:pageblockTable value="{!lstobj}"  var="querydata">
                            <apex:repeat value="{!fnlFields}" var="coldata">
                            <apex:column value="{!querydata[coldata]}">
                             </apex:column>
                                </apex:repeat>
                        </apex:pageblockTable>
                    </apex:pageBlockSection>
                </apex:pageBlock>
            </apex:outputPanel>   
    </apex:form> 
</apex:page>
Below is Controller for the Same :
 
public class objectController1{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public String selectedField {get; set;}
    public String picklistValue {get; set;}
    public List<sObject> lstobj {get; set;}
    public List<String> lstfields {get; set;}
    public List<String> fnlFields {get;set;}
    public List<SelectOption> fieldNames {get; set;}
    public List<wrapData> bindData {get;set;}
    
    Public objectController1(){   
        selectedObject ='account';
        //system.debug('HI This is fields'+selectedField );
    }
    
    public List<SelectOption> getObjectNames(){
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        
        for(String name : entities){
            objNames.add(new SelectOption(name,name));
        }
        return objNames;
    }
    
    public List<SelectOption> getObjectFields(){
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
        Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
        fieldNames = new List<SelectOption>();
        for (String fieldName: fieldMap.keySet()){  
            fieldNames.add(new SelectOption(fieldName,fieldName));
            //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
        }
        System.debug('fieldNames-----'+fieldNames);
        return fieldNames;
    }
    
    
    public PageReference Fetch(){
        
        lstobj = new List<sObject>();
        lstfields= new List<String>();
        bindData = new List<wrapData>(); 
        fnlFields = new List<String>(); 
        //System.debug('Select'+selectedField +'From'+selectedObject);
        String s1= selectedField.removeStart('[');
        String s2= s1.removeEnd(']');
        
        String Query='Select '+s2+' From '+selectedObject;
        
        
        lstobj= Database.query(Query);
        System.debug('List '+lstobj);
        
        lstfields = s2.split(',');
        //System.debug('List of fields '+lstfields);
        for(String s : lstfields){
        
        fnlFields.add(s);
        
        }
        System.debug(fnlFields);
        
        
        //bindData.add(new wrapData(selectedField,lstobj));
        return null; 
    }    
}

Please help me to create a table with more than one field with record 
Thanks in Advance
Rahul



 
Alain CabonAlain Cabon
Hi,

Could you post the source for the class wrapData ?

Regards
Rahul Gupta 166Rahul Gupta 166
Hi Alain ,

I am not using Wrap data any more in the class. 
Alain CabonAlain Cabon
Here is a first solution:
 
for(String s : lstfields){   
         
         fnlFields.add(s.trim());           
 
}

... a simple trim and life goes on but you will have other problems with the addresses.

Some fields will have to be excluded or modified.

"Unsupported type common.api.soap.wsdl.Address encountered."

Best regards
Alain
David HalesDavid Hales
Hi Rahul,

You can try this code, it will work for you..
 
public class objectController1
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public String selectedField {get; set;}
    public String picklistValue {get; set;}
    public List<sObject> lstobj {get; set;}
    public List<String> lstfields {get; set;}
    public List<String> fnlFields {get;set;}
    public List<SelectOption> fieldNames {get; set;}
   // public List<wrapData> bindData {get;set;}
       Public Integer size{get;set;} 

    public List<SelectOption> paginationSizeOptions{get;set;}
    Integer counter = 0;//TO track the number of records parsed
    Integer limitSize = 10;//Number of records to be displayed
    Integer totalSize =0; //To Store the total number of records available
    public List<sObject> datatoShow{get; set;}
    
    Public objectController1(){   
        selectedObject ='account';
        //system.debug('HI This is fields'+selectedField );
         paginationSizeOptions = new List<SelectOption>();
         paginationSizeOptions.add(new SelectOption('5','5'));
         paginationSizeOptions.add(new SelectOption('10','10'));
         paginationSizeOptions.add(new SelectOption('20','20'));
         paginationSizeOptions.add(new SelectOption('50','50'));
         paginationSizeOptions.add(new SelectOption('100','100'));
    }
    
    public List<SelectOption> getObjectNames(){
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        
        for(String name : entities){
            objNames.add(new SelectOption(name,name.toUpperCase()));
        }
        return objNames;
    }
    
    public List<SelectOption> getObjectFields(){
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType ObjectSchema = schemaMap.get(selectedObject);
        Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
        fieldNames = new List<SelectOption>();
        for (String fieldName: fieldMap.keySet()){  
            fieldNames.add(new SelectOption(fieldName,fieldName.toUpperCase()));
            //fieldMap.get(fieldName).getDescribe().getLabel();//It provides to get the object fields label.
        }
        System.debug('fieldNames-----'+fieldNames);
        return fieldNames;
    }
    
    
    public PageReference Fetch(){
        
        lstobj = new List<sObject>();
        lstfields= new List<String>();
        fnlFields = new List<String>(); 
        datatoShow= new List<sObject>();
        //System.debug('Select'+selectedField +'From'+selectedObject);
        String s1= selectedField.removeStart('[');
        String s2= s1.removeEnd(']');
        
        String Query='Select '+s2+' From '+selectedObject;
        
        
        lstobj= Database.query(Query);
        System.debug('List '+lstobj);
        totalSize = lstobj.size();
        
        //Intial adding of contacts to ContactsToShow
        //check the total records are more than limitSize and assign the records
        if((counter+limitSize) <= totalSize){
            for(Integer i=0;i<limitSize;i++){
                datatoShow.add(lstobj.get(i));
            }
        }else{
            for(Integer i=0;i<totalSize;i++){
                datatoShow.add(lstobj.get(i));
            }
        }
        lstfields = s2.split(',');
        
        System.debug('---- lstFields: ' + lstFields);
        //System.debug('List of fields '+lstfields);
        for(String s : lstfields){
        
        fnlFields.add(s.trim());
        
        }
        System.debug(fnlFields);
        
        
        return null; 
    }  
     public void beginning(){
   
        datatoShow.clear();
        counter=0;
        if((counter + limitSize) <= totalSize){
       
            for(Integer i=0;i<limitSize;i++){
                datatoShow.add(lstobj.get(i));
            }   
           
        } else{
       
            for(Integer i=0;i<totalSize;i++){
                datatoShow.add(lstobj.get(i));
            }       
           
        }
       
    }
   
    public void next(){
   
        datatoShow.clear();
        counter=counter+limitSize;
       
        if((counter+limitSize) <= totalSize){
            for(Integer i=counter-1;i<(counter+limitSize);i++){
                datatoShow.add(lstobj.get(i));
            }
        } else{
            for(Integer i=counter;i<totalSize;i++){
                datatoShow.add(lstobj.get(i));
            }
        }
    }
   
    public void previous(){
   
        datatoShow.clear();

        counter=counter-limitSize;       
       
        for(Integer i=counter;i<(counter+limitSize); i++){
            datatoShow.add(lstobj.get(i));
        }
    }

    public void last (){
   
        datatoShow.clear();
       
        if(math.mod(totalSize , limitSize) == 0){
            counter = limitSize * ((totalSize/limitSize)-1);
        } else if (math.mod(totalSize , limitSize) != 0){
            counter = limitSize * ((totalSize/limitSize));
        }
       
        for(Integer i=counter-1;i<totalSize-1;i++){
                datatoShow.add(lstobj.get(i));
        }
       
    }
   
    public Boolean getDisableNext(){
   
        if((counter + limitSize) >= totalSize )
            return true;
        else
            return false ;
    }
   
    public Boolean getDisablePrevious(){
   
        if(counter == 0)
            return true;
        else
            return false ;
    } 
    public PageReference refreshPageSize() {
         
         return null;
         
    }
  
}

The visualforce page is here:
 
<apex:page controller="objectController1" standardStylesheets="false"  showHeader="false" sidebar="false" > 
     <head>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
    </head>
           <apex:includeScript value="https://cdn.datatables.net/scroller/1.4.2/js/dataTables.scroller.min.js" />
           <apex:includeScript value="https://cdn.datatables.net/scroller/1.4.2/css/scroller.dataTables.min.css"/>
    <apex:form > 
        <apex:pageBlock > 
            <apex:pageBlockSection > 
                <apex:pageBlockSectionItem > 
                    <apex:outputlabel value="Object Names :"/> 
                    <apex:actionRegion > 
                        <apex:selectList value="{!selectedObject}" size="1"> 
                            <apex:selectOptions value="{!ObjectNames}"/> 
                            <apex:actionSupport event="onchange" rerender="myFields"/> 
                        </apex:selectList> 
                    </apex:actionRegion> 
                </apex:pageBlockSectionItem> 
                <apex:pageBlockSectionItem > 
                    <apex:outputlabel value="Field Names :"/> 
                    <apex:outputPanel id="myFields"> 
                        <apex:actionRegion > 
                            <apex:selectList value="{!selectedField}"  size="10" multiselect="true"> 
                                <apex:selectOptions value="{!ObjectFields}"/> 
                            </apex:selectList> 
                        </apex:actionRegion> 
                    </apex:outputPanel> 
                </apex:pageBlockSectionItem> 
                <Center>
                    <apex:commandButton value="Fetch Fields" action="{!Fetch}"/>
                </Center>
            </apex:pageBlockSection> 
        </apex:pageBlock> 
           <apex:outputPanel layout="block" id="pbt1">
                <apex:pageBlock reR>
                
                    <!--<apex:pageblockTable value="{!lstobj}"  var="querydata">
                        <apex:repeat value="{!lstfields}" var="coldata">
                        <apex:column value="{!querydata[coldata]}">
                         </apex:column>
                            </apex:repeat>
                    </apex:pageblockTable>
                    -->
                    <table style="width: 100%;"  id="mytable"  border="1">
                        <tr>
                            <apex:repeat value="{!lstFields}" var="fields">
                                <th>{!fields}</th>
                            </apex:repeat>
                        </tr>
                        <apex:repeat value="{!datatoShow}" var="eachRecord">
                            <tr>
                                <apex:repeat value="{!fnlFields}" var="eachField">
                                    <td>{!eachRecord[eachField]}</td>
                                </apex:repeat>
                            </tr>
                        </apex:repeat>
                    </table>
                </apex:pageBlock>
            </apex:outputPanel>  
            <apex:outputpanel id="nav_btn" style="float:right" >
               <apex:commandButton value="<<"  action="{!beginning}" disabled="{!DisablePrevious}" reRender="pbt1,nav_btn"/>
                <apex:commandButton value="<" action="{!previous}" disabled="{!DisablePrevious}" reRender="pbt1,nav_btn" />
                <apex:commandButton value=">" action="{!next}" disabled="{!DisableNext}" reRender="pbt1,nav_btn"/>
                <apex:commandButton value=">>" action="{!last}" disabled="{!DisableNext}" reRender="pbt1,nav_btn"/>   
            </apex:outputpanel>
            <apex:selectList value="{!size}" multiselect="false" size="1" onchange="refreshPageSize();">
           <apex:selectOptions value="{!paginationSizeOptions}"/>
             </apex:selectList>
    </apex:form> 
</apex:page>

Mark it best answer if it is working fine. Have a great day!!

Thanks & Regards
Mohit Sharma (1028)


 
Alain CabonAlain Cabon
Hi David,

Great work but it is still not stable (more complete code step by step at least).
  • 35:  <apex:pageBlock reR>  (a typo when you have done your copy/paste) but it lacks perhaps some lines of your code.
  • Error; unexpected token: 'From' Error is in expression '{!Fetch}' in component <apex:commandButton> in page objectcontroller1: Class.objectController1.Fetch: line 69, column 1 if you change the page size and click on "Fetch" again.
  • and stiil Unsupported type common.api.soap.wsdl.Address encountered. if you want all the fields of Account for my developer org.
The idea of Rahul Gupta is excellent and your improvement is great but it is quite difficult to have a stable result.

Regards
Alain