Activity Forums Salesforce® Discussions In below code snippet in Salesforce, what is going wrong?

  • In below code snippet in Salesforce, what is going wrong?

    Posted by Aman on September 22, 2018 at 6:18 pm

    In below code snippet,

    trigger TestBeforeDelete on Lead (before Delete) {
    
        for(Lead l : Trigger.Old)
        {
            l.addError('error');
        }
    
        String msgBody = 'Test Email';
        String Subject = 'Test from Cogni Force on Lead';
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {'[email protected]'};
        mail.setToAddresses(toAddresses);
        mail.setReplyTo('[email protected]');
        mail.setSenderDisplayName('Cogniforce Test Simulator');
        mail.setSubject(Subject);
        mail.setPlainTextBody(msgBody);
        mail.setHTMLBody(msgBody);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }

    What is your observation and what is going wrong?

    Avnish Yadav replied 6 years, 2 months ago 4 Members · 3 Replies
  • 3 Replies
  • shariq

    Member
    September 22, 2018 at 6:18 pm

    It will not send any email. Because “adderror” prevents all transactions from committing including emails.

  • Parul

    Member
    September 23, 2018 at 6:19 am

    Because “adderror” prevents all transactions from committing including emails so you will not send any email.

  • Avnish Yadav

    Member
    September 30, 2018 at 4:02 am

    Can you explain to me how it is working? Because I didn't get it.

    Thanks in advance.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos