The sample application in this section includes traditional Salesforce functionality blended with Apex. Many of the syntactic and semantic features of Apex, along with common idioms, are illustrated in this application.
In this sample application, the user creates a new shipping invoice, or order, and then adds items to the invoice. The total amount for the order, including shipping cost, is automatically calculated and updated based on the items added or deleted from the invoice.
This sample application uses two new objects: Item and Shipping_invoice.
The fields in the Item custom object include:
Name | Type | Description |
---|---|---|
Name | String | The name of the item |
Price | Currency | The price of the item |
Quantity | Number | The number of items in the order |
Weight | Number | The weight of the item, used to calculate shipping costs |
Shipping_invoice | Master-Detail (shipping_invoice) | The order this item is associated with |
The fields in the Shipping_invoice custom object include:
Name | Type | Description |
---|---|---|
Name | String | The name of the shipping invoice/order |
Subtotal | Currency | The subtotal |
GrandTotal | Currency | The total amount, including tax and shipping |
Shipping | Currency | The amount charged for shipping (assumes $0.75 per pound) |
ShippingDiscount | Currency | Only applied once when subtotal amount reaches $100 |
Tax | Currency | The amount of tax (assumes 9.25%) |
TotalWeight | Number | The total weight of all items |
All of the Apex for this application is contained in triggers. This application has the following triggers:
Object | Trigger Name | When Runs | Description |
---|---|---|---|
Item | Calculate | after insert, after update, after delete | Updates the shipping invoice, calculates the totals and shipping |
Shipping_invoice | ShippingDiscount | after update | Updates the shipping invoice, calculating if there is a shipping discount |
In Shipping Invoice Example Code both of the triggers and the test class are listed. The comments in the code explain the functionality.
Before an application can be included as part of a package, 75% of the code must be covered by unit tests. Therefore, one piece of the shipping invoice application is a class used for testing the triggers.