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
Mohidheen NMohidheen N 

EXCEPTION: System.LimitException: Too many SOQL queries: 101 STACKTRACE: AnonymousBlock: line 9, column 1

List<UserPackageLicense > llist = new List <UserPackageLicense > ([SELECT id,UserId FROM UserPackageLicense
where PackageLicenseId='05000000000000000']); //- 5000 records

system.debug(llist.size());

Map<ID,Datetime> C_U_map = new Map<ID,Datetime>();

for (UserPackageLicense Ulist: llist){
List<User> C_U_list = new List<User>([select id,LastLoginDate  from User where User.id =:Ulist.UserId]);
C_U_map.put(Ulist.UserId,C_U_list[0].LastLoginDate);

system.debug(C_U_map);
C_U_map.clear();
}

Hi There,

I'm using the above AnonymousBlock and getting governer limits. If i'm using limit clause then code is working fine.

UserPackageLicense having 5000 records how can i retrieve without governer limits.

Can you please suggest?
Best Answer chosen by Mohidheen N
Pradeep SinghPradeep Singh
Remove List<User> C_U_list = new List<User>([select id,LastLoginDate  from User where User.id =:Ulist.UserId]); out of for loop.
Use a set to get the ids of user and then after for loop, apply query to get the required data.

All Answers

Raj VakatiRaj Vakati
  1. You can export the data by using data load and update it back again 
  2. Use batch apex to do this update 
Pradeep SinghPradeep Singh
Remove List<User> C_U_list = new List<User>([select id,LastLoginDate  from User where User.id =:Ulist.UserId]); out of for loop.
Use a set to get the ids of user and then after for loop, apply query to get the required data.
This was selected as the best answer
Mohidheen NMohidheen N
Initially i struggled via set but finally got the result in expected format, Many thanks :)
Mohidheen NMohidheen N
List<UserPackageLicense > llist = new List <UserPackageLicense > ([SELECT id,UserId FROM UserPackageLicense
where PackageLicenseId='0500000000000000']);
system.debug(llist.size());

set<ID> Set_ID = new set<ID>();
for (UserPackageLicense Ulist:llist){

Set_ID.add(Ulist.UserId);
}
system.debug(Set_ID.size());


List<User> C_U_list = new List<User>([select id,name,LastLoginDate from User where User.id IN:Set_ID and LastLoginDate < LAST_N_DAYS:365]);
system.debug(C_U_list.size());
system.debug(C_U_list);