function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prerak desaiprerak desai 

I want that Bold Italic text out of the loop b'coz , I've manipulate data above 1000 so it may cross gov limit.

Public void getXMLFromList(){
        List<Map<String, Object>> lstMapField = new List<Map<String, Object>>();
        List<String> lstsObjectName = new List<String>();
        lstsObject = [Select Id, Name From Account Limit 5];     
        String accountsJSON = JSON.serialize(lstsObject);
        System.debug('accountsJSON ::'+accountsJSON);
        Map<String,Schema.SObjectType> gd = Schema.getGlobalDescribe();     
        if(lstsObject != null && lstsObject.size() > 0){
            for(sObject objA : lstsObject){
                String obj = objA.getSObjectType().getDescribe().getName();                
                Schema.SObjectType sobjType = gd.get(obj);
                Schema.DescribeSObjectResult describeResult = sobjType.getDescribe();
                Map<String,Schema.SObjectField> fieldsMap = describeResult.fields.getMap();  

                System.debug('fieldsMap.keySet() ::'+fieldsMap.keySet());       
                Map<String, Object> queriedFieldValues = new Map<String, Object>();        
                for (String fieldName: fieldsMap.keySet()) {
                    System.debug('fieldName og'+fieldName);
                    try {                             
                        queriedFieldValues.put(fieldName, objA.get(fieldName));
                    } catch (SObjectException e){
                        // Intentional capture
                        queriedFieldValues.put(fieldName,'');                         
                    }
                }        
                lstMapField.add(queriedFieldValues);
                lstsObjectName.add(obj);
                System.debug('lstMapField ::'+lstMapField);         
            }
        }        
        strXML = '';          
        attachment = new Attachment();
        XmlStreamWriter w = new XmlStreamWriter();        
        w.writeStartDocument(null, '1.0');
        if(lstMapField != null && lstMapField.size() > 0){
            Integer  i = 0;            
            for(Map<String, Object> mapField : lstMapField){
                w.writeStartElement(null, lstsObjectName[i], null);           
                for(String strField : mapField.keySet()){
                    w.writeStartElement(null, strField , null);
                    w.writeCharacters(String.valueOf(mapField.get(strField)));
                    w.writeEndElement();                     
                }
                w.writeEndElement();                          
                i++;
            }
        }
        w.writeEndDocument();            
        String xmlOutput = w.getXmlString();
        system.debug('xmlOutput --> '+ w.getXmlString());
        strXML = w.getXmlString();
       // strXML = accountsJSON;
        w.close();           
Andy BoettcherAndy Boettcher
How about you spin through your XML and fill a Set<String> with the object names, then loop through that with the getDescribe to get the unique getDescribes for those objects into a Map or other collection.  Once you have that then you can just reference that collection in your main loop.