0

I'm trying to get the DescribeSObjectResult by writing the following code:

public String fObjectName {get;set;}

fObjectName = (String)Apexpages.currentPage().getParameters().get('objname');
DescribeSObjectResult objSchema = Schema.getGlobalDescribe().get(fObjectName).sObjectType.getDescribe();

Getting error:

Expression of type Schema.SObjectType has no member named sObjectType

1 Answer 1

2

The global describe call returns a Map of Strings to sObjectType

Map<String,SObjectType>();

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_schema.htm

Simply use

Schema.getGlobalDescribe().get(fObjectName).getDescribe();

The Error

Expression of type Schema.SObjectType has no member named sObjectType

Also explains that the Schema.SObjectType is the working value and has no member of SObjectType because it is already that

You should probably cache the results of the global describe as well if you plan on using the results again as it is a fairly expensive call.

6
  • +1 although as a Reminder; you can't cache DescribeSObjectResult or DescribeFieldResult classes. The global describe itself can be cached, though.
    – sfdcfox
    Commented Mar 28, 2017 at 0:13
  • @Eric: if I tried the following DescribeSObjectResult objSchema = Schema.getGlobalDescribe().get(fObjectName).getDescribe(); I get the following error : System.NullPointerException: Attempt to de-reference a null object but I debug the fObjectName and I'm getting the value which is employee__c but if I hardcode like this DescribeSObjectResult objSchema = Schema.getGlobalDescribe().get('employee__c').getDescribe(); then its working, why is it not taking fObjectName ?
    – Nick
    Commented Mar 28, 2017 at 1:21
  • @NickKahn - Its not case sensitive so either your debug is in the wrong place and it is null when you use it in the above or it is not actually employee__c like you think. Are you sure its 2 __ and not one? There is nothing special about the property that is different than a hardcoded string unless they are not the same
    – Eric
    Commented Mar 28, 2017 at 1:24
  • its odd that when hard-coded its working and if it passing as a string param then complaining about de-reference a null and also i found the similar issue if it passed as variable then its returning null, please see developer.salesforce.com/forums/?id=906F00000009AsUIAU
    – Nick
    Commented Mar 28, 2017 at 1:34
  • maybe its casting issue? I tried cast to DescribeSObjectResult objSchema = Schema.getGlobalDescribe().get(string.valueOf(fObjectName)).getDescribe();
    – Nick
    Commented Mar 28, 2017 at 1:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .