We will cover all that you need to know in order to calculate the total of an opportunity after a discount.
You will need the following fields:
- Opportunity Amount, Field Type: Currency
- Discount %, Field Type: Percentage
You can use the names that best suit your organization for these fields. You will just need to replace their names in the code snippet below.
If you want to learn how to create Fields, please visit the following tutorial:
Fields
Give a meaningful name to your field and enter a description, although optional is recommended to let your users know that this field is a calculated one. The field type should be set to 'Currency' and the 'Calculated' box enabled in order to display the calculation section. Then copy & paste the code below:
(function(deal){
var percentage =deal.Amount * deal.Discount;
var total =deal.Amount - percentage;
return total;
}(deal));
If your fields have different names, you would need to replace the bold parts with the right field name. You can use 'CTRL + Space' to get a complete list of the fields available in your App to use in your Calculation. Once you are done, click the Save button.
This calculation will happen when the information on the Amount and Discount fields have information. If one of the two fields is missing, then the Discount total won't be calculated.
To avoid this from happening there are two possible solutions:
- Make both the Amount and the Discount fields 'Required'.
- Add validations to your Calculation in such a way that if either of the two values is missing it will simply return a '0', like in the below example:
(function(deal){
if(deal.Amount && deal.Discount){
var percentage = deal.Amount * deal.Discount;
var total = deal.Amount - percentage;
return total;
}
return 0;
}(deal));
Since we provided the amount and discount % in the Opportunity shown in the image below, the calculation was done successfully and we can see the Total of the Opportunity after the discount.