Search Here

If opportunity Stage is updated upon its creation or update then update description as either 'Opp is Closed Lost' or 'Opp is Closed Won' or 'Opp IS Open' triggers in Salesforce

 To update the Description field on an Opportunity based on its Stage whenever the Stage is updated or the Opportunity is created, you can create an Apex Trigger on the Opportunity object. The trigger will check the Stage value and populate the Description field accordingly.

Here’s the code for the trigger:

trigger UpdateOpportunityDescription on Opportunity (before insert, before update) {

    // Loop through the Opportunity records being inserted or updated

    for (Opportunity opp : Trigger.new) {

        // Check the Stage value and set the Description field accordingly

        if (opp.StageName == 'Closed Lost') {

            opp.Description = 'Opp is Closed Lost';

        } else if (opp.StageName == 'Closed Won') {

            opp.Description = 'Opp is Closed Won';

        } else if (opp.StageName == 'Open') {

            opp.Description = 'Opp is Open';

        } else {

            opp.Description = ''; // Clear the Description for other stages if necessary

        }

    }

}

Explanation of the Code

  1. Trigger Event:

    • The trigger runs on the before insert and before update events. This ensures that the Description field is updated before the Opportunity is saved.
  2. Logic:

    • The trigger checks the StageName field of the Opportunity:
      • If the Stage is Closed Lost, it sets the Description field to 'Opp is Closed Lost'.
      • If the Stage is Closed Won, it sets the Description field to 'Opp is Closed Won'.
      • If the Stage is Open, it sets the Description field to 'Opp is Open'.
    • If the Opportunity is in any other Stage, you can optionally clear the Description field or leave it as-is.
  3. Bulk Processing:

    • The trigger is bulkified, meaning it can handle multiple Opportunities in a single transaction.
@isTest
private class UpdateOpportunityDescriptionTest {
    @isTest
    static void testOpportunityDescription() {
        // Create a new Opportunity with Stage 'Closed Lost'
        Opportunity oppClosedLost = new Opportunity(
            Name = 'Test Opportunity Lost',
            StageName = 'Closed Lost',
            CloseDate = Date.today(),
            Amount = 5000
        );
        insert oppClosedLost;

        // Verify that the Description field is populated correctly
        Opportunity insertedOppLost = [SELECT Description FROM Opportunity WHERE Id = :oppClosedLost.Id];
        System.assertEquals('Opp is Closed Lost', insertedOppLost.Description, 'Description should be updated for Closed Lost stage');

        // Create a new Opportunity with Stage 'Closed Won'
        Opportunity oppClosedWon = new Opportunity(
            Name = 'Test Opportunity Won',
            StageName = 'Closed Won',
            CloseDate = Date.today(),
            Amount = 10000
        );
        insert oppClosedWon;

        // Verify that the Description field is populated correctly
        Opportunity insertedOppWon = [SELECT Description FROM Opportunity WHERE Id = :oppClosedWon.Id];
        System.assertEquals('Opp is Closed Won', insertedOppWon.Description, 'Description should be updated for Closed Won stage');

        // Create a new Opportunity with Stage 'Open'
        Opportunity oppOpen = new Opportunity(
            Name = 'Test Opportunity Open',
            StageName = 'Open',
            CloseDate = Date.today(),
            Amount = 15000
        );
        insert oppOpen;

        // Verify that the Description field is populated correctly
        Opportunity insertedOppOpen = [SELECT Description FROM Opportunity WHERE Id = :oppOpen.Id];
        System.assertEquals('Opp is Open', insertedOppOpen.Description, 'Description should be updated for Open stage');
    }
}



Tags

Post a Comment

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