Skip to main content
I have below requirment - 

Train and Coach are 2 objects. Where I need to build logic based on train schedule and availibility. From UI side admin can add the coach to the train. 

Train (Parent Object - From, To, Timings)

Coach (Child Object - From, To, Timings) Field is there. 

Now from UI side when user book the tickets. based on given 3 fields I need to add coachs in the train. lets say next week on monday there will 3 trains are schedule from X to Y location. now today we see many passanger book the train from X to Y location. so basically, based on timings I need to add number of coaches in next week train so I can accomodate all customers. Its not like 1 train run with 5-6 coach, while other train runs with 30-35 coach. there has to balance. 

Please let me know if need further clarification in scenario. 
2 respuestas
  1. 15 feb 2023, 6:48 a.m.
    Firstly, you can define the Train and Coach objects in Salesforce as custom objects with the appropriate fields.

    Next, you can create a Visualforce page to display the available coaches for a given train. Here's an example of the Visualforce page

    <apex:page controller="TrainController">

        <apex:form >

            <apex:pageBlock title="Select Train">

                <apex:pageBlockSection>

                    <apex:inputField value="{!selectedTrain}" />

                </apex:pageBlockSection>

                <apex:pageBlockButtons >

                    <apex:commandButton action="{!showAvailableCoaches}" value="Show Available Coaches" rerender="coachList" />

                </apex:pageBlockButtons>

            </apex:pageBlock>

            <apex:outputPanel id="coachList">

                <apex:pageBlock title="Available Coaches">

                    <apex:pageBlockTable value="{!availableCoaches}" var="coach">

                        <apex:column value="{!coach.Name}" />

                    </apex:pageBlockTable>

                </apex:pageBlock>

            </apex:outputPanel>

        </apex:form>

    </apex:page>

    TrainController Apex class is used to display the available coaches for a selected train.

    public class TrainController {

        public Train__c selectedTrain { get; set; }

        public List<Coach__c> availableCoaches { get; set; }

     

        public void showAvailableCoaches() {

            // Get the available coaches for the selected train

            availableCoaches = [SELECT Id, Name FROM Coach__c WHERE Train__c = :selectedTrain.Id];

        }

    }

    trigger on the booking object that listens for new bookings and updates the related train record with the number of coaches needed based on the available coaches and the number of passengers.

    trigger BookingTrigger on Booking__c (after insert) {

        List<Train__c> trainsToUpdate = new List<Train__c>();

        Set<Id> trainIds = new Set<Id>();

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

     

        // Aggregate the passenger count for each train

        for (Booking__c booking : Trigger.new) {

            trainIds.add(booking.Train__c);

            if (trainToPassengerCount.containsKey(booking.Train__c)) {

                trainToPassengerCount.put(booking.Train__c, trainToPassengerCount.get(booking.Train__c) + booking.Passenger_Count__c);

            } else {

                trainToPassengerCount.put(booking.Train__c, booking.Passenger_Count__c);

            }

        }

     

        // Query for the available coaches for each train

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

        for (Coach__c coach : [SELECT Id, Train__c, Seats__c FROM Coach__c WHERE Train__c IN :trainIds]) {

            if (trainToCoachCount.containsKey(coach.Train__c)) {

                trainToCoachCount.put(coach.Train__c, trainToCoachCount.get(coach.Train__c) + coach.Seats__c);

            } else {

                trainToCoachCount.put(coach.Train__c, coach.Seats__c);

            }

        }

     

        // Update the train records with the number of coaches needed

        for (Id trainId : trainIds) {

            if (trainToPassengerCount.containsKey(trainId) && trainToCoachCount.containsKey(trainId)) {

                Integer passengerCount = trainToPassengerCount.get(trainId);

                Integer coachCount = trainToCoachCount.get(trainId) / 50;

                Integer coachesNeeded = (passengerCount / 50) + 1;

     

                if (coachCount < coachesNeeded) {

                    Integer coachesToAdd = coachesNeeded - coachCount;

                    Train__c train = new Train__c(Id = trainId, Coaches__c = coachCount + coachesToAdd);

                    trainsToUpdate.add(train);

                }

            }

        }

     

        if (trainsToUpdate.size() > 0) {

            update trainsToUpdate;

        }

    }

    If you find my answer helpful, Please mark it as the best answer. Thanks!