You need to sign in to do that
Don't have an account?
Sumant Kuchipudi
Too many SOQL Queries: 201
Hi,
I know Salesforce has limitations but I would need alternate to this issue, please check the below code and provide alternate to that?
The requirement is: calling externam API that has 28000 records to get and that API has limitations on results (only 400 allows) so loooping 72 to get 28K and another for loop on 400 records.
@future (callout=true)
public static void getAllUsersFromCampusLabs(){
for (integer i=1;i<=72;i++){
Map<String,String> parameters = new Map<String,String>();
parameters.put('page',string.valueof(i));
parameters.put('page','1');
parameters.put('pageSize','400');
String respString = DataAccessController.call_RESTFulAPI_Request('users',parameters);
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(respString);
List<Object> usersList = (List<Object>)jsonMap.get('items');
for (Object userObject:usersList){
Map<String, Object> user = (Map<String, Object>)userObject;
String jsonUserName = String.valueOf(user.get('userId')).trim();
String userName = (String)user.get('username');
String netID = userName.split('@')[0].trim().toLowerCase();
List<Contact> userContacts = [SELECT ID,userID__c,userName__c from Contact where userID__c=:netID];
if (userContacts.size()>0){
Contact userContact = userContacts[0];
userContact.userName__c = jsonUserName;
update userContact;
}
}
}
}
I know Salesforce has limitations but I would need alternate to this issue, please check the below code and provide alternate to that?
The requirement is: calling externam API that has 28000 records to get and that API has limitations on results (only 400 allows) so loooping 72 to get 28K and another for loop on 400 records.
@future (callout=true)
public static void getAllUsersFromCampusLabs(){
for (integer i=1;i<=72;i++){
Map<String,String> parameters = new Map<String,String>();
parameters.put('page',string.valueof(i));
parameters.put('page','1');
parameters.put('pageSize','400');
String respString = DataAccessController.call_RESTFulAPI_Request('users',parameters);
Map<String, Object> jsonMap = (Map<String, Object>) JSON.deserializeUntyped(respString);
List<Object> usersList = (List<Object>)jsonMap.get('items');
for (Object userObject:usersList){
Map<String, Object> user = (Map<String, Object>)userObject;
String jsonUserName = String.valueOf(user.get('userId')).trim();
String userName = (String)user.get('username');
String netID = userName.split('@')[0].trim().toLowerCase();
List<Contact> userContacts = [SELECT ID,userID__c,userName__c from Contact where userID__c=:netID];
if (userContacts.size()>0){
Contact userContact = userContacts[0];
userContact.userName__c = jsonUserName;
update userContact;
}
}
}
}
1) DML inside the for loop
2) SOQL inside the for loop.
Try to update your code like below
Let us know if this will help you
Thanks for your reply. I have tested the code but still I'm getting the error. I'm still wondering how the loop with 71 count crosses SOQL limit from our code. Note: Here "COS" is managed package in our org, I'm wondering how that counts in my code execution.
Take the debug log, and figure out what is the execution Order, like (After calling your method, you are updating contact, is there any trigger in contact firing, which could be managed and unmanaged, and then if there is any workflow or process builder or trigger do any dml operation again to make it recursive).
If you are not able to figure out, try to add debug statement at end of this method, and check how many soql is consumed.
and successively you can check it all trigger or all apex class, to check how many soql been invoked and how its been invoked.
Thanks