Activity Forums Salesforce® Discussions What are some use cases for using the Schema class in Salesforce?

  • chanchal kumar

    Member
    July 30, 2018 at 10:56 am

    hello Avnish

    by Schema class you can retrieve data from server without SOQL query

    for eg- get recordTypeId without soql query

    Id contRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('NameOfRecordType').getRecordTypeId();

  • shariq

    Member
    September 17, 2018 at 5:48 pm

    Hi,

    Here is the code which gives best use case of schema for getting fields -

    public Void GetAllField()
    {
    String query ='';
    String SobjectApiName = 'Account';
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

    String strFields = '';

    for(String fieldName : fieldMap.keyset() )
    {
    if(strFields == null || strFields == '')
    {
    strFields = fieldName;
    }else{
    strFields = strFields + ' , ' + fieldName;
    }
    }

    query = 'select ' + strFields + ' from ' + SobjectApiName + ' Limit 10 ';

    List <Account> accList = Database.query(query);

    }

    Hope this helps.

  • Parul

    Member
    September 17, 2018 at 5:57 pm

    Contains methods for obtaining schema describe information. Schema Is data about data. SObjectType is used to check the object type. Pleas try below code it will give you list of object in your org
    For ex:

    Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    List<String> entities = new List<String>(schemaMap.keySet());
    For(String name : entities)
    {
    System.debug('----->'+name);
    }

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos