What I have to do is to get all fields of an sObject and converting it into xml runtime. I have all the fields. And I get field name and field label in xml tag. What I want is field value instead of label. I search a lot but couldn't find any good help.Here is my code.

public class XmlBuilderC{
    String strAcId = '001280000033UsO';
    String objNameAccount = 'Account';
    String strContactId = '00328000001iIvs';
    String objNameContact = 'Contact';
    String xml;
    XmlStreamWriter w = new XmlStreamWriter();
    sObject sObjectAccount = [select id, name from account where id=:strAcId];
    SObject sObjectContact = [select id from contact where id=:strContactId];
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

    //sObject objsObjectAccount = objAccount;

    public XmlBuilderC(){                   

    public void printXml(String sObjectName){
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(sObjectName).getDescribe().fields.getMap();
        w.writeStartDocument(null,'<soap:Envelope xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">');
        for(Schema.SObjectField sfield : fieldMap.Values())
            schema.describefieldresult dfield = sfield.getDescribe();                        
            System.debug('dfield.getLocalName(): '+dfield.getLocalName());
            w.writeCharacters(dfield.getLabel()); //***Here.***

        System.debug('End of printXml()');

    public String getXml(){
        System.debug('inside getXml');
        xml = w.getXmlString();
        System.debug('xml: '+w.getXmlString());
        System.debug('XmlStreamWriter closed');
        return xml;

To get the values, you will need to perform a dynamic SOQL query where you make sure that you include all the fields in the query by building the SOQL string from your describe data. You can then use SObject.get(SObjectField) for each of the SObjectField values on the query result.

Here is an example of dynamic SOQL that queries all the fields:

Id id = '001i000000UBpmI';
DescribeSObjectResult d = Account.SObjectType.getDescribe();
List<String> fields = new List<String>(d.fields.getMap().keySet());
String soql = 'select ' + String.join(fields, ', ') + ' from Account where Id = :id';
SObject sob = Database.query(soql);
  • Hi, I am new in Salesforce so please allow me to ask a dumb question. Is there any way we can cast describefieldresult into sObject? – Jigar Trivedi Jun 8 '15 at 7:38
  • No. The describe call returns information about the structure of an SObject (table); it does not return data from any row of the SObject. To look at data from a row you need to tie down which row by e.g. the "where" clause of SOQL. – Keith C Jun 8 '15 at 7:39
  • You saved me. :) – Jigar Trivedi Jun 8 '15 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.