User Story
As a user, I want to receive a notification when any of my information (such as my first name, my last name, my email or phone number) is modified in the system. This will help me stay informed about changes to my personal information and ensure that my account remains secure.
Code
trigger UserTrigger on User (before update) {
for(User oldUser : Trigger.old) {
User newUser = Trigger.newMap.get(oldUser.Id);
if(newUser.FirstName != oldUser.FirstName ||
newUser.LastName != oldUser.LastName ||
newUser.Email != oldUser.Email ||
newUser.Phone != oldUser.Phone) {
String userEmail = oldUser.Email;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new List<String>{userEmail});
mail.setSubject('Your information has been updated');
mail.setPlainTextBody('Hello, ' + oldUser.FirstName + '.\n\nYour information has been updated.');
Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{mail});
}
}
}