You need to sign in to do that
Don't have an account?
Peter Bölke
Query limit reached
Hello,
i am getting an Error "Too many SOQL queries: 101".
This Error occur in this line
Any Ideas :(?
Thanks
Peter
i am getting an Error "Too many SOQL queries: 101".
public class WarengruppenZuordnung { /********************* Properties used by getRootNodeOfUserTree function - starts **********************/ // map to hold roles with Id as the key private static Map <Id, Warengruppen_Struktur__c> warengruppeMap; // map to hold child roles with parentRoleId as the key private static Map <Id, List<Warengruppen_Struktur__c>> parentWarengruppeMap; // List holds all subordinates private static List<Warengruppen_Struktur__c> allProducts {get; set;} // Global JSON generator private static JSONGenerator gen {get; set;} public static String folderId{get;set;} public static String folderName{get;set;} public static String endpointId{get;set;} /********************* Properties used by getRootNodeOfUserTree function - ends **********************/ /********************* Properties used by getSObjectTypeById function - starts ********************* */ // map to hold global describe data private static Map<String,Schema.SObjectType> gd; // map to store objects and their prefixes private static Map<String, String> keyPrefixMap; // to hold set of all sObject prefixes private static Set<String> keyPrefixSet; /********************* Properties used by getSObjectTypeById function - ends **********************/ public static String redirUrl{get;set;} public String contactId {get;set;} /* // initialize helper data */ static { // initialize helper data for getSObjectTypeById function Map<String, String> UrlParameterMap = ApexPages.currentPage().getParameters(); //redirUrl = getRedirUrl(); gen = JSON.createGenerator(true); init1(); gen = JSON.createGenerator(true); // initialize helper data for getRootNodeOfUserTree function init2(); } /* // init1 starts <to initialise helper data> */ private static void init1() { // get all objects from the org gd = Schema.getGlobalDescribe(); // to store objects and their prefixes keyPrefixMap = new Map<String, String>{}; //get the object prefix in IDs keyPrefixSet = gd.keySet(); // fill up the prefixes map for(String sObj : keyPrefixSet) { Schema.DescribeSObjectResult r = gd.get(sObj).getDescribe(); String tempName = r.getName(); String tempPrefix = r.getKeyPrefix(); keyPrefixMap.put(tempPrefix, tempName); } } /* // init1 ends */ /* // init2 starts <to initialise helper data> */ private static void init2() { // Create a blank list allProducts = new List<Warengruppen_Struktur__c>(); for(AggregateResult wsc : [SELECT Name, RecordTypeId, RecordType.DeveloperName FROM Warengruppen_Struktur__c GROUP BY RecordTypeId, RecordType.DeveloperName, Name ORDER BY Name]) { if(String.valueOf(wsc.get('DeveloperName')).contains('Ordner')){ folderId = String.valueOf(wsc.get('RecordTypeId')); folderName = String.valueOf(wsc.get('Name')); } if(String.valueOf(wsc.get('DeveloperName')).contains('Endpunkt')){ endpointId = String.valueOf(wsc.get('RecordTypeId')); } } warengruppeMap = new Map<Id, Warengruppen_Struktur__c>([select Id, Name, Parent_Warengruppe__c, RecordTypeId from Warengruppen_Struktur__c order by Name]); // populate parent role - child roles map parentWarengruppeMap = new Map <Id, List<Warengruppen_Struktur__c>>(); for (Warengruppen_Struktur__c r : warengruppeMap.values()) { List<Warengruppen_Struktur__c> tempList; if (!parentWarengruppeMap.containsKey(r.Parent_Warengruppe__c)){ tempList = new List<Warengruppen_Struktur__c>(); tempList.Add(r); parentWarengruppeMap.put(r.Parent_Warengruppe__c, tempList); } else { tempList = (List<Warengruppen_Struktur__c>)parentWarengruppeMap.get(r.Parent_Warengruppe__c); tempList.add(r); parentWarengruppeMap.put(r.Parent_Warengruppe__c, tempList); } } } /* // init2 ends */ /* // public method to get the starting node of the RoleTree along with user list */ public static RoleNodeWrapper getRootNodeOfUserTree (Id userOrRoleId) { return createNode(userOrRoleId); } /* // createNode starts */ private static RoleNodeWrapper createNode(Id objId) { RoleNodeWrapper n = new RoleNodeWrapper(); Id roleId; if (isRole(objId)) { roleId = objId; if (!(warengruppeMap.get(objId).Parent_Warengruppe__c == null)) { List<Warengruppen_Struktur__c> tempFolderList = new List<Warengruppen_Struktur__c>(); Warengruppen_Struktur__c tempFolder = [Select Id, Name, Parent_Warengruppe__c, RecordTypeId, RecordType.DeveloperName from Warengruppen_Struktur__c where Id =: objId ORDER BY Name]; tempFolderList.add(tempFolder); if(tempFolderList.size()== 1){ if(tempFolderList[0].RecordType.DeveloperName.contains('Ordner')){ n.hasFolders = false; n.myFolders = tempFolderList; n.isLeafNode = false; allProducts.addAll(n.myFolders); } else{ n.hasFolders = false; n.myFolders = tempFolderList; n.isLeafNode = true; allProducts.addAll(n.myFolders); } } } } else { List<Warengruppen_Struktur__c> tempFolderList = new List<Warengruppen_Struktur__c>(); Warengruppen_Struktur__c tempFolder = [Select Id, Name, Parent_Warengruppe__c, RecordTypeId from Warengruppen_Struktur__c where Id =: objId ORDER BY Name]; tempFolderList.add(tempFolder); n.myFolders = tempFolderList; roleId = tempFolder.Parent_Warengruppe__c; } n.myRoleId = roleId; n.myRoleName = warengruppeMap.get(roleId).Name; n.myParentRoleId = warengruppeMap.get(roleId).Parent_Warengruppe__c; n.RecordTypeId = warengruppeMap.get(roleId).RecordTypeId; if (parentWarengruppeMap.containsKey(roleId)){ n.hasChildren = true; n.isLeafNode = false; List<RoleNodeWrapper> lst = new List<RoleNodeWrapper>(); for (Warengruppen_Struktur__c r : parentWarengruppeMap.get(roleId)) { lst.add(createNode(r.Id)); } n.myChildNodes = lst; } else { n.isLeafNode = true; n.hasChildren = false; } return n; } public static List<Warengruppen_Struktur__c> getAllProducts(Id warengruppeId){ createNode(warengruppeId); return allProducts; } public static String getTreeJSON(Id userOrRoleId) { gen = JSON.createGenerator(true); RoleNodeWrapper node = createNode(userOrRoleId); gen.writeStartArray(); convertNodeToJSON(node); gen.writeEndArray(); return gen.getAsString(); } public static String getTreeJSON() { gen = JSON.createGenerator(true); List<Warengruppen_Struktur__c> wRoots = [Select Id, Name, RecordTypeId From Warengruppen_Struktur__c Where Parent_Warengruppe__c = '' ORDER BY Name]; gen.writeStartArray(); for(Warengruppen_Struktur__c ws : wRoots){ RoleNodeWrapper node = createNode(ws.Id); convertNodeToJSON(node); } gen.writeEndArray(); return gen.getAsString(); } private static void convertNodeToJSON(RoleNodeWrapper objRNW){ gen.writeStartObject(); gen.writeStringField('title', objRNW.myRoleName); gen.writeStringField('key', objRNW.myRoleId); gen.writeStringField('RecordTypeID', String.valueOf(objRNW.RecordTypeId)); gen.writeBooleanField('unselectable', false); gen.writeBooleanField('expand', true); gen.writeBooleanField('isFolder', true); if (objRNW.hasFolders || objRNW.hasChildren) { gen.writeFieldName('children'); gen.writeStartArray(); if (objRNW.hasFolders) { for (Warengruppen_Struktur__c u : objRNW.myFolders) { gen.writeStartObject(); gen.writeStringField('title', u.Name); gen.writeStringField('key', u.Id); gen.writeStringField('RecordTypeID', String.valueOf(objRNW.RecordTypeId)); gen.writeBooleanField('isFolder', false); gen.WriteEndObject(); } } if (objRNW.hasChildren) { for (RoleNodeWrapper r : objRNW.myChildNodes) { convertNodeToJSON(r); } } gen.writeEndArray(); } gen.writeEndObject(); } /* // general utility function to get the SObjectType of the Id passed as the argument, to be used in conjunction with */ public static String getSObjectTypeById(Id objectId) { String tPrefix = objectId; tPrefix = tPrefix.subString(0,3); String objectType = keyPrefixMap.get(tPrefix); return objectType; } /* // utility function getSObjectTypeById ends */ /* // check the object type of objId using the utility function getSObjectTypeById and return 'true' if it's of Role type */ public static Boolean isRole (Id objId) { if (getSObjectTypeById(objId) == String.valueOf(Warengruppen_Struktur__c.sObjectType)) { return true; } else if (getSObjectTypeById(objId) != String.valueOf(Warengruppen_Struktur__c.sObjectType)) { return false; } return false; } /* // isRole ends */ public class RoleNodeWrapper { // Role info properties - begin public String myRoleName {get; set;} public Id myRoleId {get; set;} public Id RecordTypeId {get; set;} public String myParentRoleId {get; set;} // Role info properties - end // Node children identifier properties - begin public Boolean hasChildren {get; set;} public Boolean isLeafNode {get; set;} public Boolean hasFolders {get; set;} // Node children identifier properties - end // Node children properties - begin public List<Warengruppen_Struktur__c> myFolders {get; set;} public List<RoleNodeWrapper> myChildNodes {get; set;} // Node children properties - end public RoleNodeWrapper(){ hasFolders = false; hasChildren = false; } } public static String getRedirUrl(){ Map<String, String> UrlParameterMap = ApexPages.currentPage().getParameters(); redirUrl = UrlParameterMap.values()[1]; return redirUrl; } public String getContactId() { return Apexpages.currentPage().getParameters().get('id'); } }
This Error occur in this line
Warengruppen_Struktur__c tempFolder = [Select Id, Name, Parent_Warengruppe__c, RecordTypeId, RecordType.DeveloperName from Warengruppen_Struktur__c where Id =: objId ORDER BY Name];How to change this to get it working?
Any Ideas :(?
Thanks
Peter
- you written soql query in createNode method.
- you called createNode method in for loop
for CPU limit, just made null what maps and lists after using then you can save your heap sizeAll Answers
May I suggest You please refer the below link for reference.
- https://help.salesforce.com/articleView?id=000181404&type=1
- https://salesforce.stackexchange.com/questions/16432/workarounds-to-avoid-error-too-many-soql-queries-101
hope it helps.Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.
Thanks
Rahul Kumar
I read those articles. My Problem is to understand how can i make my code bulkified? And further more my function "createNode" is selfreferencing. This is causing even more trouble.
Thanks
Peter
i changes my Code that it uses only maps instead of SOQL-Queries. So far i am not hitting the SOQL-Limit,..but now the CPU-Limit.
My createNode now takes an ID-Set. But i dont know how to rewrite the recursion or put them to another function.
thanks for you patience. I was able to make the method "createNode" use the Maps instead of SOQL-Queries. MY problem ist now that the hiearchy is not created properly.
It should look like:
0a
-0a0a
--0a0a0a
--0a0a0b
-0a0b
0b
-0b0a
and so on. but it looks like
0a
-0a0a
-0a0a0a
-0a0a0b
-0a0b
0b
-0b0a
so there is only one sublevel.
My current code is:
This code is a adopted version of this http://forceguru.blogspot.de/2011/12/displaying-role-hierarchy-on.html .
Thanks very much for your efforts
Peter
- you written soql query in createNode method.
- you called createNode method in for loop
for CPU limit, just made null what maps and lists after using then you can save your heap size