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
Jonathan Osgood 3Jonathan Osgood 3 

Dusting off Apex Chops

Hi All,

Sadly I havent been writing apex for a long time and could use some help. I'm repurposing some code I have to compare two records and throw an error if there is a match on date. This is a before trigger that calls the below helper class . I'm getting a null pointer error on line 30 when I try to get the date from my Map which leads me to beleive I'm not populating my map correctly. Any help would be greatly appreciated. 
 
public class EventTriggerHelper {
    
    public static void CheckForDoubleBookedLocations(List<Event__c> locEvts){
        
        //collect ID's to reduce data calls
        List<ID> locationIds = new List<ID>();
        Map<ID,Event__c> requestedEventTime = new map<ID,Event__c>();
        
        //get all Locations related to the trigger
        
        for(Event__c newEvent : locEvts){
            if(newEvent.Event_Location__c != null){
                locationIds.add(newEvent.Event_Location__c);
            }
            //Populate Map
            List<Event__c> relatedEvents = [SELECT Event_Location__c, Start_Date_Time__c 
                                            FROM Event__c 
                                            WHERE Event_Location__c IN :locationIds];   
            
            for(Event__c relatedEvent : relatedEvents){
                requestedEventTime.put(relatedEvent.Id, relatedEvent);
                
                //get existing events to check against
                list<Event__c> existingEvents = [SELECT ID, Event_Location__c, Start_Date_Time__c 
                                                 FROM Event__c 
                                                 WHERE Event_Location__c IN :locationIDs];
                
                //check one list against the other
                for(Event__c evs : locEvts){
                    DateTime existStartTime = requestedEventTime.get(evs.Id).Start_Date_Time__c;
                    
                    for(Event__c event : existingEvents){
                       
                    ////Add location and end date range Logic  
                        if(event.Start_Date_Time__c == existStartTime){
                            event.addError('The Location is already booked at that time');
                        }          
                    }   
                }  
            } 
        }
    }
}
Shivdeep KumarShivdeep Kumar
Hi,
Please try below code..
 
public class EventTriggerHelper {
    
    public static void CheckForDoubleBookedLocations(List<Event__c> locEvts){
        
        //collect ID's to reduce data calls
        List<ID> locationIds = new List<ID>();
        Map<ID,Event__c> requestedEventTime = new map<ID,Event__c>();
        
        //get all Locations related to the trigger
        
        for(Event__c newEvent : locEvts){
            if(newEvent.Event_Location__c != null){
                locationIds.add(newEvent.Event_Location__c);
            }
		}
		//get all related events
		List<Event__c> relatedEvents = [SELECT Event_Location__c, Start_Date_Time__c 
                                            FROM Event__c 
                                            WHERE Event_Location__c IN :locationIds];   
        // poupulate the Map    
		for(Event__c relatedEvent : relatedEvents){
			requestedEventTime.put(relatedEvent.Id, relatedEvent);
		}
              
		//check one list against the other
		for(Event__c evs : locEvts){
			If(! requestedEventTime.IsEmpty()){
				Event__c relatedEvent =requestedEventTime.get(evs.Event_Location__c);			
			////Add location and end date range Logic  
				if(evs.Start_Date_Time__c == relatedEvent.Start_Date_Time__c){
					evs.addError('The Location is already booked at that time');
				}          
			}   
		}          
    }
}

Please let me know if this help !

Thanks
Shivdeep