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
munna123munna123 

how to create check boxes for all fields of the object selected from dropdown

i got a code in which we get a dropdown of all objects available and also the drop down of the fields.. when we select an object from dropdown, it automatically generates list of fields of that particular object in dropdown.  but i need to create a page so that when we select an object from drop down, and we click a button like "add", the list of fields of that particular object should get displayed with checkboxes.. if we checkin some fields, the values of that fields should be displayed..

the code am having is...

can any one help to get out of this




<apex:page controller="objectController">
<apex:form > 
      <apex:pageBlock >
          <apex:pageBlockSection columns="2">

              <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="1">
                                <apex:selectOptions value="{!ObjectFields}"/>
                            </apex:selectList>
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem>

          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

*************************************************************************


public class objectController
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    public String selectedObject {get; set;}

    public String selectedField {get; set;}

    Public objectController()
    {   
        selectedObject = 'account';
    }

    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();
            List<SelectOption> 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.
            }
            return fieldNames;
      }       
}
ashishcloudashishcloud

Hi Nanda,

You should you <apex:selectcheckboxes> tag to make the list of field a checkbox group like below:
 

<apex:selectCheckboxes value="{!selectedFields}"> <apex:selectOptions value="{!ObjectFields}"/> </apex:selectCheckboxes>
 



Note: Your selectedFields property should be List of string to hold all checked fields values.

You can show values of selected fields by two ways:

  1. By using javascript 
  2. By showing selectedFields values by reRendering.
Please let me know if you want some more help.

Cheers!!

 

munna123munna123
if u dont mind can u modify my code
munna123munna123
hiii bro, my requirement is when i select the required object fom dropdown and click "add" button, i have to get the list of fields of that particular object with checkboxes. when i checkin some fields like phone, name and all then i need to get all the records of that object with the selected field values. i have a program regarding this. but the thing is its not working for custom objects. it is giving some error... and i want to improve my program so that if i checkin and click " view report" button, i have to get the records of that particular object with selected fields and the list of notes and attachments (if present) for all records of that selected object... i got upto the field types. after selecting checkins of the required field types, am not able to get the records with the filed value of the particular object. and even i need to get the notes and attachments list of that particular record in another pageblock section.. can u guide me plz here is my program: