2 件の回答
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!