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
dbrunsdbruns 

Too many SOQL queries: 101

Newbie developer here. I am having a problem trying to solve an issue with too many queries using scheduled APEX. We have the following Account trigger that fires before update:

 

 

 

I am trying to update all Accounts en masse on the 1st of every month using Scheduled APEX, however, my Class will only work for a limited # of Accounts:

Works when I pass one record:

global class RunPartnerPerformance
{
    Set<Account> accs = new Set<Account>([select ID from Account where RecordTypeID = '012400000009Xes']);
    // Gets a unique list of Account ids
    {
    for ( Account account : accs ) 
          {if (account.ID == '0014000000L1CQQ')
            {
            update account;
            }
         }

    }
}

 

global class RunPartnerPerformance
{
    Set<Account> accs = new Set<Account>([select ID from Account where RecordTypeID = '012400000009Xes'] limit 1);
    // Gets a unique list of Account ids
    {
    for ( Account account : accs ) 
            {
            update account;      
            }

    }
}


Does not work in bulk:

global class RunPartnerPerformance
{
    Set<Account> accs = new Set<Account>([select ID from Account where RecordTypeID = '012400000009Xes']);
    // Gets a unique list of Account ids
    {
    for ( Account account : accs ) 
            {
            update account;      
            }

    }
}

 

Any suggestions are greatly appreciated!

dbrunsdbruns

Also, I can allow of a limit for up to 33. It appears that the combination of 3 queries in the trigger x 34 account updates is putting me over 100 queries.

Ispita_NavatarIspita_Navatar

I saw your code you are updating account in a for loop, which is increasing the count of DML operations.

Please do use a arrray of account and then update all the account in 1 go, this will reduce the DML queries dramatically.

Check the attached code for reference especially the highlighted code:-

 

       if(1)

                                {
                                    if(strLPId != null)
                                    {
                                        Account A1 = new Account(id = strLPId);
                                        A1.Total_Fund_Commitments__c = LPFAmount + dblCommitmentAmount;
                                        AccIDS.add(A1);
                                    }
                                    if(ParentId != null)
                                    {
                                        Account A2 = new Account(id = ParentId);
                                        A2.Total_Fund_Commitments__c = PInFAmount + dblCommitmentAmount;
                                        AccIDS.add(A2);
                                    }
                                }
                                      update AccIDS;
                            }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.