这是我用APEX编写的第一段代码,所以我正在学习...知道一点Java(到研究生水平),但我不知道为什么这不起作用。
我知道它不会像以前告诉我那样给salesforce一个错误(因此最后注释掉了调试。我更改了一个联系人电子邮件,它只是这样做,但我没有收到任何电子邮件!这是代码,谢谢你的帮助!
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());
}
}
答案 0 :(得分:5)
代码中的第一个问题是你的触发器不是BULK 在这里阅读它
下一个问题是您不发送包含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);
}