+ Start a Discussion
kavya mareedukavya mareedu 

Good Morning!!

I want to retrieve all the fields from record type of an particular SObject. Please do let me know how to write the SOQL Query on this.
Hi Kavya,

You can use this query to fecth all fields, I have used contact replace contact with your object name.
String rectype='Personal';// pass your recordtype name here
List<Contact> con=Database.query('Select  ' + string.join(new list<String>(Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().keyset()),',')+ ' from Contact where recordtype.name=:rectype limit 50000');
for(Contact c:con){
    system.debug (c.id);

Ajay K DubediAjay K Dubedi
Hi kavya,

Try the above code. you can change code according to your requirement.
public class TaskData {
    public static void createCommunicatOHistory(){
        Id recordId = '0010o00002J7sWQAAZ';    
        DescribeSObjectResult describeResult = recordId.getSObjectType().getDescribe();    
        List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );    
        String query =      ' SELECT ' +          String.join( fieldNames, ',' ) +      ' FROM ' +          describeResult.getName() +      ' WHERE ' +          ' id = :recordId ' +      ' LIMIT 1 '    ;    
        // return generic list of sobjects or typecast to expected type    List<SObject> records = Database.query( query );    
        System.debug( describeResult );
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Ajay Dubedi