You need to sign in to do that
Don't have an account?
Customize Email Opt Out App
I am trying to customize the email opt out app in order to update a custom field HasOptOutCampaign__c which has been created on both the lead and contact objects. Everything appears to be correct and I did not receive any error when saving this code but when I try to create an email service I receive the following error message:
- The Apex Class selected is not valid. An Apex Class that implements the Messaging.InboundEmailHandler interface must be selected.
Does anyone know how I might be able to correct this? I will paste the code below.
Thanks so much.
Global class optOutCampaign implements Messaging.inboundEmailHandler{
Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope env ) {
// Create an inboundEmailResult object for returning
//the result of the Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
// Contact and Lead lists to hold all the updated records
List<Contact> lc = new List <contact>();
List<Lead> ll = new List <lead>();
// Convert the subject line to lower case, so I can match on lower case
String mySubject = email.subject.toLowerCase();
// String I am searching for in the subject line
String s = 'campaign opt out';
// Check variable to see if the words "campaign opt out" were found in the subject line
Boolean unsubMe;
// Look for the unsubcribe word in the subject line,
// if it is found return true, otherwise false is returned
unsubMe = mySubject.contains(s);
// If unsubscribe is found in the subject line enter the if statement
if (unsubMe == true) {
try {
// Lookup all contacts with a matching email address
for (Contact c : [Select Id, Name, Email, HasOptOutCampaign__c
From Contact
Where Email = :env.fromAddress
And HasOptOutCampaign__c = false
Limit 100]) {
// Add all the contacts into the List
c.HasOptOutCampaign__c = true;
lc.add(c);
}
// update all the Contact records
update lc;
}
catch (System.QueryException e) {
System.debug('Contact Query Issue: ' + e);
}
try {
// Lookup all leads matching the email address
for (Lead l : [Select Id, Name, Email, HasOptOutCampaign__c
From Lead
Where Email = :env.fromAddress
And isConverted = false
And HasOptOutCampaign__c = false
Limit 100]) {
// Add all the leads to the List
l.HasOptOutCampaign__c = true;
ll.add(l);
System.debug('Lead Object: ' + l);
}
// Update all Lead records in the query
update ll;
}
catch (System.QueryException e) {
System.debug('Lead Query Issue: ' + e);
}
System.debug('Found the campaign opt out word in the subject line.');
}
else {
System.debug('No Campaign Opt out word found in the subject line.' );
}
// Return true and exit
// True will confirm it is complete and no bounced email
// should be send the sender of the unsubscribe request.
result.success = true;
return result;
}
// Test method to ensure you have enough code coverage
// Have created two methods, one that does the testing
// with a valid "unsubcribe" in the subject line
// and one the does not contain "unsubscribe" in the
// subject line
static testMethod void testoptoutcampaign() {
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
// Create a new test Lead and insert it in the Test Method
Lead l = new lead(firstName='Rasmus',
lastName='Mencke',
Company='Salesforce',
Email='rmencke@salesforce.com',
HasOptOutCampaign__c=false);
insert l;
// Create a new test Contact and insert it in the Test Method
Contact c = new Contact(firstName='Rasmus',
lastName='Mencke',
Email='rmencke@salesforce.com',
HasOptOutCampaign__c=false);
insert c;
// test with subject that matches the unsubscribe statement
email.subject = 'test campaign opt out test';
env.fromAddress = 'rmencke@salesforce.com';
// call the class and test it with the data in the testMethod
optoutcampaign optoutcampaignObj = new optoutcampaign();
optoutcampaignObj.handleInboundEmail(email, env );
}
static testMethod void testUnsubscribe2() {
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
// Create a new test Lead and insert it in the Test Method
Lead l = new lead(firstName='Rasmus',
lastName='Mencke',
Company='Salesforce',
Email='rmencke@salesforce.com',
HasOptOutCampaign__c=false);
insert l;
// Create a new test Contact and insert it in the Test Method
Contact c = new Contact(firstName='Rasmus',
lastName='Mencke',
Email='rmencke@salesforce.com',
HasOptOutCampaign__c=false);
insert c;
// Test with a subject that does Not contain campaign opt out
email.subject = 'test';
env.fromAddress = 'rmencke@salesforce.com';
// call the class and test it with the data in the testMethod
optoutcampaign optoutcampaignObj = new optoutcampaign();
optoutcampaignObj.handleInboundEmail (email, env);
}
}