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
Ramana.r.sfRamana.r.sf 

get all objects dynamically nd place on VF Page.search4record in those obj nd display section wise

 

Hi all, this is very urgent requirement..(Get all Objects dynamically in the system and place on VF Page.If you search for a record that should come under that particular object section)

 

I have 3 sections on VF Page.

 

Section 1: 1 text box(for searching records) and 1 Search button.

 

Section 2 : Get all objects dynamically and place in this section.(if you checked all objects that should search in all objects for that record or if you select particular objects that should search on those selected objects.)

 

Section 3 : the resulted records shold come here along with sections(for example if we found record  in the account object that should come under account section. like wise for all objects sections should come here).

 

 

 

Thanks

Ramana 

 

Navatar_DbSupNavatar_DbSup

Hi,

      Below is the sample code for getting the objects dynamically on Visual force page. You can put the own business logic according to that sample code.

 

 ---------------  Visual force code ------------------
<apex:page Controller="Describer">   
   <apex:form id="Describe">
      <apex:pageBlock id="block2" >
         <apex:pageblockbuttons location="top" >
            <apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>
         </apex:pageblockbuttons>
        <apex:pageblocksection >
          <apex:selectList value="{!selectedObject}" size="1">
           <apex:selectOptions value="{!objectNames}"/>
          </apex:selectList>
        </apex:pageblocksection>
        <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
            <apex:panelBar items="{!fields}" var="fls">
             <apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>
            </apex:panelBar>
        </apex:pageblocksection>
       </apex:pageBlock>
    </apex:form>
</apex:page>

 

-------------- Controller code ------------------

public class Describer 
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public List<Pair> lstfieldname{get;set;}
    public List <Pair> fields {get{return lstfieldname;} set{lstfieldname =value;}}
    public List <SelectOption> objectNames{public get; private set;}
    public String selectedObject {get; set;}
 
    // Intialize objectNames and fields 
    public Describer() {
        objectNames = initObjNames();
        fields = new List<Pair>();
    }
    // Populate SelectOption list  -  
    // find all sObjects available in the organization 
    private List<SelectOption> initObjNames() {
        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;
        }
         
    // Find the fields for the selected object 
     public void showFields() {
        //fields.clear();
        system.debug('$$$$$' + selectedObject);
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
        {
        schema.describefieldresult dfield = sfield.getDescribe();        
        Pair field = new Pair();     
        field.key = dfield.getname();
        field.val = dfield.getType () + ' : ' + dfield.getLabel ();    
        lstfieldname.add(field);
        }
    }
}

  Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Anjireddy BapathuAnjireddy Bapathu

Dear Navatar,

 

I am getting one error this code:

 

Error----  -Describer Compile Error: Invalid type: Pair at line 4 column 17

 

 

public with sharing class Describer { public Map schemaMap = Schema.getGlobalDescribe();

public List lstfieldname{get;set;} public List fields {get{return lstfieldname;} set{lstfieldname =value;}} public List objectNames{public get; private set;} public String selectedObject {get; set;} // Intialize objectNames and fields public Describer() { objectNames = initObjNames(); fields = new List(); } // Populate SelectOption list - // find all sObjects available in the organization private List initObjNames() { List objNames = new List(); List entities = new List(schemaMap.keySet()); entities.sort(); for(String name : entities) objNames.add(new SelectOption(name,name)); return objNames; } // Find the fields for the selected object public void showFields() { //fields.clear(); system.debug('$$$$$' + selectedObject); Map fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap(); for(Schema.SObjectField sfield : fieldMap.Values()) { schema.describefieldresult dfield = sfield.getDescribe(); Pair field = new Pair(); field.key = dfield.getname(); field.val = dfield.getType () + ' : ' + dfield.getLabel (); lstfieldname.add(field); } } }

 

 

please give me vallid code for this example...

 

 

Thanks for u help....

Ramana.r.sfRamana.r.sf

Hi Jain, thanks for giveing reply. In this solution ,all objects coming dynamically nd also getting the  fields of those objects.

 

but my requirement is we need to place objects dynamically along with the checkbox on vf page .

 

then after we need to search records(we will enter keyword in text box)  on those checked (selected) objects.

 

Thanks 

Ramana

 

 

Ramana.r.sfRamana.r.sf

Hi Anjireddy,Place this code.

 

Apex Controller :

 

public class Describer {

private Map <String, Schema.SObjectType> schemaMap =
Schema.getGlobalDescribe();
public List <Pair> fields {get; set;}
public List <SelectOption> objectNames
{public get; private set;}
public String selectedObject {get; set;}

// Intialize objectNames and fields

public Describer() {
objectNames = initObjNames();
fields = new List<Pair>();
}
// Populate SelectOption list -

// find all sObjects available in the organization

private List<SelectOption> initObjNames() {
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;
}

// Find the fields for the selected object

public void showFields() {
fields.clear();
Map <String, Schema.SObjectField> fieldMap =
schemaMap.get(selectedObject).getDescribe().fields.getMap();

for(Schema.SObjectField sfield : fieldMap.Values()){
schema.describefieldresult dfield =
sfield.getDescribe();
Pair field = new Pair();
field.key = dfield.getname();
fields.add(field);
}
}


public class Pair {
public String key {get; set;}
public String val {get; set;}
}
}

 

VF Page : 

 

<apex:page Controller="Describer">
<apex:form id="Describe">
<apex:pageBlock id="block2" >
<apex:pageblockbuttons location="top" >
<apex:commandButton value="Get Describe Object Fields" action="{!showFields}"/>
</apex:pageblockbuttons>
<apex:pageblocksection >
<apex:selectList value="{!selectedObject}" size="1">
<apex:selectOptions value="{!objectNames}"/>
</apex:selectList>
</apex:pageblocksection>
<apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
<apex:panelBar items="{!fields}" var="fls">
<apex:panelBarItem label="{!fls.key}">{!fls.val}</apex:panelBarItem>
</apex:panelBar>
</apex:pageblocksection>
</apex:pageBlock>
</apex:form>
</apex:page>

Ramana.r.sfRamana.r.sf

Hi All,


This is Ramana,we have an urgent requirement but unable to find the solution.

 

We need to place all sObjects on visualforce page dynamically along with checkboxes for each object.

 

Then after if we search for a record on the same page that should display the records in section wise.(for example if the record found in the account that should come under account section).

 

Any kind of help will be greatly appreciated.

 

Thanks in advance
Ramana