Last week as I was working on customizing Quotes for a client, and I wanted to display the subtotal for each individual line item on the quote taking into account the quantity and discount. SugarLogic and Calculated Fields are the perfect tools for this use case. If you haven't looked into adding SugarLogic to your custom SugarCRM development tool belt, you should definitely take some time to learn more about it.

SugarLogic a great way to add simple logic and calculations to module fields without dealing with messy logic hook hacks. Best of all, SugarLogic is a key feature of the much anticipated SugarCRM 7 so you can rest assured your customizations will last.

Without further ado, on to the code... To help better communicate each line item on the Quote, I created a new field on the Products module called 'subtotal_c' of type 'Currency'. To enable SugarLogic and Calculated Fields, all you need to do is check the 'Calculated Value' checkbox and click the 'Edit Formula' button to start writing your logic. Here's the SugarLogic I came up with:

To break this down a bit, there are two primary things that need to be calculated: discount and quantity.

Products on Quotes can be discounted two ways, by a specific amount or by a percentage. To handle these two cases, I started things off with the ifElse function. $discount_select is a checkbox field which evaluates to true (checked) or false (not checked) in SugarLogic, so I used it as the boolean to check against. It basically says "If $discount_select is true, then the Product is using a percentage-based discount. If $discount_select is false, then the Product is using a flat-rate discount".

The middle part (lines 3-15) handles the calculation for the percentage based discount. It calculates the percentage amount from the $discount_amount, then subtracts that amount from the $discount_price. The last part (lines 16-22) handles the calculation for the flat-rate discount. It simply subtracts the $discount_amount from the $discount_price. Both cases then handle the quantity by multiplying the price less discount by the given quantity.

To use this field, all you need to do is add it to your views or Quote templates and it'll show the calculated value. And that's it!

In the above image, you can see how the right column displays the subtotal of the line item with respect to the discount and quantity.

There is one caveat. Due to how SugarLogic works, after adding the subtotal_c field, only newly create Quotes and Product line items will be given a calculated value. To get subtotal_c to show up for previously created items, you'll need to edit and save each product record so that the calculated field can be generated. You can do that with a simple script like this:

How have you used SugarLogic in the past? Have you ever made customizations like this in the past?

Like this post?

We'll send you an email once a month with the latest posts.

Keep up to date on the latest additions

We'll send you an email every month with handpicked add-ons, reviews, tricks and tips. Don't worry, we hate spam as much as you do.