1

I am about to start a trigger for a motel. When The guest's Arrival date and Departure date are inputted into their lead, it should calculate the cost of the room. The calendar dates are a separate object and each date has a "rate (price)" field. What I would like to do is summarize all the rate fields in between the Arrival and Departure Dates using a trigger.

What is the best way to summarize these rates? It should be an after update trigger.

4
  • 1
    Why should it be an "after update" trigger? This would best be served as a "before update" trigger, since you're updating the same record.
    – sfdcfox
    Aug 18, 2015 at 19:50
  • Because we have to manually input Arrival Date, Departure Date, and Room before saving the record. I figure that the trigger event should happen upon saving the record. If before update will work in those guidelines then I may try that as well. My concern is the summarization. Would you guide me on how that works? @sfdcfox Aug 18, 2015 at 19:53
  • 1
    You'd do well to learn what "before" and "after" means in trigger context, because it's pretty clear that you have a misunderstanding of how it works. However, I'd still be glad to help you out, of course.
    – sfdcfox
    Aug 18, 2015 at 19:55
  • 1
    Thank you @sfdcfox. It would be much appreciated. I will look back at my references for a better understanding as well. Aug 18, 2015 at 20:02

1 Answer 1

2

To calculate the prices, you'd have to gather together all the possible dates, then add them together for each record. Here's a starting point:

trigger calcSum on Lead (before insert, before update) {
    Set<Date> dates = new Set<Date>();
    Map<Date, Decimal> cost = new Map<Date, Decimal>();
    Lead[] validLeads = new Lead[0];
    for(Lead record: Trigger.new) {
        record.TotalDue__c = 0;
        if(record.ArrivalDate__c != null && record.DepartureDate__c != null) {
            validLeads.add(record);
            Date tempDate = record.ArrivalDate__c;
            while(tempDate < record.DepartureDate__c) {
                dates.add(tempDate);
                tempDate = tempDate.addDays(1);
            }
        }
    }
    for(Rate__c record:[SELECT Rate__c,Date__c FROM Rate__c WHERE Date__c IN :dates]) {
        cost.put(record.Date__c, record.Rate__c);
    }
    for(Lead record: validLeads) {
        Date tempDate = record.ArrivalDate__c;
        while(tempDate < record.DepartureDate__c) {
            Decimal dayRate = cost.get(tempDate);
            if(dayRate != null) {
                record.TotalDue__c += cost.get(tempDate);
            }
            tempDate = tempDate.addDays(1);
        }
    }
}

Of course, I presume there might be multiple motels, which will complicate the code (but not considerably so).

2
  • Wow thank you. This should take me very far. Yes there are multiple motels but I should be able to take it from here. Thank you for your thoroughness @sfdcfox Aug 18, 2015 at 20:11
  • 1
    With a few modifications, I have it working, perfectly. Thank you again! @sfdcfox! Aug 18, 2015 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .