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
abivenkatabivenkat 

Syntax to get the field names for a particular object, which changes dynamically?

hi,

 

I have displayed all the object names in a select list and while an object is selected, i am retrieving the object name and i have to select the field names for it. For this i found that the syntax as given below,

 

Schema.DescribeFieldResult f = Schema.sObjectType.Account.fields;

 

The Account object is hardcoded here, but, in my scenario, object name is dynamically changing, how to modify this syntax into dynamically changing one. any ideas or any other syntax's available to get field names?? 

Or is there any soql query to retrieve the field for the requested object? please provide any ideas or suggestions to retrieve the field names...

 

thanks,

abivenkat,

SFDC Learner

Best Answer chosen by Admin (Salesforce Developers) 
ShaTShaT

Hi abhivenkat,

public string object{get;set;}

Here object is dynamic value which object You selecting.

 

Schema.sObjectType sobject_type = object.getSObjectType();
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 

 

Hope this will help you..!!!!!

 

Thanks

Shailu

 

 

All Answers

ShaTShaT

Hi abhivenkat,

public string object{get;set;}

Here object is dynamic value which object You selecting.

 

Schema.sObjectType sobject_type = object.getSObjectType();
Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap(); 

 

Hope this will help you..!!!!!

 

Thanks

Shailu

 

 

This was selected as the best answer
tes2tes2

public virtual class Describe {
public Schema.Describesobjectresult DSR {get; private set;}
private Map<String, SObjectField> mapFields;

/** constructor */
public Describe(string sObjectType) {
try {
Schema.SObjectType sot = Schema.getGlobalDescribe().get(sObjectType);
DSR = sot.getDescribe();
}
catch(exception ex){}
}
public Describe(sobject obj) {
DSR = obj.GetSObjectType().getDescribe();
}


/**
get the field map.
Simliar to fields.getMap() but will cache the calls.
*/
public Map<String, SObjectField> fieldMap {
GET {
try {
if (mapFields==null)
mapFields=DSR.fields.getMap();
return mapFields;
}
catch(exception ex){ return null; }
}
}


}

abivenkatabivenkat

 

thank you very much shailu.. your code is working good.. got the field names for the selected object..

 

is there any way i can query the field names and its values using the soql query and display it in a table???

 

 

 

thanks,

abivenkat,

SFDC Learner

Debd89Debd89

Error: GridRep Compile Error: Method does not exist or incorrect signature: [String].getSObjectType()

Then how did you avoid this error?