I have following code to return prefix of any object based on API name of object passed as string. :

private static String  getObjectPrefixID(String objName){

        Schema.DescribeSObjectResult r = genericObject.sObjectType.getDescribe();
        String keyPrefix = r.getKeyPrefix();
        return keyPrefix;

I am getting error as:

Initial term of field expression must be a concrete SObject: String

Not sure how to get the prefix ID in this case.


You can do something like

Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
system.debug('==>m is==>'+m);
Schema.SObjectType s = m.get('Account') ;
system.debug('==>Sobject Type is ==>'+s);
Schema.DescribeSObjectResult r = s.getDescribe() ;
String keyPrefix = r.getKeyPrefix();
return keyPrefix;

Replace Account with your Object API name.


Pass the object name and you will get the prefix of the object

public static String getObjectKeyPrefix(String objName){
    /*objName ='Account';*/
    schema.sObjectType sObjType = Schema.getGlobalDescribe().get(objName);
    return (sObjType.getDescribe().getKeyPrefix());

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.