ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Email Services : Apex Class to unsubscribe Email

Home / Article & Blogs / Uncategorized / Email Services : Apex Class to unsubscribe Email

Email Services : Apex Class to unsubscribe Email

By Haseeb Khan inUncategorized

In Continuation to the Previous Blog i have written Titled  ,

Email Services in Salesforce with a simple example (https://wordpress.com/post/teamforcesite.wordpress.com/566)

Here is a requirement which can apply to almost any usage of the email services feature of salesforce . The unsubscribe Email functionality , is the feature by code where you can give your users an option to unsubscribe to further marketing emails from this address , Or our salesforce org.

Companies that send marketing email to their customers and prospects need to provide a way to let the recipients unsubscribe.
The following is an example of how an email service can process unsubscribe requests.
The code searches the subject line of inbound email for the word “unsubscribe.”
If the word is found, the code finds all contacts and leads that match the From email address and sets theEmail Opt Out field (HasOptedOutOfEmail) to True.

Scenario  : Handle Unsubscribe Email

[sourcecode language=”java”]
Global class unsubscribe implements Messaging.inboundEmailHandler {
Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope env) {

// Create an inboundEmailResult object for returning
// the result of the email service.
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

// Create 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 the program can match on lower case.
String mySubject = email.subject.toLowerCase();
// The search string used in the subject line.
String s = ‘unsubscribe’;

// Check the variable to see if the word unsubscribe was found in the subject line.
Boolean unsubMe;
// Look for the word unsubcribe in the subject line.
// If it is found, return true; otherwise, return false.
unsubMe = mySubject.contains(s);

// If unsubscribe is found in the subject line, enter the IF statement.
if (unsubMe == true) {
try {
// Look up all contacts with a matching email address.
for (Contact c: [SELECT Id, Name, Email, HasOptedOutOfEmail
FROM Contact
WHERE Email = : env.fromAddress
AND hasOptedOutOfEmail = false
LIMIT 100
]) {
// Add all the matching contacts into the list.
c.hasOptedOutOfEmail = true;
lc.add(c);
}
// Update all of the contact records.
update lc;
} catch (Exception e) {
System.debug(‘Contact Issue: ‘ + e);
}
try {
// Look up all leads matching the email address.
for (Lead l: [SELECT Id, Name, Email, HasOptedOutOfEmail
FROM Lead
WHERE Email = : env.fromAddress
AND isConverted = false
AND hasOptedOutOfEmail = false
LIMIT 100
]) {
// Add all the leads to the list.
l.hasOptedOutOfEmail = true;
ll.add(l);
System.debug(‘Lead Object: ‘ + l);
}
// Update all lead records in the query.
update ll;
} catch (Exception e) {
System.debug(‘Lead 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 confirms program is complete and no emails
// should be sent to the sender of the unsubscribe request.
result.success = true;
return result;
}
}
[/sourcecode]

Create this class and assign it to the Email Service you have configured.
This will automatically receive the emails and on receiving an email
with subject ‘Unsubscribe’ will search for contacts and leads and
Check the ‘hasOptedOutOfEmail’ checkbox Field to be true.

Happy Coding!!!

74
Like this post
4 Posts
Haseeb Khan

Search Posts

Archives

Categories

Recent posts

International SEO: How to Get Website Traffic from Other Countries

International SEO: How to Get Website Traffic from Other Countries

Top Reasons to Use Salesforce Consumer Goods Cloud

Top Reasons to Use Salesforce Consumer Goods Cloud

What should Gay Pride Month mean to an organization?

What should Gay Pride Month mean to an organization?

10 Amazing Benefits of Salesforce Marketing Cloud

10 Amazing Benefits of Salesforce Marketing Cloud

How to Set SMART Goals for Employee Performance?

How to Set SMART Goals for Employee Performance?

  • Previous PostCRUD in Lightning
  • Next PostProcess automation with the help of visual workflows

Related Posts

HOW THE TALENTS ARE TRAINED AT ABSYZ
Uncategorized

HOW THE TALENTS ARE TRAINED AT ABSYZ

Why choose Salesforce Integration Partners?
Salesforce Implementation Uncategorized

Why choose Salesforce Integration Partners?

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

HYD – India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

2nd Floor, 91Springboard, Padmavathi Complex, 80 Feet Rd, Koramangala, 8th Block, Bengaluru, Karnataka-560095.
+91 8886332345

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT