How to get object Prefix from string type of object name?

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());
}