Search Here

If the Account phone is updated then populate below message in description. Description = Phone is Updated! Old Value : XXX & New Value : XXX

To update the Description field of an Account record whenever the Phone field is updated, you can create an Apex Trigger on the Account object. This trigger will compare the old and new values of the Phone field and populate the Description field with the required message if the Phone field is updated.

trigger UpdateDescriptionOnPhoneChange on Account (before update) {

    // Loop through the updated Account records

    for (Account acc : Trigger.new) {

        // Check if the Phone field has been changed (comparing old and new values)

        if (acc.Phone != Trigger.oldMap.get(acc.Id).Phone) {

            // Construct the message with the old and new Phone values

            String oldPhone = Trigger.oldMap.get(acc.Id).Phone;

            String newPhone = acc.Phone;

            // Set the Description field with the appropriate message

            acc.Description = 'Phone is Updated! Old Value: ' + oldPhone + ' & New Value: ' + newPhone;

        }

    }

}

Explanation of the Code

  1. Trigger Event:

    • This trigger runs on the before update event, so it will trigger before the Account record is saved when the Phone field is updated.
    • Using the before update event allows us to modify the Description field before the record is saved.
  2. Logic:

    • The trigger checks if the Phone field value has changed by comparing the current value (Trigger.new) with the previous value (Trigger.oldMap).
    • If the Phone field is updated, the trigger constructs a message with the old and new Phone values and populates the Description field with the message.
  3. Old and New Values:

    • Trigger.oldMap.get(acc.Id).Phone fetches the Phone value before the update (old value).
    • acc.Phone fetches the updated Phone value (new value).
  4. Message Format:

    • The message is constructed as: "Phone is Updated! Old Value: XXX & New Value: XXX", where XXX represents the old and new Phone values.

@isTest
private class UpdateDescriptionOnPhoneChangeTest {
    @isTest
    static void testPhoneUpdateDescription() {
        // Create a test Account with an initial Phone number
        Account testAccount = new Account(
            Name = 'Test Account',
            Phone = '123-456-7890',
            Description = ''
        );
        insert testAccount;

        // Update the Phone number of the Account
        testAccount.Phone = '987-654-3210';
        update testAccount;

        // Query the Account to verify the Description field has been updated
        Account updatedAccount = [SELECT Description FROM Account WHERE Id = :testAccount.Id];

        // Verify that the Description has been populated with the correct message
        String expectedDescription = 'Phone is Updated! Old Value: 123-456-7890 & New Value: 987-654-3210';
        System.assertEquals(expectedDescription, updatedAccount.Description, 'The Description field should be updated with the phone change message');
    }
}

 


Tags

Post a Comment

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