Getting the metadata of columns from SOQL Query

2.9k views Asked by At

I have a simple SOQL query in java for extracting Salesforce standard object as follows -

String soqlQuery = "SELECT FirstName, LastName FROM Contact";

QueryResult qr = connection.query(soqlQuery);

I want to get the datatype of the object fields.

2

There are 2 answers

0
Sarath Subramanian On

Say that we have the below soql.

select FirstName,LastName from Contact limit 2

The query result in the QueryResult object looks like below.

{
    [2]XmlObject
    {
        name={urn:partner.soap.sforce.com}records, value=null, children=
        [
            XmlObject{name={urn:sobject.partner.soap.sforce.com}type, value=Contact, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}Id, value=null, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}FirstName, value=Bill, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}LastName, value=Gates, children=[]}
        ]
    },
    XmlObject
    {
        name={urn:partner.soap.sforce.com}records, value=null, children=
        [
            XmlObject{name={urn:sobject.partner.soap.sforce.com}type, value=Contact, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}Id, value=null, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}FirstName, value=Alan, children=[]}, 
            XmlObject{name={urn:sobject.partner.soap.sforce.com}LastName, value=Donald, children=[]}
        ]
    },
}

In order to parse the QueryResult and to take column names, I have implemented the below method that will return the column names in comma separated String. I have mentioned the logic inside the code.

public String getColumnNames(QueryResult soqlResponse)
{       
    String columns = ""
    try
    {           
        // We are looping inorder to pick the 1st record from the QueryResult
        for (SObject record : soqlResponse.getRecords()) 
        {               
            Iterator<XmlObject> xmlList =  record.getChildren();
            int counterXml = 0;
            while(xmlList.hasNext())
            {
                XmlObject xObj = xmlList.next();
                // Since the 1st 2 nodes contains metadata of some other information, we are starting from the 3rd node only
                if(counterXml > 1)
                {                               
                    columns += xObj.getName().getLocalPart() + ",";                 
                }
                counterXml++;
            }
            // Since we can get the column names from the 1st record, we are breaking the loop after the data of 1st record is read
            break;
        }
        // We are removing the last comma in the string
        columns = columns.substring(0, columns.length() - 1);
    }
    catch(Exception ex)
    {
        
    }   
    return columns;
}
1
Ajay Dubedi On

I have written a small function below which will provide the list of Phone fields and its label present in a Custom or Standard Object of your Salesforce ORG. I hope this might help you in writing the business logic for your code.

 public list<String> getFieldsForSelectedObject(){    
   selectedPhoneNumber = ''; //to reset home number field    
   list<String> fieldsName = new list<String>(); 
   selectedObject = 'Object Name' // This should have the object name for which we want to get the fields type
   schemaMap = Schema.getGlobalDescribe(); //Populating the schema map
   try{
       if(selectedObject != null || selectedObject != '' || selectedObject != '--Select Object--'){             
            Map<String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();        
            for(Schema.SObjectField sfield : fieldMap.Values()){
                schema.describefieldresult dfield = sfield.getDescribe();
                schema.Displaytype disfield= dfield.getType();
                system.debug('#######'  + dfield );       
                if(dfield.getType() == Schema.displayType.Phone){// Over here I am trying to findout all the PHONE Type fields in the object(Both Custom/Standard) 
                    fieldsName.add('Name:'+dfield.getName() +'  Label:'+ dfield.getLabel ());
                }   
            }
        }
    }catch(Exception ex){          
        apexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'There is no Phone or Fax Field Exist for selected Object!'));
    }           
    return fieldsName;
}   

Sample OUTPUT List of String::

Name: Home_Phone__c Label: Home Phone Name: Office_Phone__c Label: Office Phone