3

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.

  • How are you performing the validation itself? Your logic is fine, though you can consolidate the lists to save a dml operation. – Adrian Larson Mar 19 '16 at 19:22
  • This is being done in a Visualforce page so the validation is thrown automatically. Well I tried putting it in the same list but I had the same issue. So thought I'd have some luck if I put it in different try-catch blocks – User2529 Mar 19 '16 at 19:31
  • Yes but...that's not standard system validation. Is it? It looks like it must be a Validation Rule or Apex Trigger validation to me. Maybe I'm just unfamiliar with this chunk of functionality. Can you add the List declarations so it is easier to interpret the types based on your question? – Adrian Larson Mar 19 '16 at 19:32
  • Oh you mean the validation regarding the resources? That's a standard functionality as I am using the standard Public Calendar and Resources – User2529 Mar 19 '16 at 19:35
  • I've edited my post to show my List declaration – User2529 Mar 19 '16 at 19:36
4
     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?

  • User2529 ... if you have any question pls ask here salesforce.stackexchange.com/questions/ask – Ratan Paul Mar 21 '16 at 10:26
  • I'm not asking a new question. I had answered my own question saying it worked but then I realised it worked only for my Visualforce page but not for my trigger. So I edited my answer – User2529 Mar 21 '16 at 10:46
  • @User2529 means you have a new question. Worked for visualforce page and didn't worked for trigger. you basically you are asking a new question.. so pls ask in salesforce.stackexchange.com/questions/ask to get more attention.. – Ratan Paul Mar 21 '16 at 11:24
0

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;
          }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.