You need to sign in to do that
Don't have an account?
bouscal
Help with Apex - send mail, update event
I'm flustered, can't understand why this won't work... help please.
Here's my code, it works fine like this but I want to add an update to the event survey_sent__c field so that once the email is successfully sent the flag on the event that triggered this action is updated. I want to do this to eliminate the recursive trigger. Also, when the survey is actually completed the event gets updated again and I want to insure another message isn't sent.
public with sharing class SurveyEmail {
//
// this is called from a before update trigger by SurveyEmail.checkEvents(Trigger.new);
//
public void checkEvents(Event[] ev){
List<Id>EventMembers = new List<Id>();
Map<Id,Id> EventMap = new Map<Id, Id>();
If (ev!=null){
For (Event e:ev){
if(e.Send_Survey__c==true && e.Event_Status__c=='Completed' && e.survey_sent__c==false){ // if e.survey_sent__c is true then abort
System.debug('##### Survey box checked, processing Event with ID = ' + e.id);
If(e.WhoId != null){
EventMembers.add(e.WhoId);
EventMap.put(e.whoid, e.id);
e.survey_sent__c = true; // tried this with the update statement at the end and it won't work - also tried creating a 2nd list and updating that, also fails
}
}
}
}
If(EventMembers.size()>0){
string EmailFailure=null;
// generate list of contacts to send message to
System.debug('##### Creating List of Contacts to send message too... ');
List<Contact>myRecipients = [SELECT id, firstname, lastname, email FROM contact WHERE id IN :EventMembers AND (NOT email LIKE '%forbidden%') ];
// create email objects as a list
System.debug('##### Creating list of messages ...');
List<Messaging.SingleEmailMessage> allMsg = new List<Messaging.SingleEmailMessage>();
OrgWideEmailAddress owa = [SELECT id, DisplayName, Address FROM OrgWideEmailAddress WHERE DisplayName='Global Sender' LIMIT 1];
// append new message to above list for each Contact
For (Contact c:myRecipients){
Id EvId = EventMap.get(c.id);
String theMsg = ('Please click the link below, or copy and paste into your browser to complete a brief survey ');
theMsg = theMsg + ('related to your recent interaction with an awesome service representative. \n \n ');
theMsg = theMsg + ('http://www.thethirdparty.com/survey?iv=############&q1=' + EvId + '&q2=' + c.id);
System.debug('##### Creating an instance of the message ...');
try{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayname(owa.DisplayName);
mail.setReplyTo(owa.Address);
mail.setTargetObjectId(c.id);
mail.setUseSignature(false);
mail.setSubject('Please complete this survey from the company.');
mail.setPlainTextBody(theMsg);
mail.setSaveAsActivity(true);
System.debug('##### Sending message ...');
Messaging.SendEmailResult[] mailResult=Messaging.sendemail(new Messaging.SingleEmailMessage[]{mail});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
if (!mailResult[0].isSuccess() ){
EmailFailure = 'Email Failed: ' + mailResult[0].getErrors()[0].getMessage();
}
}
catch(Exception Exp){
EmailFailure='Exception: ' + Exp.getCause();
}
}
}
}
}
Here's my code, it works fine like this but I want to add an update to the event survey_sent__c field so that once the email is successfully sent the flag on the event that triggered this action is updated. I want to do this to eliminate the recursive trigger. Also, when the survey is actually completed the event gets updated again and I want to insure another message isn't sent.
public with sharing class SurveyEmail {
//
// this is called from a before update trigger by SurveyEmail.checkEvents(Trigger.new);
//
public void checkEvents(Event[] ev){
List<Id>EventMembers = new List<Id>();
Map<Id,Id> EventMap = new Map<Id, Id>();
If (ev!=null){
For (Event e:ev){
if(e.Send_Survey__c==true && e.Event_Status__c=='Completed' && e.survey_sent__c==false){ // if e.survey_sent__c is true then abort
System.debug('##### Survey box checked, processing Event with ID = ' + e.id);
If(e.WhoId != null){
EventMembers.add(e.WhoId);
EventMap.put(e.whoid, e.id);
e.survey_sent__c = true; // tried this with the update statement at the end and it won't work - also tried creating a 2nd list and updating that, also fails
}
}
}
}
If(EventMembers.size()>0){
string EmailFailure=null;
// generate list of contacts to send message to
System.debug('##### Creating List of Contacts to send message too... ');
List<Contact>myRecipients = [SELECT id, firstname, lastname, email FROM contact WHERE id IN :EventMembers AND (NOT email LIKE '%forbidden%') ];
// create email objects as a list
System.debug('##### Creating list of messages ...');
List<Messaging.SingleEmailMessage> allMsg = new List<Messaging.SingleEmailMessage>();
OrgWideEmailAddress owa = [SELECT id, DisplayName, Address FROM OrgWideEmailAddress WHERE DisplayName='Global Sender' LIMIT 1];
// append new message to above list for each Contact
For (Contact c:myRecipients){
Id EvId = EventMap.get(c.id);
String theMsg = ('Please click the link below, or copy and paste into your browser to complete a brief survey ');
theMsg = theMsg + ('related to your recent interaction with an awesome service representative. \n \n ');
theMsg = theMsg + ('http://www.thethirdparty.com/survey?iv=############&q1=' + EvId + '&q2=' + c.id);
System.debug('##### Creating an instance of the message ...');
try{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayname(owa.DisplayName);
mail.setReplyTo(owa.Address);
mail.setTargetObjectId(c.id);
mail.setUseSignature(false);
mail.setSubject('Please complete this survey from the company.');
mail.setPlainTextBody(theMsg);
mail.setSaveAsActivity(true);
System.debug('##### Sending message ...');
Messaging.SendEmailResult[] mailResult=Messaging.sendemail(new Messaging.SingleEmailMessage[]{mail});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
if (!mailResult[0].isSuccess() ){
EmailFailure = 'Email Failed: ' + mailResult[0].getErrors()[0].getMessage();
}
}
catch(Exception Exp){
EmailFailure='Exception: ' + Exp.getCause();
}
}
}
}
}
All Answers
Can you please put in the code that fails?
----
Have you checked whether the email is sent out successfullly? (In Sandboxes and Dev Boxes, email's won't go).
-----
Can you try, putting that update, one line above Messaging.sendemail statement and see whether that works?
Best Regards,
JG
__________ this actually updates the field, but fails to block the recursive execution _________
If (ev!=null){
For (Event e:ev){
if(e.Send_Survey__c==true && e.Event_Status__c=='Completed' && e.survey_sent__c==false){ // if e.survey_sent__c is true then abort
System.debug('##### Survey box checked, processing Event with ID = ' + e.id);
If(e.WhoId != null){
EventMembers.add(e.WhoId);
EventMap.put(e.whoid, e.id);
e.survey_sent__c = true; // ****** this updates field, but doesn't block the recursive execution.
__________________________________________________________________________