1

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(){                   
        printXml(objNameAccount); 
        printXml(objNameContact);                       
    }

    public void printXml(String sObjectName){
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(sObjectName).getDescribe().fields.getMap();
        w.writeStartDocument('utf-8','1.0');
        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.writeStartElement(null,sObjectName,null);
            w.writeStartElement(sObjectName,dfield.getname(),sObjectName);
            w.writeCharacters(dfield.getLabel()); //***Here.***
            w.writeEndElement();
            w.writeEndDocument();
        }

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

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

1 Answer 1

1

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);
System.debug(sob.get(Account.Name));
...
3
  • 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? Jun 8, 2015 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, 2015 at 7:39
  • You saved me. :) Jun 8, 2015 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .