Last active
June 18, 2019 15:00
-
-
Save arun12209/58ece7463604e1a708ba3ea97989047d to your computer and use it in GitHub Desktop.
MCWebhookCntrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public without sharing class MCWebhookCntrl { | |
public PageReference UpdateUnsubscribeMembers() { | |
String sCode = fetchSecrateCode(); | |
System.debug('***Secrate Code: ### ' + sCode ); | |
String MCrequestCode = ApexPages.currentPage().getParameters().get('code'); | |
if (MCrequestCode == null || !sCode.equals(MCrequestCode )) { | |
System.debug('Code is not valid: Please use valid code: ' + MCrequestCode ); | |
return null; | |
} | |
String eventType= ApexPages.currentPage().getParameters().get('type'); | |
system.debug('Type of event: '+ eventType); | |
if(eventType == 'upemail'){ //Email Changed event | |
// Getting New Email | |
String newEmail = ApexPages.currentPage().getParameters().get('data[new_email]'); | |
system.debug('Changed Email: ' +newEmail); | |
// Getting Old Email | |
String oldEmail = ApexPages.currentPage().getParameters().get('data[old_email]'); | |
system.debug('Old Email: ' +oldEmail ); | |
if (oldEmail == null || oldEmail.length() <= 0) { | |
System.debug('Email Id is null or blank'); | |
return null; | |
} | |
else { | |
System.debug('Email for Email Opt Out: ' + oldEmail ); | |
} | |
String safeEmail = String.escapeSingleQuotes(oldEmail); | |
safeEmail.replace('*', '\\*'); | |
safeEmail.replace('?', '\\?'); | |
// SOSL search for records (Contact) | |
List<List<SObject>> searchList = [FIND :oldEmail IN EMAIL FIELDS RETURNING Contact(id) limit 1]; | |
//Contact List | |
List<Contact> contacts = ((List<Contact>)searchList[0]); | |
// Getting first record. you can iterate the list to get all records. | |
List<Contact> conListForUpdate = [select id,Email,MailChimpActivity__c from Contact where id =: contacts[0].id ]; | |
for (Contact c : conListForUpdate ) { | |
if( c.MailChimpActivity__c ==null) | |
c.MailChimpActivity__c =''; | |
c.MailChimpActivity__c += 'Email chnaged from '+c.Email+ ' to '+newEmail+' \r\n' ; | |
c.Email = newEmail; | |
} | |
// Update Lists | |
if (conListForUpdate.size() > 0) { | |
update conListForUpdate; | |
} | |
} | |
// Unsubscribe/ Subscribe | |
else if(eventType != 'profile'){ | |
String emailAddr = ApexPages.currentPage().getParameters().get('data[email]'); | |
if (emailAddr == null || emailAddr .length() <= 0) { | |
System.debug('Email Id is null or blank'); | |
return null; | |
} | |
else { | |
System.debug('Email for Email Opt Out: ' + emailAddr ); | |
} | |
String safeEmail = String.escapeSingleQuotes(emailAddr); | |
safeEmail.replace('*', '\\*'); | |
safeEmail.replace('?', '\\?'); | |
// SOSL search for records (Contact/Lead) | |
List<List<SObject>> searchList = [FIND :emailAddr IN EMAIL FIELDS RETURNING Contact(id), Lead(id) ]; | |
//Contact List | |
List<Contact> contacts = ((List<Contact>)searchList[0]); | |
//Lead List | |
List<Lead> leads = ((List<Lead>)searchList[1]); | |
for (Contact c : contacts) { | |
if(eventType =='subscribe'){ | |
c.HasOptedOutOfEmail = false; | |
}else if(eventType =='unsubscribe'){ | |
c.HasOptedOutOfEmail = true; | |
} | |
} | |
for (Lead l : leads) { | |
if(eventType =='subscribe'){ | |
l.HasOptedOutOfEmail = false; | |
}else if(eventType =='unsubscribe'){ | |
l.HasOptedOutOfEmail = true; | |
} | |
} | |
// Update Lists | |
if (contacts.size() > 0) { | |
update contacts; | |
} | |
if (leads.size() > 0) { | |
update leads; | |
} | |
} | |
return null; | |
} | |
public String fetchSecrateCode() { | |
try { | |
StaticResource sr = [ SELECT Body, Name FROM StaticResource WHERE Name = 'WebHooksConfig' LIMIT 1 ]; | |
String xml = sr.Body.toString(); | |
System.debug('XML data: ' + xml); | |
//XML parsing | |
Dom.Document doc = new Dom.Document(); | |
doc.load(xml); | |
Dom.XMLNode webhooks = doc.getRootElement(); | |
for (Dom.XMLNode node : webhooks.getChildElements()) { | |
String id = node.getAttribute('id', ''); | |
if (id.equalsIgnoreCase('unsubscribe')) { | |
return node.getAttribute('code', ''); | |
} | |
} | |
} | |
catch (Exception e) { | |
System.debug('*** Something went wrong: Error Occured! : ' + e.getMessage()); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment