• neha pandey 25
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
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.
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.