Search Here

Upon Account Creation, if Industry is not null and having value as ‘Media’ then populate Rating as Hot triggers in Salesforce

 To create a trigger that generates a number of Contacts equal to the value entered in the Number of Locations field on the Account object, follow the implementation steps below. Each Contact will be linked to the Account, and you can customize their attributes (e.g., name).


trigger CreateContactsBasedOnLocations on Account (after insert) {

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


    for (Account acc : Trigger.new) {

        // Check if Number_of_Locations__c is populated and greater than zero

        if (acc.Number_of_Locations__c != null && acc.Number_of_Locations__c > 0) {

            Integer numberOfContacts = acc.Number_of_Locations__c.intValue();

            for (Integer i = 1; i <= numberOfContacts; i++) {

                // Create a Contact for each location

                contactsToInsert.add(new Contact(

                    FirstName = 'Location',

                    LastName = acc.Name + ' ' + i, // Use Account Name + index for uniqueness

                    AccountId = acc.Id            // Link Contact to the Account

                ));

            }

        }

    }

    // Insert all Contacts in bulk

    if (!contactsToInsert.isEmpty()) {

        insert contactsToInsert;

    }

}

Explanation of the Code

  1. Trigger Event:

    • Runs on the after insert event since we need the AccountId for associating Contacts with the Account.
  2. Logic:

    • The trigger checks if the custom field Number_of_Locations__c (assumed to be a custom field) has a value and if it's greater than zero.
    • It then loops based on the value in Number_of_Locations__c, creating that many Contact records.
  3. Bulk Processing:

    • Ensures efficient handling by collecting all Contacts in a List<Contact> and inserting them in bulk.
  4. Contact Fields:

    • LastName: Combines the Account name with an index (e.g., AccountName 1, AccountName 2) to ensure uniqueness.
@isTest
private class CreateContactsBasedOnLocationsTest {
    @isTest
    static void testCreateContactsBasedOnLocations() {
        // Test Case 1: Account with Number_of_Locations__c greater than 0
        Account testAccount = new Account(
            Name = 'Test Account',
            Number_of_Locations__c = 3
        );
        insert testAccount;

        // Query for the Contacts created
        List<Contact> contacts = [SELECT FirstName, LastName, AccountId 
                                  FROM Contact 
                                  WHERE AccountId = :testAccount.Id];
        
        // Assert that the correct number of Contacts is created
        System.assertEquals(3, contacts.size(), 'Number of Contacts should match Number_of_Locations__c');
        for (Integer i = 1; i <= 3; i++) {
            System.assert(contacts.anyMatch(c -> c.LastName == 'Test Account ' + i), 
                          'Contact LastName should match pattern: Test Account {index}');
        }

        // Test Case 2: Account with Number_of_Locations__c = 0
        Account zeroLocationsAccount = new Account(
            Name = 'Zero Locations Account',
            Number_of_Locations__c = 0
        );
        insert zeroLocationsAccount;

        // Query for Contacts for the second Account
        List<Contact> zeroContacts = [SELECT Id FROM Contact WHERE AccountId = :zeroLocationsAccount.Id];
        System.assertEquals(0, zeroContacts.size(), 'No Contacts should be created for Number_of_Locations__c = 0');
    }
}





Tags

Post a Comment

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