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
bohemianguy100bohemianguy100 

Salesforce trigger addError method

I'm creating an event management application  that allows users to register for events.  Each event is a 1-day event that can be comprised of x number of sessions that never overlap.

I have a master-detail relationship setup between the event table (master object) and the event session table (detail object).  This allows x number of sessios for an event.

My question is how I can validate that the sessions never overlap.

In the event session table I have two date/time fields Start Time and End Time.

The only way I can think to validate if the sessions do not overlap is to use a trigger with the addError() method.  However, how can I compare the times of each session for a specific event and determine if the sessions overlap?
 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Steps to do it

 

1)Write a trigger on session before insert

2)Find out all the existing sessions for this event which current session associated with

3)Loop over all the the Existing Sessions and validate dates

 

try this 

 

trigger validateoverlap on Session__c(before insert)
{
Set<ID> eventID = set<ID>();
for(Session__c sNew : trigger.new)
{
     //Eent Reference field
     eventID.add(sNew.Event__c);
}
List<Session__c> ExistingSessionList = [Select StartDate__c , EndDate__c from Session__c where event__c in : eventID];

for(Session__c sNew : trigger.new)
{

  for(Session__c sOld : ExistingSessionList)
   {
    if(sNew.StartDate__c >= sOld.StartDate__c && sNew.StartDate__c  <= sOld.EndDate__c)
    {
      sNew.addError('Overlap Session');
    }
   }
}
}

 This will not work for bulk data load handling

All Answers

Shashikant SharmaShashikant Sharma

Steps to do it

 

1)Write a trigger on session before insert

2)Find out all the existing sessions for this event which current session associated with

3)Loop over all the the Existing Sessions and validate dates

 

try this 

 

trigger validateoverlap on Session__c(before insert)
{
Set<ID> eventID = set<ID>();
for(Session__c sNew : trigger.new)
{
     //Eent Reference field
     eventID.add(sNew.Event__c);
}
List<Session__c> ExistingSessionList = [Select StartDate__c , EndDate__c from Session__c where event__c in : eventID];

for(Session__c sNew : trigger.new)
{

  for(Session__c sOld : ExistingSessionList)
   {
    if(sNew.StartDate__c >= sOld.StartDate__c && sNew.StartDate__c  <= sOld.EndDate__c)
    {
      sNew.addError('Overlap Session');
    }
   }
}
}

 This will not work for bulk data load handling

This was selected as the best answer
dharmendra kushwah 4dharmendra kushwah 4

I am from non-coding background, know little bit of Salesforce Admin. But i want to become a Salesforce Developer. I am searching ways in internet but confused which one is right one. Can I become Apex, VF developer on my own????? Please please help. If possible give me call 9769540274

Also with my little understanding, just suggesting that I think we also need to put one more condition in IF statement to verify session overlap????

if( ( (((sNew.StartDate__c >= sOld.StartDate__c && sNew.StartDate__c  <= sOld.EndDate__c)||(sNew.startDate__C <= sOld.startDate__c && sNew.EndDate__C>=sOld.startDate__c))