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
neha pandey 25neha pandey 25 

Apex class (Email Services)

Hello,
 I want to use Email service in salesforce so that when  i send mail through gmail then lead gets created in salesforce i tried it its working fine but i want to send gmail in specific format then it send me exception.

Please help me to fix out this issue..
global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {
 global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env){

    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
    
     Lead[] newLead = new Lead[0];
         String myPlainText= ''; 
    	myPlainText = email.plainTextBody; 
    // String firstName = email.fromname.substring(0,email.fromname.indexOf(' '));
     //String lastName = email.fromname.substring(email.fromname.indexOf(' '));
     //String Emailaddress = env.fromAddress;
       String strEmailId = email.fromAddress;
      
    system.debug('email.fromAddress'+strEmailId);
     String[] emailBody = email.plainTextBody.split('\n', 0);
      String firstName;
     String lastName; 
      String jobtitle;
      String city;
      String CompanyName;
      String Notes;
      String url;  
      String[] url2;
      Integer i;
      list<String> newlist=new list<String>();
     String textBody = email.plainTextBody;
    	String blank = '';
	 String nullString = null;
	 String whitespace = '  ';
      
     //list<String> newlist=new list<String>();
     //String firstName; // insert code here
    /* Integer index = -1;
	for (Integer j=0;j<emailBody[j].length;j++) {
    if (emailBody[j].equals(firstName)) {
        index = j;
        break;
    }
	}*/
	
     
  if(emailBody.size()>0 && emailBody[0]!=null && emailBody[0]!='')
     {
     	//System.assert(!String.isNotBlank(blank));
		//System.assert(!String.isNotBlank(nullString));
		//System.assert(!String.isNotBlank(whitespace));
     	firstName = emailBody[0].substring(11);
      system.debug('emailBody[0].substring(11)'+emailBody[0].substring(11));
     } 
    if(emailBody.size()>1 && emailBody[1]!=null && emailBody[1]!='')
     {
     lastName = emailBody[1].substring(5);
     system.debug('emailBody[1].substring(10)'+emailBody[1].substring(10));
      }
    if(emailBody.size()>2 && emailBody[2]!=null && emailBody[2]!='')
      {
   /*if(textBody.indexOf('Job Title:') > -1) {
                Integer startPos = textBody.indexOf('Job Title:');
                Integer endPos = textBody.indexOf('.',textBody.indexOf('Job Title:'));
                jobtitle = textBody.substring(startPos+'Job Title:'.length(),endPos);
                jobtitle = emailBody[2].substring(10);
            }*/
     jobtitle = emailBody[2].substring(10);
     system.debug('emailBody[2].substring(10)'+emailBody[2].substring(10));
      }
    if(emailBody.size()>3 && emailBody[3]!=null && emailBody[3]!='')
      {
      	/*if(textBody.indexOf('Company Name:') > -1) {
                Integer startPos = textBody.indexOf('Company Name:');
                Integer endPos = textBody.indexOf('.',textBody.indexOf('Company Name:'));
                companyName = textBody.substring(startPos+'Company Name:'.length(),endPos);
                CompanyName = emailBody[3].substring(10); 
            }*/
      CompanyName = emailBody[3].substring(10); 
     system.debug('emailBody[3].substring(10)'+emailBody[3].substring(10));
      }
    if(emailBody.size()>4 && emailBody[4]!=null && emailBody[4]!='')
      {
      	/*if(textBody.indexOf('City:') > -1) {
                Integer startPos = textBody.indexOf('City:');
                Integer endPos = textBody.indexOf('.',textBody.indexOf('City:'));
                city = textBody.substring(startPos+'City:'.length(),endPos);
                city = emailBody[4].substring(5);
            }*/
      city = emailBody[4].substring(5);
      system.debug('emailBody[4].substring(5)'+emailBody[4].substring(5));
      }
     if(emailBody.size()>5 && emailBody[5]!=null && emailBody[5]!='')
      {
      /*if(textBody.indexOf('Notes:') > -1) {
                Integer startPos = textBody.indexOf('Notes:');
                Integer endPos = textBody.indexOf('.',textBody.indexOf('Notes:'));
                Notes = textBody.substring(startPos+'Notes:'.length(),endPos);
               Notes = emailBody[5].substring(6);
            }*/
       Notes = emailBody[5].substring(6);
       system.debug('emailBody[5].substring(6)'+emailBody[5].substring(6));
      }
    for( i=6; i<100;i++){
    
   if(emailBody.size()==i){
      system.debug(i);
             break;
     } 
     
      if(emailBody[i]!=null && emailBody[i]!='')
      {
      system.debug(i);
      system.debug(emailBody.size());
       url = emailBody[i].substring(3);
       system.debug('emailBody[i].substring(6)'+emailBody[i].substring(6));
       //url1 = emailBody[6].substring(4);
       url2 = url.split('/n');      
      // system.debug(url);
       system.debug(url);  
          system.debug(url2); 
         
          newlist.add('<a href="'+url+'">'+url+'</a><br/>');
          system.debug('newlist'+newlist);
      }
    }  
    
   // String strSubject = email.subject;
   // String myText=email.plainTextBody;
    //String myFromName = email.fromName;
    
    Lead[] leads = [SELECT Id, Name, Email,City , Title,Company,Description FROM Lead WHERE Email = :email.fromAddress];
   try{
      newLead.add(
      new Lead(
      Email = strEmailId,
      FirstName=firstName,
      LastName=lastName,
      City =city,
      Title=jobtitle ,
      Company=companyName,
      Description=Notes,
      URL__c=String.valueOf(newlist)));
      insert newLead;    
      System.debug('New Lead record: ' + newLead );   
    } 
   catch (System.DmlException e)
    {
      System.debug('Incoming email duplicates existing Lead record(s): ' + leads );    
    }
    
      result.success = true;
    return result;
    
 }

}

now its giving me output in this way..

First Name: vishu21
Last Name: Kashyap
Job Title:  Developer
Company Name :TMC
City: Noida
Notes:testing data
 
url:http://www.unisteeltech.com/solutions/fasteners.html
      http://www.unisteeltech.com/solutions/fasteners.html

But i want to see my output in this format.

Name
        Carrie Santi DeLucchi
Email
        carriesanti@gmail.com<mailto:carriesanti@gmail.com>
Job Title
        Web Developer
Company Name
        Peak Interactive
City
        Oakland
Notes
        This is a test of the 3D printing form.
Attach your files here


  *   whats-happening-drop-down.jpg<http://3dprinting.uct.com/wp-content/uploads/gravity_forms/1-94a0761c56774dc9cb019cf78434661e/2015/09/whats-happening-drop-down.jpg>

Means it firstly search for 'Name' which is start position and Email which is anywhere in email body as end position like wise for all others..
Please help me to fix out this issue.
Prabhat Kumar12Prabhat Kumar12
Use line break in your code something like this '\n'
neha pandey 25neha pandey 25
String[] emailBody = email.plainTextBody.split('\n', 0);
we are already using in this way but its not working.
neha pandey 25neha pandey 25
for(j=0; j<1000;j++){
                    if(emailBody.size()==j){
                      system.debug(j);
                         break;
                     } 
                       if( emailBody[j].length()>0 && emailBody[j]!=null && emailBody[j]!='')
                         {
                              if(emailBody[j].contains('Name'))
                        {
                             String textBody1 = emailBody[j].substring(emailBody[j].indexOf('Name')+4);
                                textBody1 =emailBody[j].substring(0, emailBody[j].indexOf('Company Name'));
                                //textBody1 =textBody[j].substring(textBody[j].indexOf('Email'));
                                lastName=textBody1;
                                system.debug('textBody of lastName'+textBody1);
                            //    String lastName1=lastName.split('\n');
                             system.debug('textBody'+lastName);
                            
                         } 
                     }

i write code in this way but getting index out of bounds -1 exception.