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?
Proximity searches are becoming more essential to businesses every day. Sales and marketing teams use them to organize field visits and campaigns. The real estate industry use them to offer better alternative locations to their customers. Hotel, travel and tourism businesses use them to offer variou...
Data Anonymizer for SugarCRM
Is data privacy your priority? Get a data-breach proof CRM system! Data Anonymizer is a tool designed to quickly encode data on your CRM instance, while preserving their diversity and relations between records. It allows you to quickly prepare a realistic instance for tests and demonstrations withou...
SugarCRM Outlook Plugin for Mac
Implicit FrontEnd for Mac is an Outlook integration solution for SugarCRM which enables users to do all their CRM work from within Outlook. Use Email side-panel in Outlook to view CRM information, update or create new CRM records and archive email to any CRM record.
- Show more addons