In my last post SFDC: Add a formula field, we see how to add a formula field and get the total of each line item field
Another thing that's missing from the Invoice is a field that aggregates all of the line items into one big invoice total. This is easy to do if the objects are in a master-detail relationship because you can use a roll-up summary field.
Calculate a Total With a Roll-Up Summary Field
Now that you have the total for each line item, it makes sense to add them all to get the invoice total. Because the line items have a master-detail relationship with the invoice, you can use a roll-up summary field to calculate this value. The roll-up summary is a special type of field that lets you aggregate information about related detail (child) objects. In this case, you want to sum the value of each line item.
- Navigate back to the Invoice custom object page from Setup by clicking Create > Objects and then clicking Invoice.
- In the Custom Fields & Relationships related list click New.
- Choose Roll-Up Summary as the data type, and click Next.
- For the Field Label field, enter Invoice Total, and click Next.
- In the Summarized Object list choose Line Items.
- For Roll Up Type, select Sum.
- In the Field to Aggregate list choose Line Item Total.
- Verify that your screen looks like this. Then click Next, Next, and Save.
Try out the App:
To see the new Invoice Total formula field in action, you only need to examine an invoice.
- Click the Invoices tab and then click an existing invoice.
- Notice the new Invoice Total field that “rolls up” all the values from the detail object’s Line Item Totals.
- To get the Line Item Total field to appear on the detail page, you’ll have to edit the page layout. (If you haven’t done that yet, see SFDC: Modify a Page Layout). When you do, it should look like the following image.
**This document is referred from salesforce help tutorials