Hotel Reservation

Easy Stay

Hotel Reservation Project

Project Overview

The Easy Stay Hotel Management System is built on the Salesforce platform using Apex and custom objects. It automates hotel and reservation management by: 

  • Creating hotels and tracking room availability. 
  • Managing guest reservations. 
  • Generating unique confirmation codes for reservations. 
  • Automatically updating available room counts after booking. 

2. Objects and Fields

Hotel Object (Hotel__c)

Field Label API Name Data Type Description
Hotel Name
Name
Text
The name of the hotel (e.g., “Grand Palace Hotel”).
Total Rooms
Total_Rooms__c
Number
The total number of rooms available in the hotel when it is created.
Available Rooms
Available_Rooms__c
Number
The current number of rooms available for booking. This field is automatically updated when reservations are made.
Objects and Fields

Reservation Object (Reservation__c)

Field Label API Name Data Type Description
Guest Name
Name
Text
The name of the guest making the reservation.
Check-In Date
Check_In__c
Date
The date when the guest is scheduled to check into the hotel.
Check-Out Date
Check_Out__c
Date
The date when the guest is scheduled to check out of the hotel.
Confirmation Code
Confirmation_Code__c
Text
A unique system-generated code assigned to each reservation (e.g., CONF1234).
Hotel
Hotel__c
Lookup
A lookup relationship to the Hotel__c object, linking the reservation to a specific hotel.
Reservation Object

3. Business Logic Implemented

  • Generate Confirmation Code: Automatically generate a unique confirmation code for each new reservation. 
  • Update Available Rooms: Reduce available rooms in the related hotel when a new reservation is made. 

4. Apex Trigger

This trigger runs on the Reservation__c object and has before insert and after insert events. 

Navigate to Setup 

  • Click the ⚙️ Gear Icon in the top-right corner. 
  • Select Setup. 

Search for Apex Triggers 

  • In the Quick Find box, type Apex Triggers. 
  • Click on Apex Triggers under Custom Code. 

Click on “New” 

  • On the Apex Trigger page, click the New button. 

Select the Object 

  • Choose the object where the trigger should run. 
  • In this case: Reservation__c. 

Define Trigger Events 

  • Specify when the trigger should fire (e.g., before insert, after insert). 
  • Example: 

Reservation Trigger trigger

trigger ReservationTrigger on Reservation__c (before insert, after insert) { 

 

    // Before Insert: Generate Confirmation Code 

    if (Trigger.isBefore && Trigger.isInsert) { 

        for (Reservation__c res : Trigger.new) { 

            String randomNum = String.valueOf(Math.abs(Crypto.getRandomInteger())).substring(0,4); 

            String code = ‘CONF’ + randomNum; // Example: CONF1234 

            res.Confirmation_Code__c = code; 

        } 

    } 

 

    // After Insert: Update Available Rooms in Hotel 

    if(Trigger.isAfter && Trigger.isInsert){ 

        Map<Id, Integer> hotelReservationCount = new Map<Id, Integer>(); 

 

        // Count reservations for each Hotel 

        for(Reservation__c res : Trigger.new){ 

            if(res.Hotel__c != null){ 

                if(hotelReservationCount.containsKey(res.Hotel__c)){ 

                    hotelReservationCount.put(res.Hotel__c, hotelReservationCount.get(res.Hotel__c) + 1); 

                } else { 

                    hotelReservationCount.put(res.Hotel__c, 1); 

                } 

            } 

        } 

 

        // Fetch related Hotels 

        List<Hotel__c> hotelsToUpdate = [SELECT Id, Available_Rooms__c FROM Hotel__c WHERE Id IN :hotelReservationCount.keySet()]; 

 

        for(Hotel__c hotel : hotelsToUpdate){ 

            Integer count = hotelReservationCount.get(hotel.Id); 

            hotel.Available_Rooms__c = hotel.Available_Rooms__c – count; 

        } 

 

        update hotelsToUpdate; 

    } 

} 

Explanation:

  • Trigger.isBefore && Trigger.isInsert → This block runs before the reservation is saved to the database. 
  • Loops through all new reservations in Trigger.new. 
  • Generates a random 4-digit number using Crypto.getRandomInteger() and prepends “CONF” to create a confirmation code. 
  • Sets this code into the field Confirmation_Code__c of the reservation. 
  • Trigger.isAfter && Trigger.isInsert → Runs after the reservation record is saved. 
  • Creates a map to count how many reservations are booked per hotel. 
  • Loops through reservations in Trigger.new and populates the map with hotel IDs and reservation counts. 
  • Queries all the hotels related to these reservations. 
  • Subtracts the number of new reservations from the hotel’s Available_Rooms__c.Updates the hotels in Salesforce. 
  • This ensures that available rooms are automatically updated after new reservations are made. 

5. Test Class

A test class ensures your trigger works correctly and Salesforce deployment requirements are met. 

ReservationTriggerTest

@isTest 

public class ReservationTriggerTest { 

     

    public static String generateConfirmationCode(Reservation__c res) { 

        String prefix = ‘RES’; 

        String datePart = String.valueOf(System.today()). replace(‘-‘, ”); 

        String randomPart = String.valueOf(Math.mod(Crypto.getRandomInteger(), 10000)); 

        return prefix + ‘-‘ + datePart + ‘-‘ + randomPart; 

    } 

 

    @isTest 

    static void testReservationTrigger() { 

 

        // Create a Hotel record 

        Hotel__c hotel = new Hotel__c( 

            Name = ‘Test Hotel’, 

            Total_Rooms__c = 10, 

            Available_Rooms__c=10 

        ); 

        insert hotel; 

 

        // Create a Reservation record 

        Reservation__c reservation = new Reservation__c( 

            Guest_Name__c = ‘Test Guest’, 

            Check_In__c = Date.today(), 

            Check_Out__c = Date.today().addDays(2), 

            Hotel__c = hotel.Id 

        ); 

        insert reservation; 

 

        // Validate Confirmation Code 

        Reservation__c insertedRes = [SELECT Confirmation_Code__c FROM Reservation__c WHERE Id = :reservation.Id]; 

        System.assertNotEquals(null, insertedRes.Confirmation_Code__c, ‘Confirmation code should not be null.’); 

 

        // Validate Available Rooms 

        Hotel__c updatedHotel = [SELECT Available_Rooms__c FROM Hotel__c WHERE Id = :hotel.Id]; 

        System.assertEquals(9, updatedHotel.Available_Rooms__c, ‘Available rooms should decrease by 1.’); 

    } 

} 

Explanation:

1.@isTest → Marks this class as a test class. Salesforce does not count test code toward org limits.

2.Creating a hotel record → Sets up a hotel with 10 available rooms.

3.Creating a reservation record → Assigns the reservation to the hotel.

4.Insert operation → Triggers your ReservationTrigger logic.

5.Validates confirmation code → Checks that Confirmation_Code__c is not null.

6.Validates available rooms → Checks that the hotel’s Available_Rooms__c has decreased by 1 (from 10 → 9).

This ensures that both the before-insert logic (confirmation code generation) and the after-insert logic (room update) work correctly.

Test Class

6. Key Features

  • Automatically generates a confirmation code for each reservation. 
  • Reduces Available Rooms dynamically upon booking. 
  • Ensures data consistency using Apex Triggers. 
  • Covered with Unit Test Class for 100% code coverage. 

7. Conclusion

The Easy Stay Hotel Management System successfully demonstrates how Salesforce can be leveraged to build a streamlined hotel and reservation management application. By using custom objects, Apex triggers, and automated business logic, the system ensures that hotel room availability and guest reservations remain accurate and consistent in real-time. 

Key benefits include: 

  • Automation of critical tasks such as confirmation code generation and room availability updates. 
  • Improved accuracy by eliminating manual updates and reducing the risk of overbooking. 
  • Scalability as the system can easily be extended with additional features like payment tracking, cancellation handling, or reporting dashboards. 
  • Reliability through the use of test classes that validate both the trigger logic and data integrity. 

In conclusion, the project provides a robust, automated, and extensible solution for hotel reservation management, showcasing the power of Salesforce Apex development and best practices in trigger design and testing. 

Handles the final stage of billing and discharge, including discounts and payment tracking after a patient visit

Course Video