Question: I am using standard `Public Calendars and Resources` where I have 2 types of resources, Vector Unit and Demo Unit. Now if any of the resources have a conflicting schedule, a standard Salesforce error is thrown and I am displaying this error using a `Try and Catch` block.

```
List <EventRelation> vectorRelationList = new List <EventRelation> ();    
List <EventRelation> demoRelationList = new List <EventRelation> (); 
if(!demoRelationList.isEmpty()) {
    try {
        insert demoRelationList; 
    }
    catch(DmlException ex) {
        demoRelationList.clear();
        delete eventList;
        return null;
    }
}          
if(!vectorRelationList.isEmpty()) {
    try {
        insert vectorRelationList;
    }
    catch(DmlException ex) {
        vectorRelationList.clear();
        delete eventList;
        return null;
    }
}

```

This is my code where I'm inserting the Demo Unit and the Vector Unit as my resources. 

Now, the problem is if I have a Demo Unit that has a conflicting schedule, the error is thrown but if a Vector Unit has a conflicting schedule and the Demo Unit doesn't, the event gets created with the vector unit having conflicting schedules. 

Notice that the `demoRelationList` is inserted first and then the `vectorRelationList`. If I change the order of insertion, then the event is created if the Demo Unit has a conflicting schedule but the vector unit doesn't.

So what am I missing here? If someone could help me out, it'd be great!

**/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*UPDATE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/**

I figured out that the `Try-Catch` block has got nothing to do with this rather it is the functionality of the resource.

Answer: 
```
     if(!demoRelationList.isEmpty()){
           try{
               insert demoRelationList; 
           }catch(DmlException ex){
               demoRelationList.clear();
               delete eventList;
               return null;
           }
      }

      if(!vectorRelationList.isEmpty()){
          try{
              insert vectorRelationList;
          }catch(DmlException ex){
              exc = string.valueof(ex);

          }
          finally{
              if(Exc != NULL){
                  vectorRelationList.clear();
                  ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'The Vector Unit is not available!'));
                  delete eventList;                  
                  return null;
              }
          }
      }

```

This code worked for my Visualforce page but it's not working for my trigger. Any inputs?
So I added the resources to a list and queried all the `events` which had the same start and end date and queried the `eventrelations` which are assigned to the current event. Worked like a charm!

```
     if(!eventRelationList.isEmpty() && !vectorRelationList.isEmpty() && !demoRelationList.isEmpty()){
          boolean flag = TRUE;
          List<Event> conflictEventList = new List<Event> ();
          conflictEventList = [Select EndDateTime, StartDateTime, (Select RelationId, Status from EventRelations where ID IN: vectorRelationList OR ID IN: demoRelationList)
                                FROM Event WHERE EndDateTime IN: endDateSet AND StartDateTime IN: startDateSet];
          system.debug('*********conflictEventList***********'+conflictEventList);
          system.debug('*********vectorRelationList***********'+vectorRelationList);
          system.debug('*********demoRelationList***********'+demoRelationList);
          if(!conflictEventList.isEmpty()){
              for(Demo_Form__c Demo : newList){
                for(Event ev: conflictEventList){
                    if(!ev.EventRelations.isEmpty()){
                        system.debug('*********ev.EventRelations***********'+ev.EventRelations);
                        demo.addError('One of the Resources has already been assigned! Kindly check the Salesforce Calendar and contact the Demo Manager');
                        flag = FALSE;
                    }
                }
              }
          }
          if(flag == TRUE){
            insert eventRelationList;
            insert vectorRelationList;
            insert demoRelationList;
          }
          else{
            eventRelationList.clear();
            demoRelationList.clear();
            vectorRelationList.clear();
            conflictEventList.clear();
            delete eventList;
          }

```