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 checkboxes for field types to get selected field values

hiii,
           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...
 
here is my code

vf page:
-----------
<apex:page controller="objectController">
<apex:form > 
      <apex:pageBlock >
          <apex:pageBlockSection title= "select the objects below">
<apex:pageBlockSectionItem >
                  <apex:outputlabel value = "Object Names :" /> 
                      <apex:actionRegion >      
                           <apex:selectList value="{!selectedObject}" size="1">
            <apex:selectOption itemValue="Account" itemLabel="Account" />
            <apex:selectOption itemValue="Contact" itemLabel="Contact"/>
            <apex:selectOption itemValue="Lead" itemLabel="Lead" />
            <apex:selectOption itemValue="report" itemLabel="report" />
            <apex:selectOption itemValue="campaign" itemLabel="campaign" />
            <apex:selectOption itemValue="merchandise" itemLabel="merchandise" />
            <apex:selectOption itemValue="invoice" itemLabel="invoice" />
            <apex:selectOption itemValue="case" itemLabel="case" />
            <apex:selectOption itemValue="solution" itemLabel="solutions" />
            <apex:selectOption itemValue="document" itemLabel="document" />
            <apex:selectOption itemValue="Opportunity" itemLabel="Opportunity"/>
            <apex:actionSupport event="onchange" rerender="myFields"/>
                            </apex:selectList>
                          <apex:commandbutton value="add"/>
                     </apex:actionRegion>                         
              </apex:pageBlockSectionItem>
               </apex:pageBlockSection>
          
<apex:pageBlockSection title ="these are the fields....">
              <apex:pageBlockSectionItem >
                      <apex:outputlabel value="Field Names :"/>   
                      <apex:outputPanel id="myFields">   
                        <apex:actionRegion >  
                           <apex:selectCheckboxes value="{!selectedField}">
    <apex:selectOptions value="{!ObjectFields}" />
    </apex:selectCheckboxes>
      <apex:commandbutton value="view report"/>     
                        </apex:actionRegion>      
                     </apex:outputPanel>
              </apex:pageBlockSectionItem>
          </apex:pageBlockSection>
             
      </apex:pageBlock>
  </apex:form>
</apex:page>

apex:
------------
public class objectController
{
    public Map <String, Schema.sObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public String selectedField {get; set;}
    
    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));
             
            }
            return fieldNames;
      }       
}  
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Your code seems OK. What error are you getting? When it happens? 

Regards.
munna123munna123
when i select custom objects from drop down am getting error as below: Visualforce Error Help for this Page System.NullPointerException: Attempt to de-reference a null object Class.objectController.getObjectFields: line 30, column 1 and even i want to get the records with selected checkbox field types and also notes and attachments of those recors ( if present) by clicking the "view report" button.... can u help me in this
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Which object are you getting this error? Be sure that when you populates your picklist you're appending the suffix notation __c to your custom objects in the picklist's itemValue attribute.
<apex:selectOption itemValue="MyCustomObject__c" itemLabel="My Custom Object" />
Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior
Hello,

Have you solved your problem?

Regards.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
munna123munna123
hiii , 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... here is my code vf page:
munna123munna123
hiiii zuinglio, i have not solved the problem yet.... can u help me to get the list of records of selected checkin fields of a particular objected from dropdown... again am posting u the scenario. plzz go through it... 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. 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... apex: