Search Here

Create a related Opportunity when an Account is created triggers in Salesforce

 Here's an Apex Trigger that automatically creates a related Opportunity whenever an Account is created.

trigger CreateOpportunityOnAccountInsert on Account (after insert) {

    List<Opportunity> opportunitiesToInsert = new List<Opportunity>();


    for (Account acc : Trigger.new) {

        // Create an Opportunity for each new Account

        Opportunity opp = new Opportunity();

        opp.Name = acc.Name + ' Opportunity'; // Opportunity name based on Account Name

        opp.AccountId = acc.Id;

        opp.StageName = 'Prospecting'; // Default stage for the Opportunity

        opp.CloseDate = Date.today().addMonths(1); // Default Close Date 1 month from today

        opportunitiesToInsert.add(opp);

    }

    // Perform bulk insert of Opportunities

    if (!opportunitiesToInsert.isEmpty()) {

        insert opportunitiesToInsert;

    }

}

Explanation of the Code

  1. Trigger Event:

    • The trigger runs on the after insert event to ensure the Account is fully created before creating the Opportunity and linking it to the Account.
  2. Logic:

    • For each new Account created, a new Opportunity is created with the following default values:
      • Name: The Opportunity is named after the Account with the suffix " Opportunity."
      • StageName: Set to 'Prospecting' (default).
      • CloseDate: Set to one month from the current date.
      • AccountId: Links the Opportunity to the newly created Account.
  3. Bulk Processing:

    • The trigger collects all the Opportunity records in a list and performs a bulk insert to ensure efficient processing.
@isTest
private class CreateOpportunityOnAccountInsertTest {
    @isTest
    static void testCreateOpportunityOnAccountInsert() {
        // Create a list of Accounts for bulk testing
        List<Account> accounts = new List<Account>{
            new Account(Name = 'Account 1'),
            new Account(Name = 'Account 2'),
            new Account(Name = 'Account 3')
        };

        // Insert the Accounts
        insert accounts;

        // Verify that Opportunities are created
        List<Opportunity> createdOpportunities = [SELECT Name, StageName, CloseDate, AccountId FROM Opportunity WHERE AccountId IN :accounts];
        System.assertEquals(3, createdOpportunities.size(), 'Three Opportunities should be created for three Accounts');

        // Verify Opportunity details for the first Account
        Opportunity opp1 = [SELECT Name, StageName, CloseDate, AccountId FROM Opportunity WHERE AccountId = :accounts[0].Id];
        System.assertEquals('Account 1 Opportunity', opp1.Name, 'Opportunity Name should be Account Name + " Opportunity"');
        System.assertEquals('Prospecting', opp1.StageName, 'Opportunity Stage should be Prospecting');
        System.assertEquals(Date.today().addMonths(1), opp1.CloseDate, 'Opportunity Close Date should be 1 month from today');
    }
}




Tags

Post a Comment

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