How to Calculate discount for opportunity in Salesforce based on percentage field?
Table of Contents
Introduction to scenario
→ Apex Trigger 5
Prerequisite
- Create a Custom field on Opportunity “Discount” and the data type of this field should be percent.
- Create a Custom field on Opportunity “Discounted Price” with Currency Data Type
Requirement
Develop an Apex Trigger on Opportunity so that when any opportunity is created and if the Discount & Amount field is not blank then calculate the discount and store it in the Discount Price field.
To Calculate the discount use the below formula and store it in a variable
Decimal discount = ( Discount * Amount ) / 100
To Calculate the Discounted Price use the below calculation and store it in a variable
Decimal discountedAmount = Amount – discount;
Hand on
Dispatcher Class
Handler Class
Outcome
Resources
- https://www.pantherschools.com/create-an-task-under-opportunity-if-accountid-is-null/
- https://www.pantherschools.com/how-to-update-account-billing-address-in-shipping-address-only-if-shipping-address-is-blank/
- https://www.pantherschools.com/how-to-create-a-task-under-account/
- https://trailhead.salesforce.com/content/learn/modules/apex_triggers
