I am new to SFDC please help.

Requirement: Restrict user deactivation if Account is owned by the user and send an Email with details.

Trigger on user object:

    trigger userTrigger on User (before insert,before update) 
    switch on Trigger.operationType
        when Before_update
            List<User> userList = Trigger.new;
            for(User currentUser: userList)
                    List<Account> acc=[Select id, Name from Account where ownerId =:currentUser.Id];
                        currentUser.addError('Users owns Account please handle before deactivating: Accounts owned '+Acc.size());
                        System.debug('Reached here');

Trigger Handler:

public class userTriggerHandler 
    public static void sendDetails(id currId)
        System.debug('send Details called');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = new String[] { 'dummy@mail.com' };
        message.optOutPolicy = 'FILTER';
        message.subject = 'Dummy Subject';
        message.plainTextBody = 'This is the message body.';
        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
        Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            System.debug('Mail Sent!');
        //Try-Catch will be used for Exception handling

If I am not using @future annotation I am getting MixedDML Exception as I am using Setup and Nonsetup object, I guess.

And If I am using future annotation this method is not getting called because of.addError() but I want both to throw error and send the email

Can I please be helped with this, Thank you. Can you suggest any other best practice?

  • There is no way to throw the error and send the email. You could set IsActive back to true, to make sure the records simply does not get disabled. When you use addError, the entire transaction is rolled back (including calls to future methods, or sending email).
    – rael_kid
    Jul 16 at 16:51
  • 1
    @rael_kid That's what Immediate Publish Platform Events are for, to save data from a doomed transaction.
    – sfdcfox
    Jul 17 at 3:04
  • Hi sfdcfox. can you please guide me how to implement it. Just the modification I need to do in above code. Thanks
    – Alpha1331
    Jul 17 at 11:43

The only way to persist anything from a transaction that fails is to use a Platform Event. Crucially, you need this Platform Event to publish immediately, not when the transaction finishes. Define a Platform Event with fields to store whatever data you need to send out, then have its subscriber trigger send your email.

  • Hi Adrian. can you please guide me how to implement it. Just the modification I need to do in above code. Thanks
    – Alpha1331
    Jul 17 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.