I have a visualforce page that uses a junction object to display child assets. The page functions as expected, however when more than 100 records are returned, the VF Page breaks with the dreaded error: "System.LimitException: Too Many SQOL Queries: 101."
How can I refactor the Controller so that the following code is properly bulkified?
public class ViewAssetsController {
ManageAssetController am = new ManageAssetController();
Id accid = am.accountId;
//Wrapper Class
public class AssetWrap{
public List<Child_Assets__c> Child {get; set;}
public Asset Parent {get;set;}
public AssetWrap(Asset relParent, List<Child_Assets__c> relChildList){
Child = relChildList;
Parent = relParent;
}
}
public Account currentAcc {get; set;}
public ViewAssetsController(ApexPages.StandardController controller) {
currentAcc = (Account)controller.getRecord();
}
public List<AssetWrap> AssetTree;
public List<AssetWrap> GetNodes(){
AssetTree = new List<AssetWrap>();
List<Asset> relParentList = [SELECT Id, Name, AccountId, Asset_Number__c, Children__c, Type__c, Status,
Quantity, Start_Date__c, End_Date__c
FROM Asset WHERE AccountId =: currentAcc.Id AND Children__c > 0];
for (Integer i = 0; i < relParentList.size(); i++){
List<Child_Assets__c> relChildList = [SELECT Parent_Asset__c, Child_Asset__c, Child_Asset__r.Name,
Child_Asset__r.Type__c, Child_Asset__r.Status, Child_Asset__r.Quantity,
Child_Asset__r.Start_Date__c, Child_Asset__r.End_Date__c
FROM Child_Assets__c WHERE Parent_Asset__c =: relParentList[i].Id];
AssetTree.add(new AssetWrap(relParentList[i], relChildList));
}
return AssetTree;
}
public PageReference viewAssets(){
PageReference ret = new PageReference('/apex/ViewAssets?id='+accId);
ret.setRedirect(true);
return ret;
}
}