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
Sumant KuchipudiSumant 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;
                    }
                }
            }
    }
Amit Chaudhary 8Amit Chaudhary 8
Issue is coming because of below issue

1) DML inside the for loop
2) SOQL inside the for loop.


Try to update your code like below
@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');
			
			set<String> setUserId = new Set<String>();
                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();
					
					setUserId.add(netID);	
				}
				
				List<Contact> lstCont = [ SELECT ID,userID__c,userName__c  from Contact where userID__c in :setUserId ];
				Map<String , Contact> mapUserIdWiseContact =  new Map<String , Contact>();
				for(Contact cont : lstCont)
				{
					mapUserIdWiseContact.put(cont.userID__c , cont);
				}
				List<Contact> lstContactToUpdate = new List<Contact>();
                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();
					
                    if (mapUserIdWiseContact.containsKey(netID) )
					{
                        Contact userContact = mapUserIdWiseContact.get(netID);
						
                        userContact.userName__c = jsonUserName;
						lstContactToUpdate.add(userContact);
                        //update userContact;
                    }
                }
				
				if(lstContactToUpdate.size() > 0 )
				{
					update lstContactToUpdate;
				}
        }
    }

Let us know if this will help you
 
Sumant KuchipudiSumant Kuchipudi
Hi Amit,

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. 
22:13:22.3 (10220964900)|FATAL_ERROR|System.LimitException: COS:Too many SOQL queries: 201
Note: Here "COS" is managed package in our org, I'm wondering how that counts in my code execution.
 
Saravana Bharathi 1Saravana Bharathi 1
Check whether any recursive call is happening.
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