0
@IsTest
private class EmailToSalesforceTests {

    // Create a new email and envelope object
    Messaging.InboundEmail email = new Messaging.InboundEmail();
    Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();



    // Create the email body
    email.plainTextBody = 'This should become a note';
    email.fromAddress = '[email protected]';
    String contactEmail = '[email protected]';
    email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
    email.subject = 'Dummy Account Name 123';

    EmailToSalesforce edr = new EmailToSalesforce();

    Test.startTest();
    Messaging.InboundEmailResult result = edr.handleInboundEmail(email, env);
    Test.stopTest();

    System.assert (result.success, 'InboundEmailResult returned a failure message');

    Account [] accDb = [select ID from Account where name=:email.subject];
    System.assertEquals (1, accDb.size(),'Account was not inserted');
    Contact [] cDb = [select firstname,lastname from Contact where email=:contactEmail];
    System.assertEquals (1, cDb.size(),'Contact was not inserted!');
    Contact c = CDb[0];
    System.assertEquals ('Jon', c.firstName);
    System.assertEquals ('Smith', c.LastName);
    Note [] nDb = [select body from Note where ParentID=:accDb[0].id];
    System.assertEquals (1,nDb.size(), 'A note should have been attached');
    System.assertEquals (email.plainTextBody, nDb[0].body);

}

}

Hi All, above is my code, and I am receiving the Compile Error in the title. Any chance someone could assist me? I can't see anything wrong with the code, considering it was provided in the Salesforce documentation. Thanks.

6
  • Can you point out which line is line 11?
    – BarCotter
    Jun 19, 2015 at 15:29
  • 4
    All of the code above should be within a static testMethod void theNameOfYourMethod() { method and not defined directly in the scope of the class itself.
    – Mark Pond
    Jun 19, 2015 at 15:41
  • The code in question is this part: email.plainTextBody = 'This should become a note'; email.fromAddress = '[email protected]'; String contactEmail = '[email protected]'; email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'}; email.subject = 'Dummy Account Name 123'; Jun 19, 2015 at 15:44
  • 1
    There is a "Feedback" link at the bottom of the new documentation - if you use that to report the problem and fix (listed by Mark) you may save others this hassle in the future.
    – Keith C
    Jun 19, 2015 at 16:13
  • Well, the suggestion from Mark did not fix the error, I'm still getting that same issue with the unexpected token. email.plainTextBody = 'This should become a note'; ---- is that the correct code for that line? SOmeone suggest that i change it to email.setplainTextBody('This should become a note'); -- but even when i did that, still no luck. MARK - am I just to add the stats TestMethod void EmailtoSalesforce() to the top and close it at the bottom with another }? Jun 19, 2015 at 16:30

1 Answer 1

1

Please include your complete code in a method and than compile it below I have Corrected the code:

 @IsTest
private class EmailToSalesforceTests {

 static testmethod void TestEmailToSalesforce(){
// all your code here. Inside the test method

}
}
5
  • Hey Ajay... please indicate what you have changed.
    – Adrian Larson
    Jun 20, 2015 at 13:57
  • I have simply put your code into a testMethod. Jun 20, 2015 at 14:10
  • My code? Try again. When you answer a question, you should explain what changes you made and why in addition to any code.
    – Adrian Larson
    Jun 20, 2015 at 14:12
  • I added a test method in the test class. Because Apex Code requires the creation and execution of unit tests. Jun 20, 2015 at 14:38
  • 1
    So update your answer...you have a lot of code compared to your amount of explanation.
    – Adrian Larson
    Jun 20, 2015 at 14:48

You must log in to answer this question.

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