+ Start a Discussion
DEV_CGDEV_CG 

how to get the filed names of sobject that we queried

hi everyone,
 I would like to get the filed values of that we queried...below i would get the schema.sobjectfileds...
i have related object fileds in my query like account.name,account.type,account.industroy ,name,id from contact.
I would like store only above files and i wold like get the vaues respectively using schema.SobjectField 

private void discoverAccessibleFields(sObject newObj) {
32        list< Schema.SobjectField> accessibleScheama = new List< Schema.SobjectField>();
33        Map<String, Schema.SobjectField> fields =
34            newObj.getSObjectType().getDescribe().fields.getMap();
35        for ( Schema.SobjectField s : fields.values()) {
36            accessibleScheama .add(a)
38            }
39        }
40    }
SandhyaSandhya (Salesforce Developers) 
Hi,

You can refer below sample code.
public static Map<String, Schema.DescribeFieldResult> getFieldMetaData(
  Schema.DescribeSObjectResult dsor, Set<String> fields) {
	
  // the map to be returned with the final data
  Map<String,Schema.DescribeFieldResult> finalMap = 
    new Map<String, Schema.DescribeFieldResult>();
  // map of all fields in the object
  Map<String, Schema.SObjectField> objectFields = dsor.fields.getMap();
		
  // iterate over the requested fields and get the describe info for each one. 
  // add it to a map with field name as key
  for(String field : fields){
    // skip fields that are not part of the object
    if (objectFields.containsKey(field)) {
      Schema.DescribeFieldResult dr = objectFields.get(field).getDescribe();
      // add the results to the map to be returned
      finalMap.put(field, dr); 
    }
  }
  return finalMap;
}
 
Set<String> fields = new Set<String>{'name','annualrevenue','BADFIELD'};

Map<String, Schema.DescribeFieldResult> finalMap = 
  Utils.getFieldMetaData(Account.getSObjectType().getDescribe(), fields);

// only print out the 'good' fields
for (String field : new Set<String>{'name','annualrevenue'}) {
  System.debug(finalMap.get(field).getName()); // field name
  System.debug(finalMap.get(field).getType()); // field type
  System.debug(finalMap.get(field).getLength()); // field length
}

Refer below link.

https://blog.jeffdouglas.com/2011/10/20/getting-salesforce-field-metadata-the-easy-way/
 
   Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 
 
 
 
Sitanshu TripathiSitanshu Tripathi