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
JorisJoris 

Too many Script statements OR Too many SOQL queries

Hi,

 

I've been working on a custom report tool. Basicly, it's an export in CSV format out of data from 8 objects. There is some user input needed so I have a visualforce page and a custom controller.

 

I have 1 parent object (Mutatie_Regel__c) with 4 childs (Vloeroppervlak_T_A__c, Relatie_Transactie_Aanbod__c, Prijsinfo__c, Vastgoed_Link_Object__c), 1 of those children is a link object for a many-to-many relationship to Vastgoedobject__c with 2 children (Adres__c, Relatie__c).

 

All the data in those objects will be one record in the csv.

 

I made the code in two ways, each hit a different governor limit. I need a third solution.

 

The governor limits as you all know:

- Total number of executed code statements: 200 000

- Total number of SOQL queries issued: 100

 

1) Too many script statements

 

I load every object in a collection.

 

	public void getAanbodData() {
		
		clearData();
		
		mapMr = new Map<ID, Mutatie_Regel__c>(	
				[ SELECT Id, VGM_OnlineId__c, Beschikbaar_Vloeroppervlak_Berekend__c, Datum__c, Beschikbaar__c, Soort_Huur_Koop__c, 
				  Aantal_Parkeerplaatsen__c, Type_Mutatieregel__c 
				  FROM Mutatie_Regel__c 
				  WHERE Meest_Recent_Aanbod__c = true 
				  AND LastModifiedDate >=: dummy.Looptijd_contract_van__c
				  AND LastModifiedDate <=: dummy.Looptijd_contract_tot_en_met__c 
				  LIMIT 1000 ]);
				  
		mapVota = New Map<ID, Vloeroppervlak_T_A__c>(	
				[ SELECT Type__c, Bijzonderheden__c, Aantal_Units__c, Units_vanaf_m2__c, Mutatie_Regel__c 
				  FROM Vloeroppervlak_T_A__c 
				  WHERE Mutatie_Regel__c in : mapMr.keySet() 
				  LIMIT 1000 ]);
				  
		mapRelta = New Map<ID, Relatie_Transactie_Aanbod__c>(	
				[ SELECT Bedrijfsnaam__c, Marktpartij__c, Marktpartij__r.Name, Soort_Relatie__c, Mutatie_Regel__c 
				  FROM Relatie_Transactie_Aanbod__c 
				  WHERE Mutatie_Regel__c in : mapMr.keySet() 
				  LIMIT 5000 ]);
				  
		mapPi = new Map<ID, Prijsinfo__c>(	
				[ SELECT Eenheid__c, Type__c, Verklaring_prijspeil__c, Prijs__c, Mutatie_Regel__c 
				  FROM Prijsinfo__c 
				  WHERE Mutatie_Regel__c in : mapMr.keySet() 
				  LIMIT 5000 ]);
  
		mapVglo = new Map<ID, Vastgoed_Link_Object__c>(	
				[ SELECT Vastgoedobject__c, Mutatie_Regel__c 
				  FROM Vastgoed_Link_Object__c 
				  WHERE Mutatie_Regel__c in : mapMr.keySet() 
				  LIMIT 1000 ]);
				  
		setVgoFromVglo = new Set<ID>();
		for ( Vastgoed_Link_Object__c vglo : mapVglo.values() ) {
			setVgoFromVglo.add(vglo.Vastgoedobject__c);
		}
		
		mapVgo = new Map<ID, Vastgoedobject__c>(	
				[ SELECT Id, VGM_OnlineId__c, Type_Locatie__c, Gemeentecode__c, Plaats__c, Name, Status_bouw__c, Kwartaal_Start_Bouw__c, Jaar_Start_Bouw__c, 
				  Kwartaal_Oplevering__c, Jaar_Oplevering__c, Info_Start_Bouw__c, info_start_bw_anders__c, Internetadres__c, Single_Multi_tenant__c, 
				  COROP_Gebied__c, Frontbreedte__c, Aantal_Bouwlagen__c, VGM_Regio__c, Gebouwnaam__c, Type_Vastgoedobject__c 
				  FROM Vastgoedobject__c 
				  WHERE Id in : setVgoFromVglo 
				  LIMIT 1000 ]);
				  
		mapAdres = new Map<ID, Adres__c>(	
				[ SELECT Straat__c, Huisnummer_Van__c, Postcode_Numeriek__c, Postcode_Alfa__c, Vastgoedobject__c 
				  FROM Adres__c 
				  WHERE Hoofd_Adres__c = true 
				  AND Vastgoedobject__c in : mapVgo.keySet() 
				  LIMIT 1000 ]);
				  
		mapRelvgo = new Map<ID, Relatie__c>(	
				[ SELECT Marktpartij__c, Marktpartij__r.Name, Vastgoedobject__c 
				  FROM Relatie__c 
				  WHERE Soort_Relatie__c = 'Ontwikkelaar'
				  AND Vastgoedobject__c in : mapVgo.keySet() 
				  LIMIT 1000 ]);
				  
	}

 I loop through the collections and match on the id of Mutatie_Regel__c

 

	// Loop every Mutatie_Regel__c
		
		for ( Mutatie_Regel__c mr : mapMr.values() ) {
			
			System.debug('##################### START OF LOOP #####################');
			
			// Clear variables
			
			rta_Organisatie = null;
			rta_Makelaar1 = null;
			rta_Makelaar2 = null;
			rta_Makelaar3 = null;
			rta_Makelaar4 = null;
			pi_Koopsom = null;
			pi_HuurprijsM2 = null;
			pi_HuurprijsJaar = null;
			pi_Servicekosten = null;
			pi_PrijsPerParkeerplaats = null;
			pi_PrijspeilVerklaring = null;
			
			// Get all child information
			
			// -- Vloeroppervlak TA
			vota = new Vloeroppervlak_T_A__c();
			for ( Vloeroppervlak_T_A__c tmpVota : mapVota.values() ) {
				if ( tmpVota.Mutatie_Regel__c == mr.Id ) {
					vota = tmpVota;
					break;
				}
			}
			
			System.debug('Vloeroppervlak_T_A__c.Size : ' + mapVota.size());
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());
			
			// -- Relatie TA
			for ( Relatie_Transactie_Aanbod__c tmpRelta : mapRelta.values() ) {
				if ( tmpRelta.Mutatie_Regel__c == mr.Id && tmpRelta.Bedrijfsnaam__c != null ) {
					rta_Organisatie = tmpRelta.Bedrijfsnaam__c;
					break;
				}
			}
			for ( Relatie_Transactie_Aanbod__c tmpRelta : mapRelta.values() ) {
				if ( tmpRelta.Mutatie_Regel__c == mr.Id && tmpRelta.Marktpartij__c != null && tmpRelta.Soort_Relatie__c.contains('Makelaar') ) {
					if ( rta_Makelaar1 == null ) rta_Makelaar1 = tmpRelta.Marktpartij__r.Name;else if ( rta_Makelaar2 == null ) rta_Makelaar2 = tmpRelta.Marktpartij__r.Name;else if ( rta_Makelaar3 == null ) rta_Makelaar3 = tmpRelta.Marktpartij__r.Name;else if ( rta_Makelaar4 == null ) rta_Makelaar4 = tmpRelta.Marktpartij__r.Name;if ( rta_Makelaar1 != null && rta_Makelaar2 != null && rta_Makelaar3 != null && rta_Makelaar4 != null ) break;
				}	
			}
			
			System.debug('Relatie_Transactie_Aanbod__c.Size : ' + mapRelta.size());
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());
			
			// -- Prijsinfo
			for ( Prijsinfo__c tmpPi : mapPi.values() ) {
				if ( tmpPi.Mutatie_Regel__c == mr.Id && tmpPi.Type__c == 'Huur' && tmpPi.Eenheid__c == 'Per m²' ) 
					pi_HuurprijsM2 = getCurrencyValue(tmpPi.Prijs__c);
				if ( tmpPi.Mutatie_Regel__c == mr.Id && tmpPi.Type__c == 'Huur' && tmpPi.Eenheid__c == 'Per Jaar' ) 
					pi_HuurprijsJaar = getCurrencyValue(tmpPi.Prijs__c);
				if ( tmpPi.Mutatie_Regel__c == mr.Id && tmpPi.Type__c == 'Huur' && tmpPi.Eenheid__c == 'Per Parkeerplaats' ) 
					pi_PrijsPerParkeerplaats = getCurrencyValue(tmpPi.Prijs__c);
				if ( tmpPi.Mutatie_Regel__c == mr.Id && tmpPi.Type__c == 'Koop' ) 
					pi_Koopsom = getCurrencyValue(tmpPi.Prijs__c);
				if ( tmpPi.Mutatie_Regel__c == mr.Id && tmpPi.Type__c == 'Servicekosten' ) 
					pi_Servicekosten = String.valueOf(tmpPi.Prijs__c);
				if ( tmpPi.Mutatie_Regel__c == mr.Id && tmpPi.Verklaring_prijspeil__c != null ) 
					pi_PrijspeilVerklaring = tmpPi.Verklaring_prijspeil__c;
			}
			
			System.debug('Prijsinfo__c.Size : ' + mapPi.size());
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());
			
			// -- Vastgoedobject
			vgo = new Vastgoedobject__c();
			for ( vastgoed_Link_Object__c tmpVglo : mapVglo.values() ) {
				if ( tmpVglo.Mutatie_Regel__c == mr.Id ) {
					for ( Vastgoedobject__c tmpVgo : mapVgo.values() ) {
						if ( tmpVgo.Id == tmpVglo.Vastgoedobject__c ) {
						 	vgo = tmpVgo;
							break;
						}
					}
					break;
				}
			}
			
			System.debug('Vastgoedobject__c.Size : ' + mapPi.size());
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());
			
			// -- Adres
			adres = new Adres__c();
			for ( Adres__c tmpAdres : mapAdres.values() ) {
				if ( tmpAdres.Vastgoedobject__c == vgo.Id ) {
					adres = tmpAdres;
					break;
				}
			}
			
			System.debug('Adres__c.Size : ' + mapAdres.size());
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());
			
			// -- Relatie VGO
			relvgo = new Relatie__c();
			for ( Relatie__c tmpRelvgo : mapRelvgo.values() ) {
				if ( tmpRelvgo.Vastgoedobject__c == vgo.Id ) {
					relvgo = tmpRelvgo;
					break;
				}
			}
			
			System.debug('Relatie__c.Size : ' + mapRelvgo.size());
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());

			// Add record information to output
			
			if ( vgo.VGM_OnlineId__c != null ) {
				
				addOutputCsv(vgo.VGM_OnlineId__c);
				addOutputCsv(vgo.Type_Vastgoedobject__c);
				addOutputCsv(vgo.Gemeentecode__c);
				addOutputCsv(vgo.Plaats__c);
				addOutputCsv(adres.Postcode_Numeriek__c);
				addOutputCsv(adres.Postcode_Alfa__c);
				addOutputCsv(adres.Straat__c);
				addOutputCsv(adres.Huisnummer_Van__c);
				addOutputCsv(vgo.Gebouwnaam__c);
				addOutputCsv(mr.Beschikbaar_Vloeroppervlak_Berekend__c);
				addOutputCsv(vota.Type__c);
				addOutputCsv('');
				addOutputCsv(pi_HuurprijsM2);
				addOutputCsv(pi_HuurprijsJaar);
				addOutputCsv(pi_Koopsom);
				addOutputCsv(mr.Datum__c);
				addOutputCsv(mr.Beschikbaar__c);
				addOutputCsv(mr.Type_Mutatieregel__c);
				addOutputCsv(vgo.Status_bouw__c);
				addOutputCsv(vgo.Kwartaal_Start_Bouw__c);
				addOutputCsv(vgo.Jaar_Start_Bouw__c);
				addOutputCsv(vgo.Kwartaal_Oplevering__c);
				addOutputCsv(vgo.Jaar_Oplevering__c);
				addOutputCsv(vgo.Info_Start_Bouw__c);
				addOutputCsv(vgo.info_start_bw_anders__c);
				addOutputCsv(vota.Bijzonderheden__c);
				addOutputCsv(vota.Aantal_Units__c);
				addOutputCsv(vota.Units_vanaf_m2__c);
				addOutputCsv('');
				addOutputCsv(rta_Organisatie);
				addOutputCsv(mr.Soort_Huur_Koop__c);
				addOutputCsv(pi_Servicekosten);
				addOutputCsv(mr.Aantal_Parkeerplaatsen__c);
				addOutputCsv(pi_PrijsPerParkeerplaats);
				addOutputCsv(pi_PrijspeilVerklaring);
				addOutputCsv(rta_Makelaar1);
				addOutputCsv(rta_Makelaar2);
				addOutputCsv(rta_Makelaar3);
				addOutputCsv(rta_Makelaar4);
				addOutputCsv(vgo.Internetadres__c);
				addOutputCsv(vgo.Single_Multi_tenant__c);
				addOutputCsv(vgo.COROP_Gebied__c);
				addOutputCsv(relvgo.Marktpartij__r.Name);
				addOutputCsv(vgo.Frontbreedte__c);
				addOutputCsv(vgo.Aantal_Bouwlagen__c);
				addOutputCsv(vgo.Type_Locatie__c);
				addOutputCsv(vgo.VGM_Regio__c);	
				addNewLine();
				
			}
			
			System.debug('Output created');
			System.debug('Limits.getScriptStatements : ' + Limits.getScriptStatements());
			
			System.debug('##################### END OF LOOP #####################');
			
		}

 Because each collection contains all the data. Alot of looping is done and alot of script statements are used that are not needed.

 

2) Too many SOQL queries

 

I figured I should limit the collections to only contain the data of that particular parent record. The problem with this solution is that if I put the queries of the child objects during the creation of the output and inside the loop of the parent object, I'll easily hit the too many soql query limit.

 

I can solve each limit by going to the other solution ...

 

I need another way out.

 

Can anyone point me into the right direction ?

 

Thanks in advance,

 

Joris