+ Start a Discussion
Manu Mahajan10Manu Mahajan10 

How can I retrieve the Id of the selected object

Hi I have created a visualforce page in which I have two select list.In one list I am fetching all the objects present in the organization.In other list based on the selected object I am fetching the fields.Now I need to get the Id of the Selected object.How can I retrieve the Id of the selected object.
Manu Mahajan10Manu Mahajan10
Hi I need to fetch Id of Selected Custom Object.I dont need to delete the record.The custom object which I have selected I neeed to get its Id.For Example If I select Test__c object then  I need the Id of Test__c object
Anoop yadavAnoop yadav
Hi,

From where you want to get the Id,
Have you written any controller and Page?
Manu Mahajan10Manu Mahajan10
Hi,I have created two visualforce pages and one controller class.In the First page I am selecting Object from the select list.In the Second list based upon the selected object I need to select the field.Once the field is selected,user will click on the Gotopage button which will navigate user to second Visualforce page.The second page should contain hyperlink to the selected object detail page.For the hyperlink I need Id of the object.

Code is as follows:
 Class:

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;}
    public List<SelectOption> fieldNames;
    public String user{get;set;}
    public String objNm;
    public String fieldNm;
    public String userNm;
    
    // Intialize objectNames and fields
    public String selectedFields {get; set;} 
    Map <String, Schema.SObjectField> fieldMap; 
    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();
        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 String getObjNm()
    {
        return selectedObject;
    }
    
     public String getObjectLabel()
     {
         String objlabel= schemaMap.get(selectedObject).getDescribe().getLabel();
        //DescribeSObjectResult describeSObjectResult =connection.describeSObject(selectedObject);
        //String objlabel=selectedObject.getDescribe().getLabel();
        return objLabel;        
     }
        public List<SelectOption> getFieldNames() 
        {                  
            fieldNames= new List<SelectOption>();
            for(Schema.SObjectField sfield : fieldMap.Values())
            {
                schema.describefieldresult dfield = sfield.getDescribe();
                fieldNames.add(new SelectOption(dfield.getName(),dfield.getLabel()));
            }
            return fieldNames;
        }
        
        public String getFieldNm()
        {
            
            return selectedFields;
           
        }
        
       /*  public String getObjId()
        {
            
            Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();
            String keyPrefix = r.getKeyPrefix();
            system.debug('valueid'+keyPrefix);
            return keyPrefix;         
        }
        */
        
         public String getFieldLabel()
        {
            String fieldLabel=fieldMap.get(selectedFields).getDescribe().getLabel();
            return fieldLabel;
        }
        
        public List<SelectOption> getUsernamelist() 
        {
            List<SelectOption> optionList = new List<SelectOption>();
            optionList.add(new SelectOption('','-empty-'));
            for (User u : [select Id, Username from User])
            {
                optionList.add(new SelectOption(u.Username,u.Username));

           }
           return optionList;    
    }

        public PageReference gotoPage()
        {
           PageReference pr = Page.FinalPageShow;
           //pr.getParameters().put('TestObjectName',selectedObject);
          // pr.getParameters().put('FieldName',selectedFields);
           pr.setRedirect(false);
           Schema.DescribeSObjectResult r =  schemaMap.get(selectedObject).getDescribe();
           system.debug('check'+schemaMap.get(selectedObject).getDescribe());
              
           return pr;
        }
   

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

--------------------------------------------------


Page1:


<apex:page Controller="Describer">
<apex:form id="Describe">
 <apex:commandButton action="{!gotoPage}" value="GoToPage"/>
<apex:pageBlock id="block2" >
 <apex:pageblocksection >
            <apex:selectList value="{!selectedObject}" size="1">
                <apex:selectOptions value="{!objectNames}"/>
                <apex:actionSupport event="onchange" rerender="Describe" action="{!showFields}"/>
            </apex:selectList>
        </apex:pageblocksection>
        <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">
            <apex:selectList value="{!selectedFields}" >
               <apex:selectOptions value="{!fieldNames}"/>
            </apex:selectList>
            <apex:selectList value="{!user}">
            <apex:selectOptions value="{!Usernamelist}"/>
        </apex:selectList><p/>
        </apex:pageblocksection>
        </apex:pageBlock>
</apex:form>
</apex:page>
------------------------------------------------------------

Page2:

<apex:page Controller="Describer">
<apex:form id="Describe">
<apex:pageBlock title="Object Name">

        <apex:pageBlockTable value="{!ObjectLabel}" var="labelobj">

            <apex:column value="{!labelobj}"/> 

        </apex:pageBlockTable>
          </apex:pageBlock> 
         <apex:pageBlock title="Field Details">  
        <apex:pageBlockTable value="{!fieldNm}" var="displayfld">

            <apex:column value="{!displayfld}"/> 
            <apex:column value="{!FieldLabel}"/> 

        </apex:pageBlockTable>

       

       </apex:pageBlock> 

<!--<apex:outputText value="{!objNm}"></apex:outputText>-->
</apex:form>
</apex:page>