• Suman Humane 5
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 5
    Replies
Hi everyone,

I am trying to write a trigger for a requirement where a new record should not be created if the dates overlap. There are 2 objects Event1 (Campaign renamed) and Attendee. There is a junction object Event Attendee which allows an attendee to register for an Event. If an attendee is already registered for an event then he/she should not be able to register for any other event which falls on those dates or whose start or end dates overlap the already registered event dates. When I execute my trigger, I get an error for every event-attendee record instead of only for those which satisfies the condition. Please help me with the trigger.
This is the trigger:
trigger Eventdatesoverlapprevention on Event_Attendee__c (before insert,before update) {
    set<string> att = new set <string>();
    set<string> evnt = new set <string>();
    set<date> stdt = new set <date>();
    set<date> endt = new set <date>();
    //getting attendee, event, start and end dates in the event attendee records in the set defined above.
    for (event_attendee__c ea:trigger.new){
        
        stdt.add(ea.event_start_date__c);
        endt.add(ea.event_end_date__C);        
        att.add(ea.Attendee__c);
        evnt.add(ea.event1__C);          
    }
    //querying for attendees that are a part of the trigger.new list
    list <attendee__C> attendee = [Select id from attendee__c where id in:att];
    
    //querying for events (campaign is renamed to event1) that are a part of the trigger.new list
    list<campaign> event = [Select id from campaign where id in:evnt];
    
    // querying the list of event attendees where the attendees, events, start date and end date is present in the trigger.new list
    list<aggregateresult> evntovrlap1  = [SELECT count(id) from event_attendee__C 
                                          where attendee__c in: att and event1__C in: evnt and event_Start_Date__C in: stdt and event_End_Date__C in: endt];
    //querying the list of event attendees where the start date is higher than the start date in trigger.new list 
    //and end date is less than the end date in the trigger.new list.
    list<aggregateresult> evntovrlap2 = [SELECT count(id) from event_attendee__C 
                                         where attendee__c in: att and event1__C in: evnt and event_Start_Date__C >=: stdt and event_end_Date__C <=: endt];
    //querying the list of event attendees where the start date is less than the start date in trigger.new list 
    //and end date is higher than the end date in the trigger.new list.
    list<aggregateresult> evntovrlap3 = [SELECT count(id) from event_attendee__C 
                                         where attendee__c in: att and event1__C in: evnt and event_start_Date__C <=: stdt and event_end_Date__C >=: endt];
    
    //for every attendee record and for every event attendee record, if attendee record finds a match in the event attendee record 
    //and if the above counts are greater than 1 then, show an error message.
    for(attendee__c a : attendee){
        for(campaign e : event){
            for(event_attendee__c ea:trigger.new){
                If(a.id==ea.Attendee__c && e.Id == ea.Event1__c){
                    If(evntovrlap1.size()>0 || evntovrlap2.size()>0 || evntovrlap3.size()>0){
                        ea.adderror('Attendee is already registered for these dates');
                    }
                }
            }  
        }
    }
    
}

 
Hi everyone,

I am trying to write a trigger for a requirement where a new record should not be created if the dates overlap. There are 2 objects Event1 (Campaign renamed) and Attendee. There is a junction object Event Attendee which allows an attendee to register for an Event. If an attendee is already registered for an event then he/she should not be able to register for any other event which falls on those dates or whose start or end dates overlap the already registered event dates. When I execute my trigger, I get an error for every event-attendee record instead of only for those which satisfies the condition. Please help me with the trigger.
This is the trigger:
 
@isTest
private class venueupdatetriggerhandlerTest{
     static testmethod void TestRecordCreate(){
     
            venue__c Ven = new venue__c();           
            campaign evn = new campaign();
             Ven.Name = 'test1';
             Ven.Location__c = 'Pune';
             Ven.Capacity__c = 100;
            insert Ven;
         system.debug('Venue created with id'+ ven.Id);
            evn.name = 'Test Event';
            insert evn;
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven.id;
            sesstest.Event1__c = evn.id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            
                 
         system.assertequals(1, [select count() from session__c where venue__c=:ven.id] );
        }
         static testmethod void TestRecordUpdate(){
             campaign evn = new campaign();
             evn.name = 'Test Event 2';
             insert evn;
            List<venue__c> Ven = new List<venue__c>();
            venue__c Ven1 = new venue__c();
            Ven1 .Name = 'test1';
             Ven1.Location__c = 'Pune';
             Ven1.Capacity__c = 100;
            Ven.add(Ven1);
            venue__c Ven2 = new venue__c();
            Ven2.Name = 'test2';
             Ven2.Location__c = 'Pune';
             Ven2.Capacity__c = 100;
            Ven.add(Ven2);
            insert Ven;
            
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven1.id;
            sesstest.Event1__c = evn.Id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            sesstest .venue__c = Ven2.id;
            update sesstest;
            system.assertequals(1, [select count() from session__c where venue__c=:ven2.id] );
            system.assertequals(0, [select count() from session__c where venue__c=:ven1.id] );
            
        }
        static testmethod void TestRecordAfterDelete(){
            campaign evn = new campaign();
            evn.name = 'Test Event 3';
            insert evn;
            List<venue__c> Ven = new List<venue__c>();
            venue__c Ven1 = new venue__c();
            Ven1 .Name = 'test1';
            Ven1.Location__c = 'Pune';
             Ven1.Capacity__c = 100;
            Ven.add(Ven1);
            insert Ven;
            
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven1.id;
            sesstest.Event1__c = evn.Id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            Delete sesstest;
            system.assertequals(0, [select count() from session__c where venue__c=:ven1.id] );
            
            
        }
        static testmethod void TestRecordAfterUndelete(){
            campaign evn = new campaign();
            evn.name = 'Test Event 4';
            insert evn;
            List<venue__c> Ven = new List<venue__c>();
            venue__c Ven1 = new venue__c();
            Ven1 .Name = 'test1';
            Ven1.Location__c = 'Pune';
             Ven1.Capacity__c = 100;
            Ven.add(Ven1);
            insert Ven;
            
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven1.id;
            sesstest.Event1__c = evn.id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            Delete sesstest;
            Undelete sesstest;
            system.assertequals(1, [select count() from session__c where venue__c=:ven1.id] );
            
            
        }
        
 }
Hi everyone,
I am pretty new to Salesforce Development. I have a simple requirement for where in I need to update a field with the count of child records associated with that object. Since the relationship is that of a look up relationship, I had to achieve this with a trigger. I was able to achieve the requirement. But what I am not able to write is test class for it. Can you please help? 
Here is the trigger and helper code :
Trigger:
trigger UpdtNumofsessions on Session__c (after insert, after update, after delete, after undelete) {
   venueupdatetriggerhandler venupdate = new venueupdatetriggerhandler();
    
    if(trigger.isinsert && trigger.isafter){
        venupdate.OnAfterInsert(trigger.new);
    }
   
    if(trigger.isupdate && trigger.isafter){
        venupdate.OnAfterUpdate(trigger.new);
    }
    
    if(trigger.isdelete && trigger.isafter){
        venupdate.OnAfterDelete(trigger.old);
    }
    
    if(trigger.isundelete && trigger.isafter){
        venupdate.OnAfterInsert(trigger.old);
    }
}

Helper Code:
 
public with sharing class venueupdatetriggerhandler {
 // update the number of sessions on venue when new session records are inserted from trigger
  public void OnAfterInsert(List<session__c> newRecords){
    updatenumberofsessions(newRecords); 
  }
  // update the primary country when records are updated from trigger  
  public void OnAfterUpdate(List<session__c> updatedRecords){
    updatenumberofsessions(updatedRecords); 
  }
 
    public void OnafterDelete(List <session__c> oldRecords){
        updatenumberofsessions(oldRecords);
    }
    
    public void OnafterUndelete(List<Session__c> oldRecords){
        updatenumberofsessions(oldRecords);
    }
  // updates the sales order with the primary purchased country for the item
    private void updatenumberofsessions(List<Session__c> newRecords){
  // create a list to hold session records whose venue is not null
     List<id> ses = new list <id>();
        for (session__c s:newRecords){
            if(s.venue__C!=null){
                ses.add(s.venue__C);
            }
            
       // list to query all the venue records in the database which is a part of the LIST 'ses'     
       list<venue__C> ven =[select id, no_of_sessions__C,(select id from sessions__r) from venue__C where id in:ses];    
  
            //loops over the query result set of venue and updates the no. of sessions field with the count of the related session records
            for(venue__c v:ven){
                v.no_of_sessions__c=v.sessions__r.size();
                }
            Update ven;
    } 
    }
    
}

Thank you so much.
Hi everyone,

I am trying to write a trigger for a requirement where a new record should not be created if the dates overlap. There are 2 objects Event1 (Campaign renamed) and Attendee. There is a junction object Event Attendee which allows an attendee to register for an Event. If an attendee is already registered for an event then he/she should not be able to register for any other event which falls on those dates or whose start or end dates overlap the already registered event dates. When I execute my trigger, I get an error for every event-attendee record instead of only for those which satisfies the condition. Please help me with the trigger.
This is the trigger:
 
@isTest
private class venueupdatetriggerhandlerTest{
     static testmethod void TestRecordCreate(){
     
            venue__c Ven = new venue__c();           
            campaign evn = new campaign();
             Ven.Name = 'test1';
             Ven.Location__c = 'Pune';
             Ven.Capacity__c = 100;
            insert Ven;
         system.debug('Venue created with id'+ ven.Id);
            evn.name = 'Test Event';
            insert evn;
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven.id;
            sesstest.Event1__c = evn.id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            
                 
         system.assertequals(1, [select count() from session__c where venue__c=:ven.id] );
        }
         static testmethod void TestRecordUpdate(){
             campaign evn = new campaign();
             evn.name = 'Test Event 2';
             insert evn;
            List<venue__c> Ven = new List<venue__c>();
            venue__c Ven1 = new venue__c();
            Ven1 .Name = 'test1';
             Ven1.Location__c = 'Pune';
             Ven1.Capacity__c = 100;
            Ven.add(Ven1);
            venue__c Ven2 = new venue__c();
            Ven2.Name = 'test2';
             Ven2.Location__c = 'Pune';
             Ven2.Capacity__c = 100;
            Ven.add(Ven2);
            insert Ven;
            
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven1.id;
            sesstest.Event1__c = evn.Id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            sesstest .venue__c = Ven2.id;
            update sesstest;
            system.assertequals(1, [select count() from session__c where venue__c=:ven2.id] );
            system.assertequals(0, [select count() from session__c where venue__c=:ven1.id] );
            
        }
        static testmethod void TestRecordAfterDelete(){
            campaign evn = new campaign();
            evn.name = 'Test Event 3';
            insert evn;
            List<venue__c> Ven = new List<venue__c>();
            venue__c Ven1 = new venue__c();
            Ven1 .Name = 'test1';
            Ven1.Location__c = 'Pune';
             Ven1.Capacity__c = 100;
            Ven.add(Ven1);
            insert Ven;
            
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven1.id;
            sesstest.Event1__c = evn.Id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            Delete sesstest;
            system.assertequals(0, [select count() from session__c where venue__c=:ven1.id] );
            
            
        }
        static testmethod void TestRecordAfterUndelete(){
            campaign evn = new campaign();
            evn.name = 'Test Event 4';
            insert evn;
            List<venue__c> Ven = new List<venue__c>();
            venue__c Ven1 = new venue__c();
            Ven1 .Name = 'test1';
            Ven1.Location__c = 'Pune';
             Ven1.Capacity__c = 100;
            Ven.add(Ven1);
            insert Ven;
            
            Session__c sesstest = new   Session__c();
            sesstest .Name = 'test2';
            sesstest .venue__c = Ven1.id;
            sesstest.Event1__c = evn.id;
            sesstest.Start_Time__c=datetime.newInstance(2019, 4, 20, 12, 30, 0);
            sesstest.End_Time__c=datetime.newInstance(2019, 4, 20, 12, 40, 0);
            sesstest.Level__c = 'Beginner';
            sesstest.Type__c = 'Workshop';
            insert sesstest ;
            Delete sesstest;
            Undelete sesstest;
            system.assertequals(1, [select count() from session__c where venue__c=:ven1.id] );
            
            
        }
        
 }
Hi everyone,
I am pretty new to Salesforce Development. I have a simple requirement for where in I need to update a field with the count of child records associated with that object. Since the relationship is that of a look up relationship, I had to achieve this with a trigger. I was able to achieve the requirement. But what I am not able to write is test class for it. Can you please help? 
Here is the trigger and helper code :
Trigger:
trigger UpdtNumofsessions on Session__c (after insert, after update, after delete, after undelete) {
   venueupdatetriggerhandler venupdate = new venueupdatetriggerhandler();
    
    if(trigger.isinsert && trigger.isafter){
        venupdate.OnAfterInsert(trigger.new);
    }
   
    if(trigger.isupdate && trigger.isafter){
        venupdate.OnAfterUpdate(trigger.new);
    }
    
    if(trigger.isdelete && trigger.isafter){
        venupdate.OnAfterDelete(trigger.old);
    }
    
    if(trigger.isundelete && trigger.isafter){
        venupdate.OnAfterInsert(trigger.old);
    }
}

Helper Code:
 
public with sharing class venueupdatetriggerhandler {
 // update the number of sessions on venue when new session records are inserted from trigger
  public void OnAfterInsert(List<session__c> newRecords){
    updatenumberofsessions(newRecords); 
  }
  // update the primary country when records are updated from trigger  
  public void OnAfterUpdate(List<session__c> updatedRecords){
    updatenumberofsessions(updatedRecords); 
  }
 
    public void OnafterDelete(List <session__c> oldRecords){
        updatenumberofsessions(oldRecords);
    }
    
    public void OnafterUndelete(List<Session__c> oldRecords){
        updatenumberofsessions(oldRecords);
    }
  // updates the sales order with the primary purchased country for the item
    private void updatenumberofsessions(List<Session__c> newRecords){
  // create a list to hold session records whose venue is not null
     List<id> ses = new list <id>();
        for (session__c s:newRecords){
            if(s.venue__C!=null){
                ses.add(s.venue__C);
            }
            
       // list to query all the venue records in the database which is a part of the LIST 'ses'     
       list<venue__C> ven =[select id, no_of_sessions__C,(select id from sessions__r) from venue__C where id in:ses];    
  
            //loops over the query result set of venue and updates the no. of sessions field with the count of the related session records
            for(venue__c v:ven){
                v.no_of_sessions__c=v.sessions__r.size();
                }
            Update ven;
    } 
    }
    
}

Thank you so much.