You need to sign in to do that
Don't have an account?
Nicole Young 2
Currently, my apex class is doing exactly what I want it to. Then i decided to test if one of the values was left blank and it failed. How do I prevent the split function from failing when a value isn't there?
global class CreateLeadEmailService implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String leadLastName=email.plaintextbody.split('Last Name: ').get(1).split('Company Name').get(0);
String leadFirstName=email.plaintextbody.split('First Name: ').get(1).split('Last Name').get(0);
String leadCompany= email.plaintextbody.split('Company Name: ').get(1).split('Phone').get(0);
String leadPhone= email.plaintextbody.split('Phone: ').get(1).split('Email:').get(0);
String leadEmail=email.plaintextbody.split('Email: ').get(1).split('<mailto').get(0);
String leadState=email.plaintextbody.split('Region: ').get(1).split('Size of').get(0);
String leadProduct=email.plaintextbody.split('Product: ').get(1).split('Plan').get(0);
String leadPlan=email.plaintextbody.split('Plan: ').get(1).split('Site:').get(0);
String leadDes=email.plaintextbody;
String leadSub=email.subject;
String leadCustomerSize=email.plaintextbody.split('Base: ').get(1).split('\\.00').get(0).replaceAll(',','');
Integer leadSize= Integer.valueOf(email.plaintextbody.split('Base: ').get(1).split('\\.00').get(0).replaceAll(',',''));
Lead l=new Lead(LastName=leadLastName, FirstName=leadFirstname, Email=leadEmail, Subject_del__c=leadSub, Description=leadDes, Company=leadCompany,
Phone=leadPhone, Availity_Market_Region__c=leadState, Plan__c=leadPlan, Product__c=leadProduct, Size_of_Customer_Base__c=leadSize );
insert l;
if(email.textAttachments != null)
{
// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = l.Id;
insert attachment;
}
}
if(email.binaryAttachments != null)
{
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = l.Id;
insert attachment;
}
}
return result;
}
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope){
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
String leadLastName=email.plaintextbody.split('Last Name: ').get(1).split('Company Name').get(0);
String leadFirstName=email.plaintextbody.split('First Name: ').get(1).split('Last Name').get(0);
String leadCompany= email.plaintextbody.split('Company Name: ').get(1).split('Phone').get(0);
String leadPhone= email.plaintextbody.split('Phone: ').get(1).split('Email:').get(0);
String leadEmail=email.plaintextbody.split('Email: ').get(1).split('<mailto').get(0);
String leadState=email.plaintextbody.split('Region: ').get(1).split('Size of').get(0);
String leadProduct=email.plaintextbody.split('Product: ').get(1).split('Plan').get(0);
String leadPlan=email.plaintextbody.split('Plan: ').get(1).split('Site:').get(0);
String leadDes=email.plaintextbody;
String leadSub=email.subject;
String leadCustomerSize=email.plaintextbody.split('Base: ').get(1).split('\\.00').get(0).replaceAll(',','');
Integer leadSize= Integer.valueOf(email.plaintextbody.split('Base: ').get(1).split('\\.00').get(0).replaceAll(',',''));
Lead l=new Lead(LastName=leadLastName, FirstName=leadFirstname, Email=leadEmail, Subject_del__c=leadSub, Description=leadDes, Company=leadCompany,
Phone=leadPhone, Availity_Market_Region__c=leadState, Plan__c=leadPlan, Product__c=leadProduct, Size_of_Customer_Base__c=leadSize );
insert l;
if(email.textAttachments != null)
{
// Save attachments, if any
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
Attachment attachment = new Attachment();
attachment.Name = tAttachment.fileName;
attachment.Body = Blob.valueOf(tAttachment.body);
attachment.ParentId = l.Id;
insert attachment;
}
}
if(email.binaryAttachments != null)
{
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
Attachment attachment = new Attachment();
attachment.Name = bAttachment.fileName;
attachment.Body = bAttachment.body;
attachment.ParentId = l.Id;
insert attachment;
}
}
return result;
}
Please try like below