Aptitude (12) C (2) C++ (10) CSS (1) Data Structure (2) English (7) General (14) HTML (3) Integration (1) Itinerary and Travel Cost (1) Java (29) JQ (5) JS (5) JSP (1) Lightning (14) logical (3) Math (1) Music Notation (17) MySql (4) Network (1) Regex (1) salesforce (131) Servlet (4) SF Admin (31) SF Dev (102) SQL (4) SQL_SERVER (1) StudyPoints (2) ytChannels (2) अन्ताक्षरी (4)

getObjectType by recordId or Prefix

  public static String objectType(String recordIdOrPrefix){
        String objectName = '';
        String IdPrefix = String.valueOf(recordIdOrPrefix).substring(0,3);
             
            //Get schema information
            Map<String, Schema.SObjectType> gd =  Schema.getGlobalDescribe(); 
             
            //Loop through all the sObject types returned by Schema
            for(Schema.SObjectType stype : gd.values())
            {
                Schema.DescribeSObjectResult r = stype.getDescribe();
                String prefix = r.getKeyPrefix();
               if(prefix!=null && prefix.equals(IdPrefix ))
               {
                    objectName = r.getName();
                    break;
                }
            }
        return objectName;
    }

No comments: