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
ANKITAANKITA 

Reg: Dynamic Apex Requirement

Hi,

I have a dropdownlist and a button called "display"

Initially i displayed all the objects in my org into dropdownlist.

Select any object then click on "display" button.

Now i would like to display all fields (<apex:inputfield value="{!Account.name}"/>) including lable and componenet type in page block table.

I know how to get all the object into drop down list.

Then i need to perform the save operation.

But after that i am confusing to approach further.

Any suggestions moves me to achieve this.

 

 

 

Thanks,

ANKITA

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

You must Have prapared a picklist of Objects , where you select object,

You must have bind its selected value to some controller property

 

On change of it you should pass the selected value which is object API Name. 

 

Could you please share your code contrller and VFP how have you created picklist of Objects.

All Answers

Shashikant SharmaShashikant Sharma

I think you need Field map from Object that you selected , try this it should help you

Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();  
        //In this example I have hard coded the object name  
        Schema.sObjectType sObjType = globalDescription.get('Account');  
          
        sObjectToBind = sObjType.newSObject();  
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();  
          
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  

r1.fields.getMap();  this will give you fields

ANKITAANKITA

Hi Shashi

Thanku so much for your reply.

Schema.sObjectType sObjType = globalDescription.get('XXXXXXXX'); 

Here in above logic, i need to place my selected object dynamically at 'xxxxxxx'.

Please reply Shashi

 

Thanks,

ANKITA

 

Shashikant SharmaShashikant Sharma

Yes you need to put API anme of your selected object

like Account,Contact,

or for custom object it would be like MyCustomObject__c

ANKITAANKITA
Schema.sObjectType sObjType = globalDescription.get('Account');

In the above, you placed object name by static.

But i need to place the selected object dynamically in this place (instead of 'Account') . So that i can access the field apis.

I don't know how to put my selected object dynamically instead of 'Account'.

 

 

Thanks,

ANKIT

Shashikant SharmaShashikant Sharma

You must Have prapared a picklist of Objects , where you select object,

You must have bind its selected value to some controller property

 

On change of it you should pass the selected value which is object API Name. 

 

Could you please share your code contrller and VFP how have you created picklist of Objects.

This was selected as the best answer
ANKITAANKITA

 Used to get the list of Objects from your Organization

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

<apex:page controller="CustomObjCon">
      <apex:form >
          CustomObjectNames:
             <apex:selectList value="{!pickname}" multiselect="false" size="1">
                      <apex:selectOptions value="{!names}">
                      </apex:selectOptions>
             </apex:selectList>
      </apex:form>
</apex:page>


 

 

 

public class CustomObjCon
{
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
public List<SelectOption> getNames()
{
          List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();   
          List<SelectOption> options = new List<SelectOption>();
    
           for(Schema.SObjectType f : gd)
          {
                 if(f.getDescribe().getName().contains('__c')) // for custom objects only
                options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
          }
          return options;
}

public String pickname { get; set; }
}


 

 

Thanks,

ANKITA