1

这是我使用 APEX 编写的第一段代码,所以我正在学习……了解一点 Java(达到研究生水平),但我不知道为什么这不起作用。

我知道它没有像之前告诉我的那样给销售人员一个错误(因此最后注释掉了调试。我更改了联系人电子邮件,它只是这样做了,但我没有收到任何电子邮件!这是代码,谢谢你有什么帮助!

trigger Test1 on Contact (after update) {

//IF Email has changed (look at the old email and compare to new)
if(trigger.old[0].Email != trigger.new[0].Email){


    //Commit current transaction, reserver email capacity
    Messaging.reserveSingleEmailCapacity(1);
    //Create an email message object
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    //List<Messaging.SendEmailResult> results = new list<Messaging.SendEmailResult>();
    //hold email address
    String[] toAddresses = new String[] {trigger.new[0].Email};

    //Assign new address string
    mail.setToAddresses(toAddresses);
    //specify return ad (assuming as being sent from server)    
    mail.setReplyTo('XXX@example.com');
    //sender name
    mail.setSenderDisplayName('Mr Apex Tester');
    //Subject Specification
    mail.setSubject('Subjected to Learning');
    //And... the content
    mail.setPlainTextBody('You have just changed your SalesForce contact email from ' + trigger.old[0].Email + ' to ' + trigger.new[0].Email + '. If this was not intentional please log back into Salesforce.com and ammend you details.');

    //if (!results.get(0).isSuccess()){                      
        //System.debug('That didnt really work did it? Reason: ' + results.get(0).getErrors()[0].getMessage());
    }
}
4

1 回答 1

5

您的代码的第一个问题是您的触发器不是在这里大量阅读

批量触发 #1

批量触发器 #2

下一个麻烦是你不发送电子邮件Messaging.sendEmail

尝试以下操作:

    trigger Test1 on Contact (after update) {
        Messaging.reserveSingleEmailCapacity(trigger.size);
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        for (Contact c : trigger.new) { // walk through all records which is processed
            Contact old = trigger.oldMap.get(c.Id); // get old record from oldMap
            if (old.Email != c.Email ) { // check current email 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setToAddresses(new String[] {c.Email};);
                email.setReplyTo('XXX@example.com');
                email.setSenderDisplayName('Mr Apex Tester');
                email.setSubject('Subjected to Learning');
                email.setPlainTextBody('You have just changed your SalesForce contact email from ' + 
                    old.Email + ' to ' + c.Email +
                     '. If this was not intentional please log back into Salesforce.com and ammend you details.');
                emails.add(email);
            }
        }
        Messaging.sendEmail(emails);
    }
于 2013-07-25T15:16:20.880 回答