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
Kishore B T 21Kishore B T 21 

how to avoid the de-duplicate event

I have an event on 12/31/2015 10:00 AM and ends at  12/31/2015 11:00 AM.

But I can compare the datetime with Greater than or equal to
select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime,venue__c from Event where  startdatetime >= 2015-12-29T01:30:00.000+0000,

But i want to do the same with  Less than or equal to or even Less than it's not working.

Please, someone, guide me on this to avoid de-duplication of event,
should give error msg when user, is trying to create a event  in between of startdatetime and enddatetime of a created event.

James LoghryJames Loghry
Kishore are you looking for an exact duplicate event or perhaps an overlapping event?

Here's how you *might* structure your query to check for duplicate events:
select Id from Event where 
    not((startdatetime < 2015-12-31T10:00:00.000+0000
    and enddatetime < 2015-12-31T10:00.000+0000) Or
    (startdatetime > 2015-12-31T11:00:00.000+0000
    and enddatetime > 2015-12-31T11:00:00.000+0000))

The query uses logical functions to check for the following situations:
  • If the start date of the new event is during an existing event, but the end date is afterward.
  • If the start date of the new event is before an existing eent, but the end date is during the existing event
  • If the start date of the new event is before the existing event, and after the existing event.
Of course you might have to tweak it a bit, especially if you're worried about stuff like timezones.

However, that should at least get you started.
 
srlawr uksrlawr uk
First off, I would point out that, you can't compare dates in SOQL... so even though in your example above you are using a date string '2015-12-29T01:30:00.000+0000' if that is actually (in your query) a date field from somewhere else, you're gunna have a bad time sadly :(

The "trick" widely accepted is to put the date comparison into a formula, and then "stick" the date in question into a field (referenced by that formula) and you can the run a SOQL query over the formula field (ie. add a field called event_create_date__c which you check against startdatetime and enddatetime in a seperate formula (which can be a checkbox) and then in your code, stuff the date value into event_create_date__c and then SOQL against the formula).

I don't think I have made that very clear at all.. 
Kishore B T 21Kishore B T 21
Sorry guys I haven't mentioned this, I am comparing the dates Dynamically from the page to controller, it's not Hardcoded, and while comparing the dates, the format looks like 12/30/2015 06:40 PM  
This is how my calender looks.

 User-added image

this is my VF Page
 
<apex:pageBlock >   
          <apex:pageBlockSection >   
            <apex:pageblockSectionItem >
                Venue: <apex:inputField value="{!eventDetail.Venue__c}">
                </apex:inputField>                
            </apex:pageblockSectionItem>     
             </apex:pageBlockSection>    
             <apex:pageBlockSection >   
            <apex:pageblockSectionItem >
                StartDate: <apex:inputField value="{!eventDetail.startdatetime}">
                </apex:inputField>                
            </apex:pageblockSectionItem>     
             </apex:pageBlockSection>    
             <apex:pageBlockSection >   
            <apex:pageblockSectionItem >
                EndDate: <apex:inputField value="{!eventDetail.enddatetime}">
                </apex:inputField>                
            </apex:pageblockSectionItem>     
             </apex:pageBlockSection>    
          </apex:pageBlock>   
<apex:commandButton action="{!saveAndCongrat}" value="Validate"/>
and my controller
public with sharing class eventinsert {
     public event eventDetail {get;set;}
     public DateTime startdatetime{get;set;} 
     public DateTime enddatetime{get;set;}
     public List<event> elist=new List<event>();

    public eventinsert(ApexPages.StandardController controller) {
       eventDetail=new event();
    }

public pagereference saveAndCongrat(){
                        system.debug('Venue__c'+eventDetail.Venue__c+'  '+UserInfo.getUserId()   );
                        system.debug('start'+ eventDetail.startdatetime);
                        system.debug('end'+eventDetail.enddatetime);
     elist=[select Id, Subject, isAllDayEvent, StartDateTime, EndDateTime,venue__c from Event where venue__c =:eventDetail.Venue__c];
 for(event e:elist){

    if(e.StartDateTime>= eventDetail.startdatetime)
{
//This works fine but cannot compare with <= with datetime
   //This line doesnt work startDateTime>=:startdatetime and  EndDateTime<=:enddatetime 
ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,' Event '+e.subject+' is booked for this time !'+' e.StartDateTime!!!'+e.StartDateTime+' eventDetail.startdatetime!!!'+eventDetail.startdatetime);
         ApexPages.addMessage(myMsg);
          system.debug('INSIDE +INSIDE');

        }
        else{
             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.error,' e.StartDateTime!!!'+e.StartDateTime+' eventDetail.startdatetime!!!'+eventDetail.startdatetime);
             ApexPages.addMessage(myMsg);
         }
  }      
 } 
return null;

}



 
Kishore B T 30Kishore B T 30
@James Loghry,

I have tried the snippet what you have given me, but it's the alternate form of startdatetime==e.startdatetime.
it's matching only equals condition.