You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use this to allow a user to unsubscribe from a newsletter via a reply email
// This is the updated code to be used for the Salesforce plugin: https://appexchange.salesforce.com/appxListingDetail?listingId=a0N300000016YDZEA2
// Install the plubin.
// Changout all of the Apex installed in the unsubscribe class with updated code below.
// Add the custom field hasOptedOutOfEmail__c (without the __c, salesforce will add that) to your Lead and Contact object.
// Create an email in Setup > Custom Code > Email Services (and make it active)
// Then just append ?subject=Unsubscribe to the end of the email and insert that link into any mailings.
// This allows a user to unsubscribe via email.
// Use a process to automate any changes when the hasOptedOutofEmail__c checkbox is changed to true. ie check the standard salesforce EmailOptOut or unsubscribe them from a specific list
// Further documentation can be found here: https://appexchange.salesforce.com/servlet/servlet.FileDownload?file=00P3000000P3Vv2EAF
GlobalclassunsubscribeimplementsMessaging.inboundEmailHandler{
Global Messaging.InboundEmailResulthandleInboundEmail(Messaging.InboundEmailemail,
Messaging.InboundEnvelopeenv ) {
// Create an inboundEmailResult object for returning //the result of the Apex Email ServiceMessaging.InboundEmailResultresult=newMessaging.InboundEmailResult();
// Contact and Lead lists to hold all the updated recordsList<Contact> lc=newList <contact>();
List<Lead> ll=newList <lead>();
// Convert the subject line to lower case, so I can match on lower caseStringmySubject=email.subject.toLowerCase();
// String I am searching for in the subject lineStrings='unsubscribe';
// Check variable to see if the word "unsubscribe" was found in the subject line BooleanunsubMe;
// Look for the unsubcribe word in the subject line, // if it is found return true, otherwise false is returnedunsubMe=mySubject.contains(s);
// If unsubscribe is found in the subject line enter the if statementif (unsubMe==true) {
try {
// Lookup all contacts with a matching email addressfor (Contactc: [SelectId, Name, Email, hasOptedOutOfEmail__cFromContactWhereEmail= :env.fromAddressAndhasOptedOutOfEmail__c=falseLimit100]) {
// Add all the contacts into the List c.hasOptedOutOfEmail__c=true;
lc.add(c);
}
// update all the Contact recordsupdatelc;
} catch (System.QueryExceptione) {
System.debug('Contact Query Issue: '+e);
}
try {
// Lookup all leads matching the email addressfor (Leadl: [SelectId, Name, Email, hasOptedOutOfEmail__cFromLeadWhereEmail= :env.fromAddressAndisConverted=falseAndhasOptedOutOfEmail__c=falseLimit100]) {
// Add all the leads to the List l.hasOptedOutOfEmail__c=true;
ll.add(l);
System.debug('Lead Object: '+l);
}
// Update all Lead records in the queryupdatell;
} catch (System.QueryExceptione) {
System.debug('Lead Query Issue: '+e);
}
System.debug('Found the unsubscribe word in the subject line.');
} else {
System.debug('No Unsuscribe word found in the subject line.' );
}
// Return true and exit// True will confirm it is complete and no bounced email // should be the sender of the unsubscribe request. result.success=true;
returnresult;
}
// 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 linestatic testMethod voidtestUnsubscribe() {
// Create a new email and envelope objectMessaging.InboundEmailemail=newMessaging.InboundEmail() ;
Messaging.InboundEnvelopeenv=newMessaging.InboundEnvelope();
// Create a new test Lead and insert it in the Test Method Leadl=newlead(firstName='Seph', lastName='Cordovano',
Company='Southern Cloud', Email='[email protected]',
HasOptedOutOfEmail__c=false);
insertl;
// Create a new test Contact and insert it in the Test Method Contactc=newContact(firstName='Josh', lastName='Robinson',
Email='[email protected]', HasOptedOutOfEmail__c=false);
insertc;
// Instantiate the classunsubscribeunsub=newunsubscribe();
// Set email for testing Lead with subject that matches the unsubscribe statementemail.subject='test unsubscribe test';
env.fromAddress='[email protected]';
// Test the class method against the Lead unsub.handleInboundEmail(email, env );
// Set email for testing Contact with subject that matches the unsubscribe statementenv.fromAddress='[email protected]';
// Test the class method against the Contact unsub.handleInboundEmail(email, env );
}
static testMethod voidtestNonUnsubscribe() {
// Create a new email and envelope objectMessaging.InboundEmailemail=newMessaging.InboundEmail() ;
Messaging.InboundEnvelopeenv=newMessaging.InboundEnvelope();
// Create a new test Lead and insert it in the Test Method Leadl=newlead(firstName='Seph', lastName='Cordovano',
Company='Southern Cloud', Email='[email protected]',
HasOptedOutOfEmail__c=false);
insertl;
// Create a new test Contact and insert it in the Test Method Contactc=newContact(firstName='Josh', lastName='Robinson',
Email='[email protected]', HasOptedOutOfEmail__c=false);
insertc;
// Instantiate the classunsubscribeunsub=newunsubscribe();
// Set email for testing Lead with subject that matches the unsubscribe statementemail.subject='test subject';
env.fromAddress='[email protected]';
// Test the class method against the Lead unsub.handleInboundEmail(email, env );
// Set email for testing Contact with subject that matches the unsubscribe statementenv.fromAddress='[email protected]';
// Test the class method against the Contact unsub.handleInboundEmail(email, env );
}
}