Search Here

Creates the number of contacts which are equal to the number which we will enter in the Number of Locations field on the Account Object triggers in Salesforce

 Here’s an Apex Trigger that creates a Contact for an Account when the Industry is set to Banking. The Contact's LastName will be the Account Name, and the Contact's Phone will be the Account's Phone.

trigger CreateContactForBankingAccount on Account (after insert) {

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


    for (Account acc : Trigger.new) {

        // Check if the Industry is Banking

        if (acc.Industry == 'Banking') {

            // Create a Contact associated with the Account

            contactsToInsert.add(new Contact(

                LastName = acc.Name,      // LastName as Account Name

                Phone = acc.Phone,        // Contact Phone as Account Phone

                AccountId = acc.Id        // Associate Contact with Account

            ));

        }

    }

    // Insert all Contacts in bulk

    if (!contactsToInsert.isEmpty()) {

        insert contactsToInsert;

    }

}

Explanation of the Trigger

  1. Trigger Event:

    • The trigger runs on the after insert event because the AccountId is needed to associate the Contact with the Account.
  2. Condition:

    • The trigger checks if the Industry field on the Account is set to Banking.
  3. Contact Fields:

    • LastName: Set to the Account Name.
    • Phone: Set to the Account Phone.
    • AccountId: Links the Contact to the newly created Account.
  4. Bulk Processing:

    • The code is bulkified by adding Contact records to a List<Contact> and inserting them in bulk.
@isTest
private class CreateContactForBankingAccountTest {
    @isTest
    static void testCreateContactForBankingAccount() {
        // Test Case 1: Account with Industry as Banking
        Account bankingAccount = new Account(
            Name = 'Banking Corp',
            Industry = 'Banking',
            Phone = '123-456-7890'
        );
        insert bankingAccount;

        // Query for the associated Contact
        Contact bankingContact = [SELECT LastName, Phone, AccountId 
                                   FROM Contact 
                                   WHERE AccountId = :bankingAccount.Id];
        
        // Assert that the Contact is created correctly
        System.assertEquals('Banking Corp', bankingContact.LastName, 'LastName should match Account Name');
        System.assertEquals('123-456-7890', bankingContact.Phone, 'Phone should match Account Phone');
        System.assertEquals(bankingAccount.Id, bankingContact.AccountId, 'AccountId should match');

        // Test Case 2: Account with Industry not Banking
        Account otherAccount = new Account(
            Name = 'Tech Corp',
            Industry = 'Technology',
            Phone = '987-654-3210'
        );
        insert otherAccount;

        // Query for Contacts related to the non-banking Account
        List<Contact> nonBankingContacts = [SELECT Id FROM Contact WHERE AccountId = :otherAccount.Id];
        System.assertEquals(0, nonBankingContacts.size(), 'No Contact should be created for non-Banking Account');
    }
}





Tags

Post a Comment

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