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
mahesh p 54mahesh p 54 

test class for before insert

trigger Booking_5Appointments_perDay on Passport_Booking_Appointment__c (before insert) {
     if (Trigger.isBefore && Trigger.isInsert) {
         List<Id> passportSlotAvailabilities_ids = new List<Id>();
         for(Passport_Booking_Appointment__c j:Trigger.new)
         {
             passportSlotAvailabilities_ids.add(j.Passport_Slot_Availability__c);
         }
         
         List<Passport_Booking_Appointment__c> other_bookings = [SELECT Id, CreatedDate, CreatedById,Passport_Slot_Availability__c FROM Passport_Booking_Appointment__c WHERE Passport_Slot_Availability__c IN : passportSlotAvailabilities_ids];
         Map<id,integer> bookingAppointmentmap = new Map<id,integer> ();
         for (Passport_Booking_Appointment__c existent_record : other_bookings) {
            Date existent_record_created_date = existent_record.CreatedDate.date();
            if (existent_record.CreatedById == UserInfo.getUserId() && existent_record_created_date == Date.today()) {
                if(bookingAppointmentmap.get(existent_record.Passport_Slot_Availability__c) == null) {
                    bookingAppointmentmap.put(existent_record.Passport_Slot_Availability__c,1);
                } else {
                    bookingAppointmentmap.put(existent_record.Passport_Slot_Availability__c,bookingAppointmentmap.get(existent_record.Passport_Slot_Availability__c)+1);
                }
            }
         }
         
         for (Passport_Booking_Appointment__c j : Trigger.new) {
            if(bookingAppointmentmap != null && bookingAppointmentmap.get(j.Passport_Slot_Availability__c) >=5) {
                j.addError('Cannot create Booking Appointment for your desired slot please come back tomorrow.');
            }
        }
    }

}
can i get the test class for before insert trigger
v varaprasadv varaprasad
Hi Mahesh,

Try This :
 
@isTest
public class TestClassTemplate {
    public static testmethod void methodTest(){
        //Create Passport_Slot_Availability__c data
        
		Passport_Slot_Availability__c psa = new Passport_Slot_Availability__c();
		psa.name = 'Test';
		//Add all required fields
		insert psa;
		
		//Insert 5 Passport_Booking_Appointment__c  records
		list<Passport_Booking_Appointment__c> lstPBA = new list<Passport_Booking_Appointment__c>();
		
		for(integer i = 0; i <6; i++){
		  Passport_Booking_Appointment__c pba = new Passport_Booking_Appointment__c();
		  pba.name = 'pba'+i;
		  pba.Passport_Slot_Availability__c = psa.id;
		  //Add reaming fields
		  lstPBA.add(pba);
		}
      try{
	  insert lstPBA
	  }catch(DMLexception e){
	     system.debug('Following error message occured '+.getMessage());
	  }
    }

}

 

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
mahesh p 54mahesh p 54
It is showing 62 percent code coverage could you explain what does it mean in this case