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
DeekDeek 

Apex Code Fine Tuning

Hi All,

Need help to fine tune the below code as intermittently its throwing error  - System.LimitException: Too many query rows: 50001

This code updates the current date and time on a custom date/time field in Account when the Event Status (custom field) is set to 'Completed' for an Account/Oppty.

I am not getting a clue how to avoid this error, so requesting for a quick solution to refine this code if any.

Thanks in Advance!


trigger updatelastactivitydateinaccount on Event (after insert,after update) {
Set<Id> accIds = new Set<Id> ();
List<Account> lstAcc;


for(Event e: Trigger.new) {
if (e.AccountId != NULL &&  e.Currencyisocode == 'USD') {
    if(e.Activity_Status__c == 'Completed') {
       accIds.add(e.AccountId);
}
   
   
    lstAcc = [Select Last_Activity_Dt__c from Account where  Currencyisocode = 'USD' AND ID IN: accIds];
  
for(Event e: Trigger.new) {
   if(Trigger.isInsert) {
      if(e.Activity_Status__c == 'Completed') {
         for(Account a: lstAcc) {
            if(a.Id == e.AccountId) {
              if(e.StartDateTime>a.Last_Activity_Dt__c){
              a.Last_Activity_Dt__c= e.StartDateTime;
              }
            } 
         }
        
     }
  }

        else if(Trigger.isUpdate) {
          if(e.Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).Activity_Status__c != 'Completed') {
            for(Account a: lstAcc) {
                  if(a.Id == e.AccountId) {
                    if(e.StartDateTime>a.Last_Activity_Dt__c){
                    a.Last_Activity_Dt__c= e.StartDateTime;
                    }
                   }
            }
         }
      }
    }
update lstAcc;
}
Satish_SFDCSatish_SFDC
Are you doing a bulk update through data loader.

I think the error is due to the query which gets all the events . Is there a scope to add more filter critiera to that query.

Regards,
Satish Kumar
DeekDeek
Hi Sathish,

There is no bulk update anywhere. Its just happened for a user. Thats should be fine by adding more criteria.

Which query you ar referring to add filters?

Is it possible for you to suggest any generalized filter criteria?

Pls help.
Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

Try the  below modified code

trigger updatelastactivitydateinaccount on Event(after insert, after update)
{
    Set < Id > accIds = new Set < Id > ();
    list < account > lstforaccupdate = new list < account > ();
    for (Event e: Trigger.new)
    {
        if (e.AccountId != NULL && e.Currencyisocode == 'USD')
        {
            if (e.Activity_Status__c == 'Completed')
            {
                accIds.add(e.AccountId);
            }
        }
    }
    if (accIds.size() > 0)
    {
        map < id, account > lstAcc = new map < id, account > ([Select Last_Activity_Dt__c from Account where Currencyisocode = 'USD' AND ID IN: accIds]);
        for (Event e: Trigger.new)
        {
            if (Trigger.isInsert)
            {
                if (e.Activity_Status__c == 'Completed')
                {
                    if (lstAcc.containskey(e.AccountId))
                    {
                        if (e.StartDateTime > lstAcc.get(e.AccountId) Last_Activity_Dt__c)
                            lstforaccupdate.add(new account(id = e.AccountId, Last_Activity_Dt__c = e.StartDateTime));
                    }

                }
            }
            else if (Trigger.isUpdate)
            {
                if (e.Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).Activity_Status__c != 'Completed')
                {
                    if (lstAcc.containskey(e.AccountId))
                    {
                        if (e.StartDateTime > lstAcc.get(e.AccountId) Last_Activity_Dt__c)
                            lstforaccupdate.add(new account(id = e.AccountId, Last_Activity_Dt__c = e.StartDateTime));
                    }

                }
            }
        }
        if (lstforaccupdate.size() > 0)
            update lstforaccupdate;

    }
}
DeekDeek
Hi Ankit,

Deeply appreciate your help.  I have completely new to apex and never heard of maps keyword. A good learning for me.

The code is saved w/o any compilation errors but the code coverage dropped to 0%. This is bit frustrating though.


I have wriiten a test class for the first time  and still the coverage is 0 %.

Could you please help me on this ?

@isTest
private class EventTester {
static testMethod void myUnitTest() {
Account acct1 = new Account(name='test account1',Segment='Core');
insert acct1;
Event EventToCreate = new Event(WhatId=acct1.Id,AccountId=acct1.Id,Subject='TESTING',Description='dp',StartDateTime=date.Today());
insert EventToCreate;
Event EvtUpd = [select StartDateTime from Event where id = :EventToCreate.Id];
System.assert(EvtUpd.StartDateTime == EventToCreate.StartDateTime);
    }
}