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
kittu9kittu9 

System.LimitException" Too Many SOQL Queries 101

public class AutoDeleteSubscriptions2
{
public List<User> usr ;
public List<EntitySubscription> es ;
public Map<String,ID> m1 = new Map<String,ID>();
public Map<String,ID> m2 = new Map<String,ID>();

public List<EntitySubscription> es1;

public List<Group> grp ;
public void deleteSubscriptions()
{
usr = [SELECT Id FROM User];
es = [SELECT Id,SubscriberId FROM EntitySubscription];

grp = [SELECT Id FROM Group];

System.debug('************GRoup****************'+grp);

System.debug(' Entity Subscription size : '+es.size()+' User Size : '+ usr.size());

for(User u : usr)
{
m1.put(u.Id,u.Id);
}
System.debug('***********User Ids**********'+m1.values()+'*****Map Size****'+m1.size());

System.debug(' Entity Subscription size : '+es.size());

for (EntitySubscription e : es)
{
m2.put(e.SubscriberId,e.SubscriberId);
}

System.debug('*************Entity Subscription User Ids***********'+m2.values()+'*****Map Size****'+m2.size());
// Comparing two map values
Set<String> ls = m1.keySet();

Set<String> ls1 = m2.keySet();
System.debug('**************List Elements*****************'+ls.size()+'*************'+ls1.size());

for (Id i :ls)
{
for ( Id j : ls1)
{
System.debug(' I = '+i +' J = '+j);
if ( i == j)
{
System.debug(' Both are equal ' + i +'***************** '+ j);
List<EntitySubscription> es = [select Id from EntitySubscription where SubscriberId =: j]; //50th line
System.debug('**************************List Size ***********************'+es.size());

if(es.size() > 490)
{
es1 = [select Id from EntitySubscription where SubscriberId =: j and ParentId NOT IN :usr order by CreatedDate limit 25];
delete es1;
}
}
}
}
}
}

 

I am moving this class to production am getting the following error "System.LimitException"  Too Many SOQL Queries 101.

Class.AutoDeleteSubscriptions2.deleteSubscriptions:line 50,column 1

Bhawani SharmaBhawani Sharma
You have SOQL query in double for loop.
for(ls) {
for(ls) {
Query
}
}

So suppose there are 10 items in ls list and 15 items in ls1 for each ls record. Now you query will be executed 10*15=150 times. while you have limit of 100. Please keep your query outside the loop. Use Map or List to hold the data and use that in looping statements
kittu9kittu9

can you please help me how to change that code to avoid that situation? Please help me on that.

Bhawani SharmaBhawani Sharma
What is your objective here? Do you only want to delete EntitySubscription record after it's reached on > 490 for a User ?
kittu9kittu9

The main objective is we have list of users. In that list of users whose entity subscription count > 490
have to delete those entity subscriptions.

Bhawani SharmaBhawani Sharma
You can use group by query for this purpose. Like
for(AggregateResult agg : [Select count() total, SubscriberId from
EntitySubscription group by SubscriberId]) {

if(agg.get('total') > 490) {
//do you logic
}

}
kittu9kittu9

for(AggregateResult agg : [Select count() total, SubscriberId,Id from EntitySubscription group by SubscriberId =: j])
{
if(agg.get('total') > 490) {
//do you logic
}

}

 

I given like above, Displaying error message like "Error: AutoDeleteSubscriptions2 Compile Error: unexpected token: total at line 63 column 47"

Raj.ax1558Raj.ax1558

Hi, 

 

You have write SOQL statement in for loop. I tried to resolve you problem and sumrised you code.

 

public class AutoDeleteSubscriptions2
{
public List<User> usr ;
public List<EntitySubscription> es ;
public Map<String,ID> m1 = new Map<String,ID>();
public Map<String,ID> m2 = new Map<String,ID>();
public List<EntitySubscription> es1;
public List<Group> grp ;
public void deleteSubscriptions()
{
usr = [SELECT Id FROM User];
es = [SELECT Id,SubscriberId FROM EntitySubscription];

grp = [SELECT Id FROM Group];

System.debug('************GRoup****************'+grp);

System.debug(' Entity Subscription size : '+es.size()+' User Size : '+ usr.size());

for(User u : usr)
{
m1.put(u.Id,u.Id);
}
System.debug('***********User Ids**********'+m1.values()+'*****Map Size****'+m1.size());

System.debug(' Entity Subscription size : '+es.size());

for (EntitySubscription e : es)
{
m2.put(e.SubscriberId,e.SubscriberId);
}

System.debug('*************Entity Subscription User Ids***********'+m2.values()+'*****Map Size****'+m2.size());
// Comparing two map values
Set<String> ls = m1.keySet();

Set<String> ls1 = m2.keySet();
System.debug('**************List Elements*****************'+ls.size()+'*************'+ls1.size());
list<EntitySubscription> TemperoryDeteteList = new list<EntitySubscription>();
Set<id> jIdset = new set<id>();
for (Id i :ls)
{
for ( Id j : ls1)
{
System.debug(' I = '+i +' J = '+j);
if ( i == j)
{
jIdset.add(j);
}
}
}
if(jIdset.size() > ))
{
System.debug(' Both are equal ' + i +'***************** '+ j);
List<EntitySubscription> es = [select Id from EntitySubscription where SubscriberId IN: jIdset ]; //50th line
}
if(es.size() > 490)
{
es1 = [select Id from EntitySubscription where SubscriberId IN: jIdset and ParentId NOT IN :usr order by CreatedDate limit 1];
delete es1;
}


}
}
}

 

 

Check this code, I used set In this and remove the delete statement from for loop.

 

Click on KUDOS button if the post helps you!!!! Marked as solution for others users help in a same query.

 

Thank you, 

Raj Jha

kittu9kittu9

for (Id i :ls)
{
for ( Id j : ls1)
{
  System.debug(' I = '+i +' J = '+j);
  if ( i == j)
  {
    jIdset.add(j);
  }
}
}

 

this "jIdset" set will contains all the subscribers Ids,same as es list declare above. Here based on the "j" value am retriving only that particular user id records to test the count.  So am getting that System.LimitException" Too Many SOQL Queries 101

 

List<EntitySubscription> es = [select Id from EntitySubscription where SubscriberId IN: jIdset ]; 

 

with this will get all the entitysubscriptions of all the user. But want to test for each and every users count. Can you please change according to this. Thanks in advance