Activity Forums Salesforce® Discussions Write Salesforce Apex Code to take RecordID as input and print Object name and field names of sObject.

  • shariq

    Member
    September 22, 2018 at 9:38 pm

    List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
    Map<String,String> objectMap = new Map<String,String>();
    for(Schema.SObjectType f : gd)
    {
    objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
    }

    String sampleId ='00390000003LIVw';
    String prefix = sampleId.substring(0,3);
    String objectName = objectMap.get(prefix);
    System.debug('** SObject Name ** '+objectName);

    Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();
    List<String> fieldList = new List<String>();
    fieldList.addAll(desResult.keySet());
    for(integer i =0;i<fieldList.size();i++)
    {
    System.debug('** Field Name ** '+fieldList[i]);
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos