+ Start a Discussion
Shubham KherdeShubham Kherde 


Hi all,
I am facing Too many SOQL queries: 101 error for following code but as per governor limit my SOQL Query is outside the for loop still getting this error and also showing following process builder error with this
 ( 09:59:49:020 EXCEPTION_THROWN [184]|System.DmlException: Upsert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Update Oppty Lookup RelatedTo Events 2 test” process failed. Give your Salesforce admin these details. This error occurred when the flow tried to update records: LIMIT_EXCEEDED: System.LimitException: Too many SOQL queries: 101. You can look up ExceptionCode values in the SOAP API Developer Guide. Error ID: 1987318438-82422 (-64273350): []   )
  Map<Id, Opportunity> oppdbmap = new Map<Id, Opportunity>(
            [select Id, Booked__c, Initial_Appt_Date_Time__c, Initial_Appt_Time__c,
                (select Id, Subject, ActivityDateTime from Events where What.Type = 'Opportunity' and ActivityDateTime != null and Subject != 'Cancelled Appointment' order by ActivityDateTime desc limit 1) 
             from Opportunity where Id in: oppevmap.keyset()]);

        // if an existing event is more recent than a new one, we will use an existing one
        for (Id oid: oppevmap.keyset()){
            Event etempnew = oppevmap.get(oid);
            Opportunity oppdb = oppdbmap.get(oid);
            Event etempdb = oppdb == null ? null : oppdb.Events == null ? null : oppdb.Events.isempty() ? null : oppdb.Events[0];
            if (etempdb != null && etempdb.ActivityDateTime != null){
                if (etempnew == null || etempnew.ActivityDateTime == null || etempdb.ActivityDateTime > etempnew.ActivityDateTime){
                    oppevmap.put(oid, etempdb);

would anyone know how I can fix?

Shubham Kherde