[SalesForce] Apex: Dynamically get fields for an SObject – no hardcoding

It states in Salesforce Apex docs that I can get fields for a particular SObject (standard or custom) in the following way:

Map<String, Schema.SObjectField> M = Schema.SObjectType.Custom__c.fields.getMap();

But this requires hardcoding. as I need to hardcode the API name of the SObject : Schema.SObjectType.

Is there a way to access all fields on an SObject dynamically without hard coding? I am getting a list of all SObjects in my org by calling Schema.getGlobalDescribe(), but this leaves me with API names of individual SObjects. There is no way to get all those SObject's field info dynamically. Am I missing something or it is just not possible?

I want something like:

List <Schema.SObjectField> fieldList = Schema.SObjectType.get(SObject_API_Name).fields;

Best Answer

You don't need to hard code the Object name as you have specified, you can indeed do it dynamically, for example:

SObjectType accountType = Schema.getGlobalDescribe().get('Account');
Map<String,Schema.SObjectField> mfields = accountType.getDescribe().fields.getMap();