1

I am attempting to set up an email-to-lead email service. Our non-profit receives leads in the content of emails that are sent, and I am trying to write an apex class that correctly parses the information from the email. This is my first attempt at learning apex code, so I appreciate your patience with my noob understanding. Here is my apex code so far:

global class NmsEmailToLead implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,

                                                       Messaging.InboundEnvelope env){


    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();


    String[] emailBody = email.plainTextBody.split('\n', 0);

    String firstName = emailBody[1].substring(10);

    String lastName = emailBody[2].substring(10);

    String address = emailBody[3].substring(8);

    String state = emailBody[4].substring(5);

    String zipcode = emailBody[5].substring(7);

    String city = emailBody[6].substring(4);

    String leadEmail = emailBody[7].substring(12);

    String phone = emailBody[8].substring(8);

    String paymentsBehind = emailBody[11].substring(15).trim();

    String loanAmount = emailBody[13].substring(11).trim();

    String estimatedHomeValue = emailBody[14].substring(21).trim();

    String creditLevel = emailBody[17].substring(12);

    String interestRate = emailBody[20].substring(14).trim();

    Lead[] newLead = new Lead[0];


     try {
        Map<String, Schema.SObjectType> sObjectMap = Schema.getGlobalDescribe() ;
        Schema.SObjectType s = sObjectMap.get('Lead') ;
        Schema.DescribeSObjectResult resSchema = s.getDescribe() ;
        Map<String,Schema.RecordTypeInfo> recordTypeInfo = resSchema.getRecordTypeInfosByName();
        Id rtId = recordTypeInfo.get('Housing Counseling').getRecordTypeId();

       newLead.add(new Lead(RecordTypeId = rtId,
       FirstName=firstName,LastName=lastName,Phone = phone,Email = leadEmail,
       Street = address,State = state,City = city,PostalCode = zipcode,
       Payments_Behind__c = decimal.valueof(paymentsBehind),Total_Amount__c = decimal.valueof(loanAmount),
       Estimated_Home_Value__c = decimal.valueof(estimatedHomeValue),
       Credit_Level__c = creditLevel,Interest_Rate__c = decimal.valueof(interestRate)));

       insert newLead;       
    }

   catch (QueryException e) {
       System.debug('Lead Query Issue: ' + e);

   }


   result.success = true;

   return result;

  }

}

Based on a suggestion from a question I already asked at the salesforce developer community here I am trying to add this helper method to this code somehow. From my understanding, I will add this helper method to Decimal somewhere in the code (at the top somewhere?) and then instead of calling decimal.valueof(string) in the try block I will be replacing those methods with decimal.getDecimalValue(string).

private Decimal getDecimalValue(String str){
    return String.isEmpty(str) || 'N/A' == str ? null : Decimal.valueOf(str);
}

However, my problem is that when I cut and paste this helper method into my apex class, anywhere I put it it gives me an error: Compile Error: expecting a semi-colon, found '(' I'm very new to Apex and so don't understand the syntax at all, and can't figure out why I am getting this error. Thanks in advance for your help.

3
  • 1
    I've added the helper method to one of my own classes. It saves without error there. Where exactly are you trying to paste it?
    – Jagular
    Commented Sep 22, 2014 at 17:41
  • I've tried to paste it between the ' Messaging.InboundEmailResult' and the 'String[] emailBody' lines, between the 'Lead[] newLead = new Lead[0];' line and the try block, and within the try block at the very beginning.
    – ben432rew
    Commented Sep 22, 2014 at 17:49
  • 1
    You might find it worth reading through the sections of Understanding Classes so you are clearer on what is possible. You need to put it before or after the handleInboundEmail method, with after being a more common style.
    – Keith C
    Commented Sep 22, 2014 at 17:49

3 Answers 3

1

I agree with the comment made by @KeithC, you shoudl take a look at some of the documentation and this will become a bit more clear to you.

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_classes_defining.htm|StartTopic=Content%2Fapex_classes_defining.htm|SkinName=webhelp

As far as your current question, this should work

global class NmsEmailToLead implements Messaging.InboundEmailHandler {

      global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
            //ALL YOUR CODE FOR THIS METHOD
      }

      private Decimal getDecimalValue(String str){
            return String.isEmpty(str) || 'N/A' == str ? null : Decimal.valueOf(str);
      }

}
1
  • Thanks, that's where I was able to put the helper method. I had assumed that once I put this decimal method in, I would be replacing the "decimal.valueof" functions in the try block with "decimal.getDecimalValue" instead, but I am getting a "Variable does not exist error". Do I not need to do this? I'm still getting a 'Invalid decimal: N/A' error when emails are being sent, do I need to change the ternary expression or just change something else in my apex code?
    – ben432rew
    Commented Sep 23, 2014 at 15:45
0

Try pasting the helper function between the last two "}" characters (curly braces). It should work there.

0

In addition to checking out the developer docs, it's saved me more than once to make sure I'm writing my code in place that's designed to help me catch errors. I'm also a non-profit developer & fairly new to coding in Apex, and getting familiar with the Force.com IDE has made it much easier to move through my code & catch where I've forgotten to close methods (i.e. pair curly brackets), etc. and to learn to understand those compile errors. There's a great into video from DF13 that covers the Force.com IDE.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .