function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nicole Young 2Nicole 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;
    }
Amit Chaudhary 8Amit Chaudhary 8
Please add Null check and add Contains Keyword to fix it

Please try like below
global class CreateLeadEmailService implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope)
	{
	
		String leadLastName;
		String leadFirstName;
		String leadCompany;
		String leadPhone;
		
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
		Lead l=new Lead();

		if(email.plaintextbody.contains('Last Name: '))
		{
			leadLastName=email.plaintextbody.split('Last Name: ').get(1).split('Company Name').get(0);
			l.LastName=leadLastName;
		}

		if(email.plaintextbody.contains('First Name: '))
		{
			leadFirstName=email.plaintextbody.split('First Name: ').get(1).split('Last Name').get(0); 
			l.FirstName=leadFirstname;
		}

		if(email.plaintextbody.contains('Company Name: '))
		{
			leadCompany= email.plaintextbody.split('Company Name: ').get(1).split('Phone').get(0);
			l.Company=leadCompany;
		}
		
		...................	

			
        
        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;
    }

 
Nicole Young 2Nicole Young 2
Hi Amit! Thanks for your quick reply. Line 14 you are checking to see if 'Last Name: ' is in the email. That is the name of the field and will always be there. I need to be prepared for if the actual last name doesn't follow that field name i.e. Smith, Wilson, Young. Is that possible?