So this weekend, following the Giants punching their trip to the World Series (Fear the Beard), I officially registered for Dreamforce 2010. I advise that everyone who will be attending this year to create their Chatter profile and sign-up for sessions using the Agenda Builder as soon as possible. Also any recommendations would be appreciated since it will be my first time attending any type of cloud computing event.
Ok so let’s jump into Apex triggers. If you recall from my previous post, we will use Apex Code to create these triggers and complete our scheduled payments. Apex Triggers operate like most standard database triggers, you define the trigger name for when the trigger fires and write code to act on the event. This can be before or after insert, update, or delete.
For our application we will be writing two triggers. The first trigger will create scheduled payment objects for an invoice when the invoice is created, and the second trigger will update the invoice's status when the status of a scheduled payment changes. Begin by opening the Scheduled Invoice object definition by clicking ‘Setup’, then ‘Build’, and finally ‘Objects’. At the bottom of the page you will find the ‘Triggers’ section, at which point you will select ‘New’. This will bring up the Apex Code editor, which you will see in the following screen shot:
For our first Trigger we need to replace the blank template by copying the following code and pasting it into the editor:
// After a new entry is insert into the Scheduled Invoice custom object table
// this trigger will fire and create all the scheduled payment objects for
// that invoice.
trigger ScheduledInvoice_After_Insert on Scheduled_Invoice__c
( after insert )
{
// Apex Code's maximum batch size for updates, inserts
// and deletes. The final key word indicates a constant.
final Integer MAX_BATCH_SIZE = 200;
List<Scheduled_Payment__c> newPayments;
Date endDate;
Boolean isMonthly = false;
Integer daysInPeriod = 0;
Integer numberOfPeriods = 0;
Double paymentPerPeriod = 0;
Date dueDate;
newPayments = new List<Scheduled_Payment__c>();
// Loop through the new invoices and create scheduled payments
// for each one depending on their settings.
// The trigger object has two properties containing lists of updated
// objects, new and old. For insert triggers, only the new property
// has values.
// The for loop below is similar to a foreach loop in C#. The C#
// equivalent would be foreach ( Object var in varArray )
for ( Scheduled_Invoice__c invoice : trigger.new )
{
// Convert the Duration in Months to an actual number of
// periods. If the schedule type is monthly, then the number
// of periods is equal to the duration in months, otherwise
// you need to calculate it.
if ( invoice.Schedule_Type__c == 'Monthly' )
{
numberOfPeriods = invoice.Duration_in_Months__c.intValue();
isMonthly = true;
}
else
{
Integer numberOfDays;
// For weekly and bi-weekly, calculate the end date first.
endDate = invoice.Effective_Date__c.addMonths(
invoice.Duration_in_Months__c.intValue() );
// Next, get the number of days.
numberOfDays = invoice.Effective_Date__c.daysBetween(
endDate );
// Setup the number of days in the period for weekly and
// bi-weekly payments.
if ( invoice.Schedule_Type__c == 'Weekly' )
daysInPeriod = 7;
else
daysInPeriod = 14;
// Divide the number of days by the number of days in the
// period to get the number of periods.
numberOfPeriods = Math.floor( numberOfDays /
daysInPeriod ).intValue();
}
// Calculate the payment per period.
paymentPerPeriod = invoice.Total_Amount_Due__c /
numberOfPeriods;
// Generate a new scheduled payment object for each
// period of the invoice.
for ( Integer x = 0; x < numberOfPeriods; x++ )
{
// Calcualte the due date for this period.
if ( isMonthly )
{
dueDate = invoice.Effective_Date__c.addMonths( x + 1 );
}
else
{
dueDate = invoice.Effective_Date__c.addDays(
daysInPeriod * ( x + 1 ) );
}
// Create the new scheduled payment object and add it
// to the list of new payment objects to be inserted.
// For SObjects (Salesforce Objects) you can specify the
// property values in the constructor by using the syntax
// <Property Name> = <value>.
newPayments.add( new Scheduled_Payment__c(
Invoice__c = invoice.Id, Status__c = 'Pending',
Due_Date__c = dueDate,
Amount_Due__c = paymentPerPeriod ) );
// If you've hit the maximum batch size, insert the new
// data
and clear the list.
if ( newPayments.size() == MAX_BATCH_SIZE )
{
Database.insert( newPayments );
newPayments.clear();
}
}
}
// If you still have data you need to insert, insert it now.
if ( newPayments.size() > 0 )
{
Database.insert( newPayments );
}
}
}
Once again the final step is to simply click ‘Save’. Normally this code would be placed in a completely separate Apex Code class editor, but in order to be concise we will include it directly into the trigger. We will go over Apex Code classes when we learn about VisualForce next week. It will make more sense in that context.
In the next post we will create the second Trigger and test both of them when they are in place.