Search Here

On Account create two checkbox fields labeled as Contact and Opportunity. Now when a new Account record is created and if a particular Contact or Opportunity checkbox is checked then create that related record. Also Opportunity record should be created only if the Account record Active picklist is populated with a Yes.

 To meet your requirements, we'll create an Apex Trigger on the Account object. This trigger will check the values of the Contact and Opportunity checkbox fields on the Account. If the Contact checkbox is checked, a related Contact will be created, and if the Opportunity checkbox is checked, an Opportunity will be created. The Opportunity will only be created if the Active picklist on the Account is set to "Yes."

trigger CreateRelatedRecordsOnAccountCreate on Account (after insert) {

    List<Contact> contactsToInsert = new List<Contact>();

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

    // Loop through all newly created Accounts

    for (Account acc : Trigger.new) {

        // Check if the Contact checkbox is selected

        if (acc.Contact__c == true) { // Contact__c is the checkbox field on Account

            Contact newContact = new Contact();

            newContact.AccountId = acc.Id;

            newContact.LastName = acc.Name + ' Contact'; // Set Contact LastName based on Account Name

            newContact.Email = acc.Name + '@example.com'; // Sample email, customize as needed

            contactsToInsert.add(newContact);

        }

        // Check if the Opportunity checkbox is selected and the Account is Active

        if (acc.Opportunity__c == true && acc.Active__c == 'Yes') { // Opportunity__c and Active__c fields

            Opportunity newOpportunity = new Opportunity();

            newOpportunity.AccountId = acc.Id;

            newOpportunity.Name = acc.Name + ' Opportunity'; // Set Opportunity Name based on Account Name

            newOpportunity.StageName = 'Prospecting'; // Default Stage

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

            opportunitiesToInsert.add(newOpportunity);

        }

    }

    // Insert the related Contacts if there are any

    if (!contactsToInsert.isEmpty()) {

        insert contactsToInsert;

    }

    // Insert the related Opportunities if there are any

    if (!opportunitiesToInsert.isEmpty()) {

        insert opportunitiesToInsert;

    }

}

Explanation of the Code

  1. Trigger Event:

    • This trigger runs on the after insert event because we want to create the Contact and Opportunity records after the Account record has been created.
  2. Logic:

    • For each new Account, the trigger checks:
      • If the Contact checkbox (Contact__c) is checked, a Contact is created and linked to the Account.
      • If the Opportunity checkbox (Opportunity__c) is checked and the Active picklist field (Active__c) is set to "Yes", an Opportunity is created and linked to the Account.
    • The Opportunity will only be created if the Active picklist is set to "Yes" on the Account.
  3. Bulk Processing:

    • The trigger is designed to handle bulk processing, meaning if multiple Account records are inserted, the corresponding Contact and Opportunity records are created in bulk.
  4. Default Values:

    • The Contact record will have a default LastName based on the Account name and a sample email.
    • The Opportunity will have a default Stage of "Prospecting" and a CloseDate set to one month from today.
@isTest
private class CreateRelatedRecordsOnAccountCreateTest {
    @isTest
    static void testCreateContactAndOpportunity() {
        // Create a test Account with both Contact and Opportunity checkboxes selected and Active set to Yes
        Account testAccount = new Account(
            Name = 'Test Account',
            Contact__c = true,
            Opportunity__c = true,
            Active__c = 'Yes' // Set Active picklist to Yes
        );
        insert testAccount;

        // Query the newly created Contact
        Contact createdContact = [SELECT AccountId, LastName, Email FROM Contact WHERE AccountId = :testAccount.Id];
        System.assertEquals('Test Account Contact', createdContact.LastName, 'Contact Last Name should be based on Account Name');
        System.assertEquals('Test Account@example.com', createdContact.Email, 'Contact Email should be generated correctly');

        // Query the newly created Opportunity
        Opportunity createdOpportunity = [SELECT AccountId, Name, StageName, CloseDate FROM Opportunity WHERE AccountId = :testAccount.Id];
        System.assertEquals('Test Account Opportunity', createdOpportunity.Name, 'Opportunity Name should be based on Account Name');
        System.assertEquals('Prospecting', createdOpportunity.StageName, 'Opportunity Stage should be Prospecting');
        System.assertEquals(Date.today().addMonths(1), createdOpportunity.CloseDate, 'Opportunity Close Date should be 1 month from today');
    }

    @isTest
    static void testOpportunityNotCreatedWhenActiveIsNo() {
        // Create a test Account with Opportunity checkbox selected but Active set to No
        Account testAccount = new Account(
            Name = 'Test Account No Active',
            Contact__c = true,
            Opportunity__c = true,
            Active__c = 'No' // Set Active picklist to No
        );
        insert testAccount;

        // Query the created records to verify no Opportunity is created
        List<Opportunity> createdOpportunities = [SELECT Id FROM Opportunity WHERE AccountId = :testAccount.Id];
        System.assertEquals(0, createdOpportunities.size(), 'Opportunity should not be created when Active is No');
    }
}





Tags

Post a Comment

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