Don’t allow to change Amount, if Opportunity Old Amount not equal to New Amount in Salesforce | Apex Trigger to prevent change if field has specific value on Opportunity in Salesforce

80 views

Hey guys, today in this post we are going to learn about Write Apex Trigger to Don’t allow to change Amount, if Opportunity Old Amount not equal to New Amount in Salesforce

 

Final Output →

 

Other related post that would you like to learn in Salesforce

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!

 

 

Create Apex Trigger →

Step 1:- Create Apex Controller : OpportunityTrigger.apxt

SFDX:Create Apex Trigger>> New >> OpportunityTrigger.apxt

OpportunityTrigger.apxt [Apex Trigger]

  1.    TRIGGER OpportunityTrigger ON Opportunity (BEFORE INSERT, after INSERT, BEFORE UPDATE, after UPDATE) {
  2.  
  3.     IF(TRIGGER.isBefore && TRIGGER.isUpdate){
  4.        OpportunityTriggerHandler.updateAmountHelper(TRIGGER.new, TRIGGER.oldMap);
  5.     }ELSE IF(TRIGGER.isAfter){
  6.  
  7.     }
  8.  
  9. }

 

Create Trigger Handler Controller →

Step 2:- Create Apex Controller : OpportunityTriggerHandler.apxc

SFDX:Create Apex Trigger>> New >> OpportunityTriggerHandler.apxc

Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

OpportunityTriggerHandler.apxc [Trigger Handler]

  1.  
  2.  public class OpportunityTriggerHandler {
  3.  
  4.     public static void updateAmountHelper(List<Opportunity> oppList, Map<Id, Opportunity> oldOppList){
  5.         Map<Id, Opportunity> oldRecOpp = NEW Map<Id, Opportunity>();
  6.         FOR(Opportunity opp1:oldOppList.values()){
  7.             oldRecOpp.put(opp1.Id,opp1);
  8.         }
  9.  
  10.         FOR(Opportunity Opp2:oppList){
  11.             IF(Opp2.Amount != oldRecOpp.get(Opp2.Id).Amount){
  12.                 Opp2.Amount.addError('Amount Cannot be change');
  13.             }
  14.         }
  15.  
  16.  
  17.     }
  18.  
  19. }

 

Further post that would you like to learn in Salesforce

 

FAQ (Frequently Asked Questions)

How do I change the opportunity amount in Salesforce?

You can't directly edit the amount unless the opportunity has no products. To change the amount for an opportunity that contains products, edit the sales price or quantity of the related products.

How do I create a custom opportunity in Salesforce?

From the Service Home, navigate to Accounts. Click the name of the account for which you want to create an opportunity. Click New Opportunity. Enter the campaign information and click Save.

What is the difference between amount and expected revenue in Salesforce?

As you can see from the example, the Amount column represents the total expected revenue to be generated from the deals in the list while the Expected Revenue is related to which stage the deal is in and the probability associated with that stage based on historical outcomes.

 
Note:: – You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses →

👉 Get Free Course →

📌 Salesforce Administrators

📌 Salesforce Lightning Flow Builder

📌 Salesforce Record Trigger Flow Builder

👉 Get Free Course →

📌 Aura Lightning Framework

📌 Lightning Web Component (LWC)

📌 Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net