I have an Email Service that creates Leads from 3rd party vendors. We now want to accept a UTM parameter, but the vendor can’t concatenate. In my class, how can I turn this (which is in the body of the email):
Marketing Referrer Content: Facebook
Marketing Referrer Medium: Displaypro
Marketing Referrer Campaign: Q32022_display
Marketing Referrer Source: Google
Marketing Referrer Term: 50Visa
Into this:
?utm_medium=displaypro&utm_source=google&utm_campaign=Q32022_Display&utm_content=facebook&utm_term=50Visa
For Additional Context: Sample Email:
A new lead from Lead Gen tool!
Submitted Form Data
Email: testing2@test.com
First Name: Dev2
Last Name: TestLead
Phone: 310-555-5555
Zip Code: 91403
Marketing Referrer Content: Facebook
Marketing Referrer Medium: Displaypro
Marketing Referrer Campaign: Q32022_display
Marketing Referrer Source: Google
Marketing Referrer Term: 50Visa
Current Class
//Description: This class accepts a a structured email from a 3rd party vendor and attempts to create a new Lead
public class EmailServiceInboundLead implements Messaging.InboundEmailHandler{
public 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();
Lead newLead = new Lead();
newLead.FirstName = emailSearch(email.plainTextBody, 'First Name:');
newLead.LastName = emailSearch(email.plainTextBody, 'Last Name:');
newLead.Email = emailSearch(email.plainTextBody,'Email:');
newLead.PostalCode = emailSearch(email.plainTextBody, 'Postal Code:');
newLead.Company = 'not provided';
newLead.Phone = emailSearch(email.plainTextBody,'Phone:');
newLead.Description = email.plainTextBody;
//variable to hold any errors
Set <String> apexErrors = new Set<String>();
try{
insert newLead;
} catch(DmlException e){
apexErrors.add(e.getMessage() + ' Email Body: ' + email.plainTextBody);
}
//Use values from the Email to create a new EmailMessage
EmailMessage emailMessage = new EmailMessage();
emailMessage.status = '3'; // email was sent
emailMessage.subject = email.subject;
emailMessage.htmlBody = email.htmlBody;
emailMessage.TextBody = email.plainTextBody;
String[] toIds = new String[] {newLead.id};
emailMessage.toIds = toIds;
try{
insert emailMessage;
}catch(DmlException e){
apexErrors.add(e.getMessage() + ' Email Body: ' + email.plainTextBody);
}
// Associate the EmailMessage to the Lead
EmailMessageRelation r = new EmailMessageRelation();
r.emailMessageId = emailMessage.Id;
r.relationId = newLead.id;
r.relationType = 'FromAddress';
try{
insert r;
}catch(DmlException e){
apexErrors.add(e.getMessage() + ' Email Body: ' + email.plainTextBody);
}
//Run Lead assignment Rule
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.AssignmentRuleHeader.useDefaultRule = TRUE;
try{
Database.update(newLead,dmo);
}catch(DmlException e){
apexErrors.add(e.getMessage() + ' Email Body: ' + email.plainTextBody);
}
//if there were any errors, send to Admin
if (!apexErrors.isEmpty()) {
ZocdocEmailUtility.sendAdminEmail('EmailServiceInboundLead Error', String.join(new List < String > (apexErrors), '<br/>'), !Test.isRunningTest());
}
result.success = true;
return result;
}
//Parse through the email body to find a value and return its value
public static String emailSearch(String emailBody, String type){
if(String.isEmpty(emailBody)) return null;
String stringLine;
String returnString;
//Split the email body lines out by returns (\n)
for(String msgBodyLine : emailBody.split('\n')) {
//if the current line has the text passed into the method in it grab the entire line and assign it to the stringLine variable
if(!String.isEmpty(msgBodyLine) && msgBodyLine.containsIgnoreCase(type)) {
stringLine = msgBodyLine;
system.debug('stringLine = ' + stringLine);
break;
}
}
//If the stringLine variable is not null trim the string at the colon which will provide you with a value.
if(stringLine != null) {
system.debug('stringLine search = ' + stringLine);
returnString = stringLine.split(':')[1].trim();
}
//return the found STRING
return returnString;
}
}