Search Here

Account records should have a field named ‘Recent Opportunity Amount’. It should contain the opportunity amount of the latest created opportunity on account triggers in Salesforce

 To update the Recent Opportunity Amount field on the Account object with the Opportunity Amount of the latest created Opportunity, you can create an Apex Trigger on the Opportunity object. This trigger will update the Account with the amount of the most recent Opportunity whenever an Opportunity is created or updated.

trigger UpdateRecentOpportunityAmount on Opportunity (after insert, after update) {

    // Create a map to store Account IDs and the latest Opportunity Amount

    Map<Id, Decimal> accountToRecentAmount = new Map<Id, Decimal>();


    // Loop through all Opportunities inserted or updated

    for (Opportunity opp : Trigger.new) {

        // Only process Opportunities that are associated with an Account

        if (opp.AccountId != null) {

            // Store the Opportunity Amount for the corresponding AccountId

            accountToRecentAmount.put(opp.AccountId, opp.Amount);

        }

    }

     // Update the Account records with the latest Opportunity Amount

    if (!accountToRecentAmount.isEmpty()) {

        List<Account> accountsToUpdate = new List<Account>();


        // Loop through the map and create Account records to update

        for (Id accountId : accountToRecentAmount.keySet()) {

            Account accToUpdate = new Account(Id = accountId);

            accToUpdate.Recent_Opportunity_Amount__c = accountToRecentAmount.get(accountId);

            accountsToUpdate.add(accToUpdate);

        }

        // Perform bulk update on Accounts

        if (!accountsToUpdate.isEmpty()) {

            update accountsToUpdate;

        }

    }

}

Explanation of the Code

  1. Trigger Event:

    • The trigger is fired on the after insert and after update events because the Opportunity is created or updated, and the associated Account needs to be updated with the latest Opportunity Amount.
  2. Logic:

    • For each Opportunity that is inserted or updated, the trigger checks if it is associated with an Account (AccountId).
    • The Amount of the Opportunity is stored in a map with the AccountId as the key. If multiple Opportunities exist for an Account, only the most recent Opportunity's Amount is considered (you can extend this logic if you need more complex handling, like tracking the latest Opportunity based on creation date).
  3. Bulk Processing:

    • A map is used to efficiently collect the latest Opportunity Amount for each Account. After processing all Opportunities, a bulk update is performed on the Account records.
@isTest
private class UpdateRecentOpportunityAmountTest {
    @isTest
    static void testUpdateRecentOpportunityAmount() {
        // Create an Account for testing
        Account testAccount = new Account(Name = 'Test Account');
        insert testAccount;

        // Create Opportunities for the Account
        Opportunity opp1 = new Opportunity(AccountId = testAccount.Id, Amount = 10000, StageName = 'Prospecting', CloseDate = Date.today().addDays(30));
        Opportunity opp2 = new Opportunity(AccountId = testAccount.Id, Amount = 15000, StageName = 'Proposal', CloseDate = Date.today().addDays(60));
        insert new List<Opportunity>{opp1, opp2};

        // Query the Account to check the Recent Opportunity Amount
        Account updatedAccount = [SELECT Recent_Opportunity_Amount__c FROM Account WHERE Id = :testAccount.Id];

        // Verify that the Recent Opportunity Amount is updated to the latest Opportunity Amount
        System.assertEquals(15000, updatedAccount.Recent_Opportunity_Amount__c, 'The Recent Opportunity Amount should be updated to 15000');
    }
}



Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.