3

How can I configure Salesforce to send an email to a user when its user in the system is activated (IsActive = true)?

Right now I can create a workflow rule that triggers when the User record is changed, and specify to run when the IsActive field is set to true, but when I try to create the Email Alert, I don't see a way to inform that I want the email to be sent to that user.

I only get these options:

enter image description here

But none of them seem to fit. I tried using the 'Email Field' one, but it doesn't work.

Since I tried this, I thought of doing it with a trigger too, but when I send the notification email, the email simply does not arrive at my inbox. No errors are thrown, the Deliverability is ok, and the code works when I run it from the console.

I'm calling this method in an afterUpdate trigger on User:

public void notifyUser() {
    EmailUtils email_utils = new EmailUtils();
    EmailTemplate template = [SELECT Id, DeveloperName FROM EmailTemplate WHERE DeveloperName = 'UsuarioAtivado' LIMIT 1];

    Map<Id, User> oldmap = (Map<Id, User>) Trigger.oldMap;
    List<Id> activated_users_ids = new List<Id>();

    for (User u : (List<User>) Trigger.new) {
        if (oldMap.containsKey(u.Id) && !oldMap.get(u.Id).IsActive && u.IsActive) {
            System.debug('User activated. [' + u.Id + ']');
            activated_users_ids.add(u.Id);
        }
    }

    List<User> usuarios_ativados = [SELECT Id, Name, Email FROM User WHERE Id IN :activated_users_ids];

    Messaging.reserveSingleEmailCapacity(usuarios_ativados.size());

    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

    for (User u : usuarios_ativados) {
        Messaging.SingleEmailMessage sem = new Messaging.SingleEmailMessage();
        sem.setTemplateId(template.Id);
        sem.setSaveAsActivity(false);
        sem.setOrgWideEmailAddressId(email_utils.addresses.get('CompanyName'));
        sem.setTargetObjectId(u.Id);
        emails.add(sem);
        System.debug('Email:');
        System.debug(sem);
    }

    Messaging.sendEmail(emails);
}
2
  • Could you tell please, what didn't work for email field in workflow? And since both - workflow and trigger don't work, I would suggest you to check an e-mail address on user. Also, write System.debug(Messaging.sendEmail(emails)); instead of Messaging.sendEmail(emails); to check the results. And after that look into debug logs - it could be that your trigger doesn't fire the code.
    – EvAzi
    Commented Jan 9, 2017 at 14:08
  • I've collected the SendResult, and iterated over it. It is always successful (isSuccess() returns true). Commented Jan 9, 2017 at 14:11

1 Answer 1

5

I am able to see the Email field of User in Email Alert and this is working. The specified email received email once User has been activated using Workflow.

enter image description here

2
  • 1
    I am able to select it as well. Didn't get to the point where I had an answer typed up but this is almost exactly what it would have looked like.
    – Adrian Larson
    Commented Jan 9, 2017 at 14:11
  • Eeeeh. I might have forgotten to activate the workflow rule. Thanks. One thing still bothers me though: the trigger didn't work, and it is doing the exact same thing as the workflow rule. Commented Jan 9, 2017 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .