There are 2 Custom objects Passport_Slot_Availability__c and Passport_Booking_Appointment__c.They both have master detail relationship. where Passport_Slot_Availability__c is master and Passport_Booking_Appointment__c is detail.

Requirement: For a day only 5 passport bookings should be created by the users for a particular Passport Slot. On the same day when the 6th user is trying to create another passport booking for the same passport slot as that of 5 users, error message should be thrown. But on the next day the user should be able to create a new passport booking for that same passport slot if available.

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];
     for (Passport_Booking_Appointment__c j : Trigger.new) {
        for (Passport_Booking_Appointment__c existent_record : other_bookings) {
            if (j.Passport_Slot_Availability__c == existent_record.Passport_Slot_Availability__c && j.CreatedById == UserInfo.getUserId()) {
                Date existent_record_created_date = existent_record.CreatedDate.date();

                if (existent_record_created_date == Date.today()) {
                    j.addError('Cannot create Booking Appointment because limit has exceeded for today.');
                }
            }
        }
    }
}

} In the passport booking there is Master-Detail(Passport_Slot_Availability) field.If i choose 9 to 12 slot only 5 users can book an appointment per day. how to achieve the above?

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.