Shipping Invoice Example Walk-Through

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.

The Shipping Invoice sample requires custom objects. You can either create these on your own, or download the objects and Apex code as an unmanaged package from the Salesforce AppExchange. To obtain the sample assets in your org, install the Apex Tutorials Package. This package also contains sample code and objects for the Apex Quick Start.

Note

Scenario 

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.

Data and Code Models 

This sample application uses two new objects: Item and Shipping_invoice.

The following assumptions are made:

  • Item A cannot be in both orders shipping_invoice1 and shipping_invoice2. Two customers cannot obtain the same (physical) product.
  • The tax rate is 9.25%.
  • The shipping rate is 75 cents per pound.
  • Once an order is over $100, the shipping discount is applied (shipping becomes free).

The fields in the Item custom object include:

NameTypeDescription
NameStringThe name of the item
PriceCurrencyThe price of the item
QuantityNumberThe number of items in the order
WeightNumberThe weight of the item, used to calculate shipping costs
Shipping_invoiceMaster-Detail (shipping_invoice)The order this item is associated with

The fields in the Shipping_invoice custom object include:

NameTypeDescription
NameStringThe name of the shipping invoice/order
SubtotalCurrencyThe subtotal
GrandTotalCurrencyThe total amount, including tax and shipping
ShippingCurrencyThe amount charged for shipping (assumes $0.75 per pound)
ShippingDiscountCurrencyOnly applied once when subtotal amount reaches $100
TaxCurrencyThe amount of tax (assumes 9.25%)
TotalWeightNumberThe total weight of all items

All of the Apex for this application is contained in triggers. This application has the following triggers:

ObjectTrigger NameWhen RunsDescription
ItemCalculateafter insert, after update, after deleteUpdates the shipping invoice, calculates the totals and shipping
Shipping_invoiceShippingDiscountafter updateUpdates the shipping invoice, calculating if there is a shipping discount

The following is the general flow of user actions and when triggers run:

Flow of user action and triggers for shopping cart application

Flow of user action and triggers for the shopping cart application

  1. User clicks Orders | New, names the shipping invoice and clicks Save.
  2. User clicks New Item, fills out information, and clicks Save.
  3. Calculate trigger runs. Part of the Calculate trigger updates the shipping invoice.
  4. ShippingDiscount trigger runs.
  5. User can then add, delete or change items in the invoice.

In Shipping Invoice Example Code both of the triggers and the test class are listed. The comments in the code explain the functionality.

Testing the Shipping Invoice Application 

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.

The test class verifies the following actions are completed successfully:

  • Inserting items
  • Updating items
  • Deleting items
  • Applying shipping discount
  • Negative test for bad input