0

I am having an issue with my code that I should not get. I definitely have more than 10,000 opportunities/accounts in production but my code should only be updating the opportunities related to the account of the event I am inserting. I am getting this error when trying to insert the event on an account with less than 20 related opportunities. Any help is appreciated thanks.

Apex Trigger:

trigger EventTrigger on Event (before insert, before update) {
    if(Trigger.isInsert){
        list<Event> CSTIMPList = new list<Event>();
        for(Event evnt : trigger.new){
            if(evnt.Implementation_App_Type__c != null && evnt.System_Admin_Over_Ride__c != 'Mark Complete App Date'){
                if(evnt.Implementation_App_Type__c == 'CST'){
                    CSTIMPList.add(evnt);
                }
            }
        }
        if(CSTIMPList.size() > 0 && CSTIMPList != null){
            Event_HDL.updateimpdateCST(CSTIMPList);
        }
    }
}

Apex Class:

public class Event_HDL {
    public static void updateimpdateCST(Event[] evnts){
            List<Opportunity> OppList = new list<Opportunity>();
            List<Account> relatedacclist = new list<Account>();
            //map the Opportunity record
            Map<Id, List<Event>> oppIds = new Map<Id, List<Event>>{}; 
            for(Event evnt : evnts){
                oppIds.put(evnt.WhatId, new list<Event>());
                oppIds.get(evnt.WhatId).add(evnt);
            }
            //map the account
            Map<Id, List<Opportunity>>  soaIds= new Map<Id, List<Opportunity>>{};
            Map<Id, List<Opportunity>>  accIds= new Map<Id, List<Opportunity>>{};
            for(Opportunity opp : [Select Id, AccountId, Implementation_Appointment_Date__c,
                                   Account.Regarding_Review_FP__c, Account.Regarding_Review_Super__c, Account.Regarding_Review_ROA__c from Opportunity where Id in: oppIds.keySet()]){
                soaIds.put(opp.Account.Regarding_Review_FP__c, new list<Opportunity>());
                soaIds.put(opp.Account.Regarding_Review_Super__c, new list<Opportunity>());
                soaIds.put(opp.Account.Regarding_Review_ROA__c, new list<Opportunity>());
                soaIds.get(opp.Account.Regarding_Review_FP__c).add(opp);
                soaIds.get(opp.Account.Regarding_Review_Super__c).add(opp);
                soaIds.get(opp.Account.Regarding_Review_ROA__c).add(opp);
                accIds.put(opp.AccountId, new list<Opportunity>());
                accIds.get(opp.AccountId).add(opp);
            }
            //update the related account with the new imp app date
            for(Account acc : [Select Id from Account where Id in: accIds.keySet()]){
                for(Event evnt : evnts){
                    acc.CST_Implementation_Appointment_Date__c = evnt.StartDateTime;
                }
                relatedacclist.add(acc);
            }
            if(relatedacclist.size() > 0 && relatedacclist != null ){
                update relatedacclist;
            }
            //update all cst soas and recommendations(only from the latest review) off the back of them with the new imp app date
            for(Opportunity opp : [Select Id from Opportunity where Related_SOA__c in: soaIds.keySet() OR Id in: soaIds.keySet()]){
                for(Event evnt : evnts){
                    opp.Implementation_Appointment_Date__c = evnt.StartDateTime; 
                    opp.Trust_Deed_Date__c = evnt.StartDateTime;
                    opp.Trust_Deed_Appointment_Date__c = evnt.StartDateTime;
                }
                OppList.add(opp);
            }

            //update the opportunities
            if(OppList.size() > 0 && OppList != null){
                update OppList;
            }
    }
}
3
  • on which line you are getting this issue? update OppList; or update relatedacclist; ? Commented Jul 7, 2016 at 4:18
  • can you confirm if this trigger doesn't fire another trigger (Account and opportunity)? i guess which is causing error
    – Ratan Paul
    Commented Jul 7, 2016 at 4:20
  • look in your debug log and see if each soql is returning the # of records you expect
    – cropredy
    Commented Jul 7, 2016 at 5:00

1 Answer 1

0

The option I will suggest you is to use Batch Class. This will solve your problem once and for any number of records.

Create a batch class that will take input as List of SObjects and update it there.

Use Iterable<sObject> to provide values to execute method instead of Database.QueryLocator.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .