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
kunakuna 

System.LimitException: Too many script statements: 200001

Hi,

 

I am new to salesforce and tring to update two fields on Account through trigger while changing on call report object.

Below code is working fine for single record as well as bulk record.

 

While mass data(80,000 record) update  trying to update i am getting System.LimitException: Too many script statements: 200001 for 900 records.

 

Almost all the record got updated successfully but getting error for few records.

 

Can anyone please suggest what needs to be changed so that i will not get the above error.

 

 

 

trigger DisplayRltdCallRepOnAcc on Call_Report__c(after insert, after update,after delete){
    List<Call_Report__c> lastcallRltdCallRepOnAcc = new List<Call_Report__c>();
    List<Call_Report__c> nextCallRltdCallRepOnAcc = new List<Call_Report__c>();
    Set<Id> updatelastCallAccountIds = new set<Id>();
    Set<Id> updateNextCallAccountIds = new set<Id>();
    List<Call_Report__c> rltdToClientIds = new List<Call_Report__c>();
    List<Account> updateAccLastCall = new List<Account>();
    List<Account> updateAccNextCall = new List<Account>();
       
    if(trigger.isInsert || trigger.isUpdate){
        System.debug('Trigger.New values:::::: '+trigger.new);
            rltdToClientIds = trigger.new;
    }else if(trigger.isDelete){
           // Note -- there is no trigger.new in delete 
           rltdToClientIds = trigger.old;
          }
    
    List < Id > clientIds = new List < Id >();
    if(rltdToClientIds.size() > 0){
        for ( Call_Report__c  c: rltdToClientIds){
            clientIds.add( c.Related_to_Client__c );
        }
    }
    System.debug('Related to Client IDS '+ clientIds.size());
    if(clientIds.size()> 0){
        lastcallRltdCallRepOnAcc = [Select Call_Date__c,Related_to_Client__c From Call_Report__c c where Status__c='Complete'
                                    and c.Type__c not in ('Attempt','Email') and Call_Date__c <= TODAY
                                    and Related_to_Client__c!=null and
                                    Related_to_Client__c IN :clientIds order by Call_Date__c desc];
    }
    System.debug('After Query for Last Call :::::size is '+ lastcallRltdCallRepOnAcc.size());

    Date lastCalldate;
    List<Date> callDateListLastCall = new list<Date>();
   
    if(clientIds.size() > 0){
    for(Id cId: clientIds){
        callDateListLastCall.clear();
        if(lastcallRltdCallRepOnAcc.size() > 0){
            For(Call_Report__c cReport:lastcallRltdCallRepOnAcc ){
                if(cId == cReport.Related_to_Client__c){
                    callDateListLastCall.add(cReport.Call_Date__c);                          
                }   
            }
            if(callDateListLastCall.size() > 0){   
                lastCalldate= callDateListLastCall[0];
            }
            for(Integer i=0; i< callDateListLastCall.size(); i++){
                if(lastCalldate <= callDateListLastCall[i] ){
                    lastCalldate = callDateListLastCall[i];
                }
            }
            if(cId != null) {
                Account acc = new Account(Id = cId ,Last_Call_Date__c = lastCalldate );       
                System.debug('--Last Call Date-- '+acc.Last_Call_Date__c);
                if(updatelastCallAccountIds.add(acc.Id)){
                    updateAccLastCall.add(acc);
                }
            }
        }else{
            if(cId != null){
            Account acc = new Account(Id =cId , Last_Call_Date__c = null);
             if(updatelastCallAccountIds.add(acc.Id)){
                 updateAccLastCall.add(acc);
             }       
            }
    }

    }
    System.debug('Last call update Account@@@ '+ updateAccLastCall.size());
    update updateAccLastCall;

    }   
     

   
    if(clientIds.size()> 0){
        nextCallRltdCallRepOnAcc = [Select Call_Date__c,Related_to_Client__c,Type__c From Call_Report__c c where Status__c='Incomplete'
                                    and c.Type__c not in ('Attempt','Email') and  Call_Date__c >= TODAY
                                    and Related_to_Client__c!=null and
                                    Related_to_Client__c IN :clientIds order by Call_Date__c asc];
    }
    System.debug('After Query for Next Call size is @@@ '+ nextCallRltdCallRepOnAcc.size());
 
    Date nextCalldate;
    List<Date> callDateListNextCall = new list<Date>();
    //Date compareDateNextCall;
    if(clientIds.size() > 0){
        for(Id cId: clientIds){
            callDateListNextCall.clear();
            if(nextCallRltdCallRepOnAcc.size() > 0){
                For(Call_Report__c cReport:nextCallRltdCallRepOnAcc){
                    if(cId == cReport.Related_to_Client__c){
                        callDateListNextCall.add(cReport.Call_Date__c);                          
                    }   
                }
                if(callDateListNextCall.size() >0){
                    nextCalldate= callDateListNextCall[0];
                }
                for(Integer i=0; i< callDateListNextCall.size(); i++){
                    if(nextCalldate >= callDateListNextCall[i] ){
                        nextCalldate = callDateListNextCall[i];
                    }
                }
                if(cId != null) {
                    Account acc1 = new Account(Id = cId ,Next_Scheduled_Call__c = nextCalldate);       
                    System.debug('--Next Call Date-- '+acc1.Next_Scheduled_Call__c);
                    if(updateNextCallAccountIds.add(acc1.Id)){
                        updateAccNextCall.add(acc1);
                    }
                }   
            }else{
                if(cId != null){
                Account acc1 = new Account(Id = cId, Next_Scheduled_Call__c = null);
                    if(updateNextCallAccountIds.add(acc1.Id)){
                        updateAccNextCall.add(acc1);
                    }
               }       
            } 
        }
        System.debug('Next call update Account@@@ '+ updateAccNextCall.size());
        update updateAccNextCall;
    } 
  
}

Rahul SharmaRahul Sharma

This trigger can be optimized to a great extent.

Try to remove unused variables, avoid unnecessary for loops.

 

if(clientIds.size()> 0){
	//you queried record in a list using set
        lastcallRltdCallRepOnAcc = [Select Call_Date__c,Related_to_Client__c From Call_Report__c c where Status__c='Complete'
                                    and c.Type__c not in ('Attempt','Email') and Call_Date__c <= TODAY
                                    and Related_to_Client__c!=null and
                                    Related_to_Client__c IN :clientIds order by Call_Date__c desc];
    }
    System.debug('After Query for Last Call :::::size is '+ lastcallRltdCallRepOnAcc.size());
    Date lastCalldate;
    List<Date> callDateListLastCall = new list<Date>();
	
    
    if(clientIds.size() > 0){
    //Iterating Sets is not needed, instead just iterate List //Remove the set iteration as its of no use.
    for(Id cId: clientIds){
        callDateListLastCall.clear();
        if(lastcallRltdCallRepOnAcc.size() > 0){
	    // This is only needed
            For(Call_Report__c cReport:lastcallRltdCallRepOnAcc ){
                if(cId == cReport.Related_to_Client__c){
                    //Why are you storing it, you get the id directly with cReport.Call_Date__c			
                    callDateListLastCall.add(cReport.Call_Date__c);                          
                }   
            }
            if(callDateListLastCall.size() > 0){   
	    //you get the id directly with cReport.Call_Date__c, so why you using callDateListLastCall
                lastCalldate= callDateListLastCall[0];
            }
            for(Integer i=0; i< callDateListLastCall.size(); i++){
                if(lastCalldate <= callDateListLastCall[i] ){
                    lastCalldate = callDateListLastCall[i];
                }
            }
            if(cId != null) {
                Account acc = new Account(Id = cId ,Last_Call_Date__c = lastCalldate );       
                System.debug('--Last Call Date-- '+acc.Last_Call_Date__c);
                if(updatelastCallAccountIds.add(acc.Id)){
                    updateAccLastCall.add(acc);
                }
            }
        }else{
            if(cId != null){
            Account acc = new Account(Id =cId , Last_Call_Date__c = null);
             if(updatelastCallAccountIds.add(acc.Id)){
                 updateAccLastCall.add(acc);
             }       
            }
    }
    }
    System.debug('Last call update Account@@@ '+ updateAccLastCall.size());
    update updateAccLastCall;
    }   

 Due to use of a double for loop, your script statements are executed n times, n is number of client Ids.

 

kunakuna

Hi Rahul,

 

Thanks for your reply.

 

For each client have multiple call report with call dates.While updating call report i have to compare the dates because i have to display most recents call dates . So i am adding all dates in to a list and trying to compare .

 

I am not understand your First comment on queried.Here i am fetching and storing in to a list.

 

Please suggest.

 

Thanks

Ajit

Rahul SharmaRahul Sharma

What exaclty your trigger does?

kunakuna

 

We have two different objects .one is Account and another is Call Report .Call report is attached with Account .For each Account we can see the related call report.For each account multiple call reports are there .If i am going to update, insert or delete a call report for a particular client i need to display the latest call in two fields (Last Call and Next call ) which i have added as a Account custom fields.

 

I have tyo handle the code for bulk update also.

 

As trigger.new will retun all client Ids for bulk data so getting all related to client Ids from Call report.

 

Then I am getting all the call report records in a list for all the clients.

 

Using a for loop for each client Ids to get all call reports related to a particular client and getting all call dates for that client and storing in to a list.

Then i am comparing all the Call dates for each client for recent call and that i am trying to update on Account.

 

If there is no call records or condition is not matching for a call, I have to make two fields which are in Account as a null .   

 

Thanks

Ajit