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
Melody4500Melody4500 

Too many records coming back on LIST in Trigger.isBefore, need to rewrite to Map?

Hello, 
I am troubleshooting a Trigger.isBefore where there is a List that pulls back almost 10,000 records. This is being triggered from a VF page and when they pick multiple appointments in an execution, it can hit the 50,000+ SOQL rows returned error. I wanted to replace the Trigger.New with Trigger.NewMap but since this is an isBefore trigger, the IDs won't be there so it won't work (if I'm understanding that correctly). Any other recommendations on how I can rewrite to pull back less records or to only run the query once? SOQL Query that is causing the issue is bolded below. 

trigger StudioSlotTriggerAll on Studio_Slot__c (before insert, before update) 
{
    if(Trigger.isBefore)
    {
        if(Trigger.isInsert || Trigger.isUpdate){
            Datetime startDate = Datetime.now();
            Datetime endDate = Datetime.now();
            Set<Id> studioIds = new Set<Id>();
            {
                for(Studio_Slot__c studioSlot : Trigger.new)
                {
                    
                    if(studioSlot.Start_Time__c >= studioSlot.End_Time__c)
                    {
                        studioSlot.addError('Start Date must be before End Date!');
                    }
                    else
                    {
                        if(studioSlot.Start_Time__c > startDate) 
                        {
                            startDate = studioSlot.Start_Time__c;
                        }
                        
                        if(studioSlot.End_Time__c > endDate)
                        {
                            endDate = studioSlot.End_Time__c;
                        }
                    }
                    
                    studioIds.add(studioSlot.Studio__c);
                }
            }
            
            List<Studio_Slot__c> existingStudioSlotList =
                [SELECT Start_Time__c, End_Time__c, Id, Studio__c
                 FROM Studio_Slot__c
                 WHERE Start_Time__c >= :startDate
                 AND End_Time__c <= :endDate
                 ORDER BY Start_Time__c 
                 Limit 10000 
                ];
 
            for(Studio_Slot__c studioSlot : Trigger.new)
            {
                for(Studio_Slot__c existingStudioSlot : existingStudioSlotList)
                {
                    //overlapping studio slots
                    if(studioSlot.Id != existingStudioSlot.Id && studioSlot.Studio__c == existingStudioSlot.Studio__c && (existingStudioSlot.Start_Time__c < studioSlot.End_Time__c && existingStudioSlot.End_Time__c > studioSlot.Start_Time__c))
                    {
                        studioSlot.addError('Studio Slot is overlapping with the existing studio slot(Id): ' + existingStudioSlot.Id);
                    }
                }
            }
Navin Selvaraj23Navin Selvaraj23
Hi Melody4500,

You should use more Where clause to reduce the number of returning queries. So, there will be the common fields or data between the incoming records and aleadt available records in SF. So, you have to use maximum fieds to filter for the return of minimun records.

List<Studio_Slot__c> existingStudioSlotList =
                [SELECT Start_Time__c, End_Time__c, Id, Studio__c
                 FROM Studio_Slot__c
                 WHERE Start_Time__c >= :startDate
                 AND End_Time__c <= :endDate
                 ORDER BY Start_Time__c 
                 Limit 10000 
                ];  

In the above query, you can use Studio__c in where clause to filter the records. By the way of using where clause, you will have minimum records in return.
So, no error will be thrown.

Regards,
Navin S