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
rajashekar reddy 18rajashekar reddy 18 

getting error too many soql queries

here i am generating xml file.proviously it was working fine now i am getting error too many soql queries.
here i am generating XML file.it was working fine till now. but now iam facing this exception.
my code is

public class OutBoundListingData {
    
    public static string genratedXML{get;set;}
    
    public OutBoundListingData()
    {
        GenerateListingXML();
    }
    
    public static List<pba__Listing__c> getListingRecords()
    {
        List<pba__Listing__c> objListListing=[select Id,ListingId__c,pba__Property__c,Common_Charges__c,pba__Address_pb__c,
                                              of_Units__c,pba__City_pb__c,pba__State_pb__c,pba__PostalCode_pb__c,
                                              pba__Latitude_pb__c,pba__Longitude_pb__c,pba__Country_pb__c
                                              ,Street_Intersection__c,Display_Address__c,pba__Status__c,
                                              Listing_Status__c,pba__ListingType__c,pba__ListingPrice_pb__c,
                                              pba__Listing_Website__c,Listed_Date__c,Listing_Email__c,Move_in_Date__c,
                                              HasGarden__c,HasBackyard__c,HasBalcony__c,HasTerrace__c,HasPrivateRoofDeck__c,
                                              HasPrivateCourtyard__c,HasStorageSpace__c,HasGarage__c,HasRoofGarden__c,
                                              HasHealthClub__c,HasCourtyard__c,UtilitiesIncluded__c,HasWasherDryer__c,
                                              Pets_Allowed__c,Commission__c,OPPaidToCobroker__c,Lease_Term_Type__c,
                                              Lease_Type__c,Move_In_Fee__c,Move_Out_Fee__c,Pet_Security_Amount__c,
                                              Security_Deposit__c,Fee_Type__c,Name,pba__Description_pb__c,pba__Bedrooms_pb__c,
                                              pba__FullBathrooms_pb__c,Rooms__c,pba__LotSize_pb__c,pba__YearBuilt_pb__c,
                                              Prepost__c,pba__PropertyType__c,Building_Description__c,Condition__c,
                                              Exposures__c,Showing_Instructions__c,View__c,NewDevelopment__c,
                                              Construction_Era__c,Building_Type__c,Lobby_Attendance__c,Block__c,
                                              Management_Company__c,pba__Listing_Agent_Firstname__c,
                                              pba__Listing_Agent_Lastname__c,pba__Listing_Agent_Email__c,
                                              pba__Listing_Agent_Photo__c,pba__Listing_Agent_Mobil_Phone__c,
                                              pba__Listing_Agent_Phone__c,Listing_Agent_Brokerage_Name__c,
                                              pba__Listing_Agent_Street__c,pba__Listing_Agent_City__c,pba__Listing_Agent_Zip__c,
                                              Neighborhood__c,Neighborhood_Description__c,Appliances__c,Cooling_Systems__c,
                                              Elevator__c,Exterior_Types__c,Floor_Coverings__c,of_Floors__c,NewConstruction__c ,
                                              Listing_Secondary_Agent_Firstname__c,Listing_Secondary_Agent_Lastname__c,
                                              Listing_Secondary_Agent_Email__c,Listing_Secondary_Agent_Photo__c,
                                              Listing_Secondary_Agent_Mobil_Phone__c,Listing_Secondary_Agent_Phone__c,
                                              Listing_Secondary_Agent_Brokerage_Name__c,Listing_Secondary_Agent_Street__c,
                                              Listing_Secondary_Agent_City__c,Listing_Secondary_Agent_Zip__c ,
                                              Floorplan_URL__c,Listing_URL__c from pba__Listing__c where RLSListingKey__c=null];
        return objListListing;
    }
    public static void GenerateListingXML()
    {
        XmlStreamWriter w =new XmlStreamWriter();  
        w.writeStartElement(null,'Listings',null);            
            for(pba__Listing__c Item : getListingRecords()){               
                w.writeStartElement(null,'Listing',null);               
                w.writeStartElement(null, 'Type', null);
                w.writeCharacters(Item.pba__ListingType__c);  
                w.writeEndElement();
                w.writeStartElement(null, 'WebID', null);
                w.writeCharacters(Item.Id);
                w.writeEndElement();
                w.writeStartElement(null,'ListingId', null);
                w.writeCharacters(Item.ListingId__c);
                w.writeEndElement();
                w.writeStartElement(null, 'CommonCharges', null);
                if(Item.Common_Charges__c!=null)
                    w.writeCharacters(String.valueOf(Item.Common_Charges__c));  
                w.writeEndElement();              
                w.writeStartElement(null, 'Floorplans', null);               
                w.writeStartElement(null, 'Floorplan', null);
                w.writeStartElement(null, 'PictureUrl', null);
                if(Item.Floorplan_URL__c!=null)
                    w.writeCharacters(String.valueOf(Item.Floorplan_URL__c));  
                w.writeEndElement();   
                w.writeEndElement();                 
                w.writeEndElement();                 
                w.writeStartElement(null, 'Commission', null);
                if(Item.Commission__c!=null)
                    w.writeCharacters(String.valueOf(Item.Commission__c));  
                w.writeEndElement();              
                w.writeStartElement(null, 'UnitNumber', null);
                if(Item.of_Units__c!=null)
                    w.writeCharacters(String.valueOf(Item.of_Units__c));  
                w.writeEndElement();    
                w.writeStartElement(null, 'City', null);
                if(Item.pba__City_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__City_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'State', null);
                if(Item.pba__State_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__State_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Zip', null);
                if(Item.pba__PostalCode_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__PostalCode_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Lat', null);
                if(Item.pba__Latitude_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Latitude_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Long', null);
                if(Item.pba__Longitude_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Longitude_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'County', null);
                if(Item.pba__Country_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Country_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'StreetIntersection', null);
                if(Item.Street_Intersection__c!=null)
                    w.writeCharacters(String.valueOf(Item.Street_Intersection__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'DisplayAddress', null);
                if(Item.Display_Address__c!=null)
                    w.writeCharacters(String.valueOf(Item.Display_Address__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Neighborhood', null);
                if(Item.Neighborhood__c!=null)
                    w.writeCharacters(String.valueOf(Item.Neighborhood__c));  
                w.writeEndElement();               
                w.writeEndElement();
                w.writeStartElement(null, 'ListingDetails', null);               
                w.writeStartElement(null, 'Status', null);
                if(Item.pba__Status__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Status__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ListingStatus', null);
                if(Item.pba__Status__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Status__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ListingType', null);
                if(Item.pba__ListingType__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__ListingType__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Price', null);
                if(Item.pba__ListingPrice_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__ListingPrice_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ListingUrl', null);
                if(Item.pba__Listing_Website__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Listing_Website__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'DateListed', null);
                if(Item.Listed_Date__c!=null)
                    w.writeCharacters(String.valueOf(Item.Listed_Date__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ListingEmail', null);
                if(Item.Listing_Email__c!=null)
                    w.writeCharacters(String.valueOf(Item.Listing_Email__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'DateAvailable', null);
                if(Item.Move_in_Date__c!=null)
                    w.writeCharacters(String.valueOf(Item.Move_in_Date__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasGarden', null);
                if(Item.HasGarden__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasGarden__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasBackyard', null);
                if(Item.HasBackyard__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasBackyard__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasBalcony', null);
                if(Item.HasBalcony__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasBalcony__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasTerrace', null);
                if(Item.HasTerrace__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasTerrace__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasPrivateRoofDeck', null);
                if(Item.HasPrivateRoofDeck__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasPrivateRoofDeck__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasPrivateCourtyard', null);
                if(Item.HasPrivateCourtyard__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasPrivateCourtyard__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasStorageSpace', null);
                if(Item.HasStorageSpace__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasStorageSpace__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasGarage', null);
                if(Item.HasGarage__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasGarage__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasRoofGarden', null);
                if(Item.HasRoofGarden__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasRoofGarden__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasHealthClub', null);
                if(Item.HasHealthClub__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasHealthClub__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasCourtyard', null);
                if(Item.HasCourtyard__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasCourtyard__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'UtilitiesIncluded', null);
                if(Item.UtilitiesIncluded__c!=null)
                    w.writeCharacters(String.valueOf(Item.UtilitiesIncluded__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'HasWasherDryer', null);
                if(Item.HasWasherDryer__c!=null)
                    w.writeCharacters(String.valueOf(Item.HasWasherDryer__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'AllowsPets', null);
                if(Item.Pets_Allowed__c!=null)
                    w.writeCharacters(String.valueOf(Item.Pets_Allowed__c));  
                w.writeEndElement();                                 
                w.writeStartElement(null, 'PetSecurityAmount', null);
                if(Item.Pet_Security_Amount__c!=null)
                    w.writeCharacters(String.valueOf(Item.Pet_Security_Amount__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'SecurityDeposit', null);
                if(Item.Security_Deposit__c!=null)
                    w.writeCharacters(String.valueOf(Item.Security_Deposit__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'FeeType', null);
                if(Item.Fee_Type__c!=null)
                    w.writeCharacters(String.valueOf(Item.Fee_Type__c));  
                w.writeEndElement();                
                w.writeEndElement();
                w.writeStartElement(null, 'BasicDetails', null);                
                w.writeStartElement(null, 'Title', null);
                if(Item.Name!=null)
                    w.writeCharacters(String.valueOf(Item.Name));  
                w.writeEndElement();
                w.writeStartElement(null, 'Description', null);
                if(Item.pba__Description_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Description_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Bedrooms', null);
                if(Item.pba__Bedrooms_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__Bedrooms_pb__c));  
                w.writeEndElement();                  
                w.writeStartElement(null, 'Prepost', null);
                if(Item.Prepost__c!=null)
                    w.writeCharacters(String.valueOf(Item.Prepost__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'PropertyType', null);
                if(Item.pba__PropertyType__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__PropertyType__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'BuildingType', null);
                if(Item.Building_Type__c!=null)
                    w.writeCharacters(String.valueOf(Item.Building_Type__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'BuildingDescription', null);
                if(Item.Building_Description__c!=null)
                    w.writeCharacters(String.valueOf(Item.Building_Description__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Condition', null);
                if(Item.Condition__c!=null)
                    w.writeCharacters(String.valueOf(Item.Condition__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Exposures', null);
                if(Item.Exposures__c!=null)
                    w.writeCharacters(String.valueOf(Item.Exposures__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ShowingInstructions', null);
                if(Item.Showing_Instructions__c!=null)
                    w.writeCharacters(String.valueOf(Item.Showing_Instructions__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Views', null);
                if(Item.View__c!=null)
                    w.writeCharacters(String.valueOf(Item.View__c));  
                w.writeEndElement();                          
                w.writeEndElement();            
                w.writeStartElement(null, 'Building', null);               
                w.writeStartElement(null, 'NewDevelopment', null);
                if(Item.NewDevelopment__c!=null)
                    w.writeCharacters(String.valueOf(Item.NewDevelopment__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ConstructionEra', null);
                if(Item.Construction_Era__c!=null)
                    w.writeCharacters(String.valueOf(Item.Construction_Era__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'BuildingType', null);
                if(Item.Building_Type__c!=null)
                    w.writeCharacters(String.valueOf(Item.Building_Type__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'LobbyAttendance', null);
                if(Item.Lobby_Attendance__c!=null)
                    w.writeCharacters(String.valueOf(Item.Lobby_Attendance__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Block', null);
                if(Item.Block__c!=null)
                    w.writeCharacters(String.valueOf(Item.Block__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'Lot', null);
                if(Item.pba__LotSize_pb__c!=null)
                    w.writeCharacters(String.valueOf(Item.pba__LotSize_pb__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'ManagementCompany', null);
                if(Item.Management_Company__c!=null)
                    w.writeCharacters(String.valueOf(Item.Management_Company__c));  
                w.writeEndElement();               
                w.writeEndElement();             
                w.writeStartElement(null, 'Pictures', null);
                List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ];               
                for(pba__PropertyMedia__c objImages: ListImages)
                {
                    w.writeStartElement(null, 'Picture', null);                   
                    w.writeStartElement(null, 'PictureUrl', null);  
                    if(objImages.pba__URL__c != null)
                        w.writeCharacters(String.valueOf(objImages.pba__URL__c));  
                    w.writeEndElement();                    
                    w.writeEndElement();
                }
                w.writeEndElement();            
                w.writeStartElement(null, 'Videos', null);
                w.writeEndElement();
                w.writeStartElement(null, 'SecondaryAgent', null);               
                w.writeStartElement(null, 'FirstName', null);
                if(Item.Listing_Secondary_Agent_Firstname__c !=null)
                    w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Firstname__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'LastName', null);
                if(Item.Listing_Secondary_Agent_Lastname__c!=null)
                    w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Lastname__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'EmailAddress', null);
                if(Item.Listing_Secondary_Agent_Email__c!=null)
                    w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Email__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'PictureUrl', null);
                if(Item.Listing_Secondary_Agent_Photo__c!=null)
                    w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Photo__c));  
                w.writeEndElement();
                w.writeStartElement(null, 'MobilePhoneLineNumber', null);
                if(Item.Listing_Secondary_Agent_Mobil_Phone__c!=null)
                    w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Mobil_Phone__c));  
                w.writeEndElement();
               
              
               
          w.writeEndElement();
        string xml = w.getXmlString();
        w.close();
        genratedXML='<?xml version="1.0"?>'+xml;
    }
}

 
Best Answer chosen by rajashekar reddy 18
Agustina GarciaAgustina Garcia
Let me try to explain. We are going to use several collections in order to iterate over them instead of having to add queries. Imagine this scenario:

I have Numbers, Leters and Colors. Every Number is related to a single Leter and every Leter can have a list of Colors (It is like your case). So I will create a first map with this info:

1,A
2,B
3,C

Then I will have another structure 

A, {Blue, Red}
B, {Black, Grey}
V, {Green}

Your first loop itereate ofer Numbers, and your second over colors so it would be something like
for(Numbers theNumber : theNumbers)
{
   //Get the Letter related to this number
   //for instance, theNumber is 1, then I get A
   //If I'm working with A and now I need to iterate over colours, 
   //below colourList is Blue and Red
   for(Colors theColor : colourList)
   {
      //your code
   }
}
Does it make sense?

Find here your code with the new maps. It is a new method so you can copy-paste y compare till you understand it. I also added comments. Please, take into account I did not try to compile as I do not have your objects and fields, so maybe you get some compilation issues. Just fix them.
 
public static void GenerateListingXML2()
    {
        //Create a map with pba_Listing__c Id and its record pba__Property__c
        //I'm guessing pba__Property__c field type is Text if it is Number, just change it
        //to this Map<Id, Decimal> idPropertyMap = new Map<Id, Decimal>();
        
        Map<Id, String> idPropertyMap = new Map<Id, String>();
        for(pba__Listing__c Item : getListingRecords())
        {
            idPropertyMap.put(Item.Id, Item.pba__Property__c)
        }

        //Take out of the loop this query
        //List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ]; 
        
        
        //This query will bring all pba__PropertyMedia__c records
        List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c IN :idPropertyMap.values()];

        //Create a Map with the pba__PropertyMedia__c.pba__Property__c field value and its list
        //Similar as before, I guess the field type is Text, otherwise change it
        
        List<pba__PropertyMedia__c> imagesList;
        Map<String, List<pba__PropertyMedia__c>> propertyAndImagesMap = new Map<String,List<pba__PropertyMedia__c>>();
        for(pba__PropertyMedia__c theImage : ListImages)
        {
            String theProperty = theImage.pba__Property__c;

            //Check if the value is already in the map
            if(propertyAndImagesMap.containsKey(theProperty))
            {
                imagesList = propertyAndImagesMap.get(theProperty);
            }
            else //There is no Image list yet, so I initialize the list
            {
                imagesList = new List<pba__PropertyMedia__c>();
            }

            //Add the new element and the list to the map
            imagesList.add(theImage);
            propertyAndImagesMap.put(theProperty,imagesList);
        }

        //Now I have all structures to iterate over them instead of doing queries inside a loop
        //So back to your code

        XmlStreamWriter w =new XmlStreamWriter();  
        w.writeStartElement(null,'Listings',null);            
        for(pba__Listing__c Item : getListingRecords())
        {
            w.writeStartElement(null,'Listing',null);               
            w.writeStartElement(null, 'Type', null);
            w.writeCharacters(Item.pba__ListingType__c);  
            w.writeEndElement();
            w.writeStartElement(null, 'WebID', null);
            w.writeCharacters(Item.Id);
            w.writeEndElement();
            w.writeStartElement(null,'ListingId', null);
            w.writeCharacters(Item.ListingId__c);
            w.writeEndElement();
            w.writeStartElement(null, 'CommonCharges', null);
            if(Item.Common_Charges__c!=null)
                w.writeCharacters(String.valueOf(Item.Common_Charges__c));  
            w.writeEndElement(); 
            
            //Add rest of your code till for loop

            /* This is your query and I will not use it. I will use my map structure
            but leave your query here so you would not get lost
            List<pba__PropertyMedia__c> ListImages = [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ];*/

            //Look for the Property that I have to chose using the first map I created
            String thePropertyINeed = idPropertyMap.get(Item.pba__Property__c);

            //Look for the list of Images based on this property using the second map
            List<pba__PropertyMedia__c> ListImagesToIterate = propertyAndImagesMap.get(thePropertyINeed);

            //Iterate over the above list instead of this one that comes from the query
            //for(pba__PropertyMedia__c objImages: ListImages)
            for(pba__PropertyMedia__c objImages: ListImagesToIterate)
            {
                w.writeStartElement(null, 'Picture', null);                   
                w.writeStartElement(null, 'PictureUrl', null);  
                if(objImages.pba__URL__c != null)
                    w.writeCharacters(String.valueOf(objImages.pba__URL__c));  
                w.writeEndElement();                    
                w.writeEndElement();
            }

            w.writeEndElement();            
            w.writeStartElement(null, 'Videos', null);
            w.writeEndElement();
            w.writeStartElement(null, 'SecondaryAgent', null);               
            w.writeStartElement(null, 'FirstName', null);
            if(Item.Listing_Secondary_Agent_Firstname__c !=null)
               w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Firstname__c)); 

            //Add rest of your code till the end
        }
    }
Hope this help
Agustins

 

All Answers

Agustina GarciaAgustina Garcia
HI,

You are adding a SOQL inside of a loop so probably the number of elements in your getListingRecords methods is now more than 100 and you achieve the maximun size governor limit (101)

In your above code, line 297, you do this
 
List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ];

Try to dho this at the beggining of your code:
public static void GenerateListingXML()
{

    //Guess pba__Property__c is a String. If not, change the map
    Map<Id, String> myMap = new Map<Id, String>();
    for(pba__Listing__c Item : getListingRecords())
    {
        myMap.put(Item.Id, Item.pba__Property__c);
    }

    List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c, pba__Property__c from pba__PropertyMedia__c where pba__Property__c IN myMap.values()];

    Map<String, pba__PropertyMedia__c> myMap2 = new Map<String, pba__PropertyMedia__c>();
    for(pba__PropertyMedia__c ele : ListImages)
    {
        myMap2.put(ele.pba__Property__c , ele);
    }

    XmlStreamWriter w =new XmlStreamWriter();
    w.writeStartElement(null,'Listings',null);           
    
    for(pba__Listing__c Item : getListingRecords())
    {
        //your code

        /* Instead of this
        for(pba__PropertyMedia__c objImages: ListImages)
        {
            //your code
        }
        Do this */

        pba__PropertyMedia__c theImage = myMap2.get(Item.pba__Property__c);

        //your code
    }
}
As I don't have your fields, I wsa not able to compile, so maybe there is something that is wrong in the code. Just double check.

As another advice, don't post such a big code. People would take a look and after scrolling down twice they would close the question. Try to make it a bit easier and expand in the case someone ask you.

Hope this helps
Agustina
 
Tugce SirinTugce Sirin
Problem is on this line:
 
List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ];

You should never write an soql query inside a for loop. (or any kind of loop) What you need to to is query all pba__PropertyMedia__c before for then create a map that will hold pba_Property__c as key and records as value. So you can say propertyMediaMap.get(Item.pba__Property__c); instead of running an soql query.
rajashekar reddy 18rajashekar reddy 18
Hi Agustina,

                     here each property has several images.but here each property is displaying only single image.
 
Agustina GarciaAgustina Garcia
Yes, I was expecting something like this. Try to create 2 maps and use them in the code in order to avoid doing a SOQL inside of a loop

First map: Map<Id, ImagesObject> --> key is the Image Id and the value the Image record itself
Second Map: Map<Id, Id> --> Key is the Property Id and value the Image that display Id (I guess this value comes from Property record)

Then, if you do something like:
 
Map<Id,Id> propertyAndDisplayImage = new Map<Id,Id>(); //Populate somehow
Map<Id, SObject> imageIdMap = new Map<Id, SObject>();

SObject imageDisplayed;
for(Id propId : propertyAndDisplayImage.keySet())
{
   Id displayImageId = propertyAndDisplayImage.get(propId);
   imageDisplayed = imageIdMap.get(displayImageId);
   
   //Now you can use your image to work with it.
}


 
rajashekar reddy 18rajashekar reddy 18
Hi Agustina,

                  can you explain briefly i am not able to use this concept. i am getting lot of error,should i use two concepts at a time or should i use only this concept.can you explain with the coding.

  Thanks  
Agustina GarciaAgustina Garcia
Let me try to explain. We are going to use several collections in order to iterate over them instead of having to add queries. Imagine this scenario:

I have Numbers, Leters and Colors. Every Number is related to a single Leter and every Leter can have a list of Colors (It is like your case). So I will create a first map with this info:

1,A
2,B
3,C

Then I will have another structure 

A, {Blue, Red}
B, {Black, Grey}
V, {Green}

Your first loop itereate ofer Numbers, and your second over colors so it would be something like
for(Numbers theNumber : theNumbers)
{
   //Get the Letter related to this number
   //for instance, theNumber is 1, then I get A
   //If I'm working with A and now I need to iterate over colours, 
   //below colourList is Blue and Red
   for(Colors theColor : colourList)
   {
      //your code
   }
}
Does it make sense?

Find here your code with the new maps. It is a new method so you can copy-paste y compare till you understand it. I also added comments. Please, take into account I did not try to compile as I do not have your objects and fields, so maybe you get some compilation issues. Just fix them.
 
public static void GenerateListingXML2()
    {
        //Create a map with pba_Listing__c Id and its record pba__Property__c
        //I'm guessing pba__Property__c field type is Text if it is Number, just change it
        //to this Map<Id, Decimal> idPropertyMap = new Map<Id, Decimal>();
        
        Map<Id, String> idPropertyMap = new Map<Id, String>();
        for(pba__Listing__c Item : getListingRecords())
        {
            idPropertyMap.put(Item.Id, Item.pba__Property__c)
        }

        //Take out of the loop this query
        //List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ]; 
        
        
        //This query will bring all pba__PropertyMedia__c records
        List<pba__PropertyMedia__c> ListImages= [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c IN :idPropertyMap.values()];

        //Create a Map with the pba__PropertyMedia__c.pba__Property__c field value and its list
        //Similar as before, I guess the field type is Text, otherwise change it
        
        List<pba__PropertyMedia__c> imagesList;
        Map<String, List<pba__PropertyMedia__c>> propertyAndImagesMap = new Map<String,List<pba__PropertyMedia__c>>();
        for(pba__PropertyMedia__c theImage : ListImages)
        {
            String theProperty = theImage.pba__Property__c;

            //Check if the value is already in the map
            if(propertyAndImagesMap.containsKey(theProperty))
            {
                imagesList = propertyAndImagesMap.get(theProperty);
            }
            else //There is no Image list yet, so I initialize the list
            {
                imagesList = new List<pba__PropertyMedia__c>();
            }

            //Add the new element and the list to the map
            imagesList.add(theImage);
            propertyAndImagesMap.put(theProperty,imagesList);
        }

        //Now I have all structures to iterate over them instead of doing queries inside a loop
        //So back to your code

        XmlStreamWriter w =new XmlStreamWriter();  
        w.writeStartElement(null,'Listings',null);            
        for(pba__Listing__c Item : getListingRecords())
        {
            w.writeStartElement(null,'Listing',null);               
            w.writeStartElement(null, 'Type', null);
            w.writeCharacters(Item.pba__ListingType__c);  
            w.writeEndElement();
            w.writeStartElement(null, 'WebID', null);
            w.writeCharacters(Item.Id);
            w.writeEndElement();
            w.writeStartElement(null,'ListingId', null);
            w.writeCharacters(Item.ListingId__c);
            w.writeEndElement();
            w.writeStartElement(null, 'CommonCharges', null);
            if(Item.Common_Charges__c!=null)
                w.writeCharacters(String.valueOf(Item.Common_Charges__c));  
            w.writeEndElement(); 
            
            //Add rest of your code till for loop

            /* This is your query and I will not use it. I will use my map structure
            but leave your query here so you would not get lost
            List<pba__PropertyMedia__c> ListImages = [select id,pba__URL__c from pba__PropertyMedia__c where pba__Property__c=:Item.pba__Property__c ];*/

            //Look for the Property that I have to chose using the first map I created
            String thePropertyINeed = idPropertyMap.get(Item.pba__Property__c);

            //Look for the list of Images based on this property using the second map
            List<pba__PropertyMedia__c> ListImagesToIterate = propertyAndImagesMap.get(thePropertyINeed);

            //Iterate over the above list instead of this one that comes from the query
            //for(pba__PropertyMedia__c objImages: ListImages)
            for(pba__PropertyMedia__c objImages: ListImagesToIterate)
            {
                w.writeStartElement(null, 'Picture', null);                   
                w.writeStartElement(null, 'PictureUrl', null);  
                if(objImages.pba__URL__c != null)
                    w.writeCharacters(String.valueOf(objImages.pba__URL__c));  
                w.writeEndElement();                    
                w.writeEndElement();
            }

            w.writeEndElement();            
            w.writeStartElement(null, 'Videos', null);
            w.writeEndElement();
            w.writeStartElement(null, 'SecondaryAgent', null);               
            w.writeStartElement(null, 'FirstName', null);
            if(Item.Listing_Secondary_Agent_Firstname__c !=null)
               w.writeCharacters(String.valueOf(Item.Listing_Secondary_Agent_Firstname__c)); 

            //Add rest of your code till the end
        }
    }
Hope this help
Agustins

 
This was selected as the best answer