0

I'm passing the object name based on the Id of an object and I'm not sure how to convert from String to SObject so if I have hard code the name of the object it works fine.

Schema.DescribeSObjectResult objSchema = Employee__c.sObjectType.getDescribe();

Error:

Initial term of field expression must be concrete sObject: String

Id myid = 'a0Ci000000vd7xA';     
String objectName = myid.getSobjectType().getDescribe().getName();

Schema.DescribeSObjectResult objSchema = objectName.sObjectType.getDescribe();
Map<String, Schema.SObjectField> fieldMap = objSchema.fields.getMap();
for (String fieldName : fieldMap.keySet()){
    SObjectField sfield = fieldMap.get(fieldName);
    schema.describefieldresult dfield = sfield.getDescribe();
    if (null != dfield.getInlineHelpText()) {
        //System.debug(dfield.getName() + ':' + dfield.getInlineHelpText());
        System.debug(dfield.getName());
    }
}
1
  • 1
    Would not myid.getSobjectType() return the same Schema.DescribeSObjectResult object you are expecting in line 3? Mar 22 '17 at 16:25
4

One (particularly inefficient) method is to use Schema.getGlobalDescribe:

DescribeSObjectResult objSchema = Schema.getGlobalDescribe().get(objectName)
    .sObjectType.getDescribe();

I would personally use getSObjectType on an Id, which skips a very expensive describe call:

DescribeSObjectResult objSchema = myid.getSObjectType().getDescribe();

Or, you could even use the key prefix if you wanted to keep it as a string:

DescribeSObjectResult objSchema = Id.valueOf(objPrefix+'0'.repeat(12))
    .getSObjectType().getDescribe();
3
  • sfdcfox,May I ask why you are repeating 0's 12 times here on your last line of code? key prefix is 3 digits but why 12 repetition of 0's?
    – SF Learner
    Mar 22 '17 at 22:19
  • @ShaliniMendu Because an ID is 15 or 18 characters long. So, for example, if you use '001' as the prefix, you would get '001000000000000', which Apex would interpret as an Account Id. The remaining 12 characters (mostly) don't matter as far as Id.getSObjectType is concerned, so you can use 0 values.
    – sfdcfox
    Mar 22 '17 at 22:36
  • So, its not the exact Id that you are trying to create here, it just is to indicate the type of the object(using keyprefix). I thought the system would complain of Invalid Id or something of that sort if it doesn't match with an exact ID.. good to know this little tip!Thanks for your clarification.
    – SF Learner
    Mar 22 '17 at 22:42
1

You can manipulate the DescribeSObjectResult however you wish if you just omit your getName() call:

DescribeSObjectResult describe = myId.getSObjectType().getDescribe();
for (SObjectField field : describe.fields.getMap().values)
{
    if(field.getDescribe().getInlineHelpText() != null))
    {
        system.debug(field);
    }
}

You don't even need to cache the Id:

DescribeSObjectResult describe = Id.valueOf('a0Ci000000vd7xA').getSObjectType().getDescribe();
2
  • getting error Invalid type DescribeSObject
    – Nick Kahn
    Mar 22 '17 at 17:41
  • @NickKahn Just a typo.
    – Adrian Larson
    Mar 22 '17 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.