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
BakTBakT 

Email Services Test Class

Hi guys,

I tried following the email services sample code but I am running into a problem with the test class. For some reason it's giving me the error: Error: Compile Error: unexpected token: '=' at line 11 column 28

Here's the link to the sample email services: https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com 
Best Answer chosen by BakT
ShashForceShashForce
If you see the test method closely, there is a line missing. The top class is defined (EmailDemoReceiveHandlerTests), but there is a line missing to define the test class. You will also notice that there is an extra "}" in the code. So, obviously, one line is missing in the code. Please define the signature for the test method and you should be good.

All Answers

ShashForceShashForce
Hi,

Please check for any missing syntax that might be causing this, like a semi-colon before of after or on line 11 in your code.

Thanks,
Shashank
BakTBakT
That's the thing, everything before it is coded properly.

Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        // Set up your data if you need to
        
        // Create the email body
        email.plainTextBody = 'This should become a note'; //this is where the error comes up.


ShashForceShashForce
Can you paste the whole code so that I can test in my developer org?
BakTBakT
Here's the email service:
global class EmailDemoReceive implements Messaging.InboundEmailHandler {
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                         Messaging.Inboundenvelope envelope) {
                                                         Account account;
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

try {
// Look for account whose name is the subject and create it if necessary
if ([select count() from Account where Name = :email.subject] == 0) {
  account = new Account();
  account.Name = email.subject;
  insert account;
} else {
  account = [select Id from Account where Name = :email.subject];
}
for (String address : email.ccAddresses) {
  Contact contact = new Contact();
  Matcher matcher = Pattern.compile('<.+>').matcher(address);
 
  // Parse addresses to names and emails
  if (matcher.find()) {
    String[] nameParts = address.split('[ ]*<.+>')[0].replace('"', '').split('[ ]+');
   
    contact.FirstName = nameParts.size() > 1 ? nameParts[0] : '';
    contact.LastName = nameParts.size() > 1 ? nameParts[nameParts.size()-1] : nameParts[0];
    contact.Email = matcher.group().replaceAll('[<>]', '');
  } else {
    contact.LastName = address;
    contact.Email = address;
  }
 
  // Add if new
  if ([select count() from Contact where Email = :contact.Email] == 0) {
    contact.AccountId = account.Id;
    insert contact;
  }
}
// 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 = account.Id;
  insert attachment;
}
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
  Attachment attachment = new Attachment();
 
  attachment.Name = bAttachment.fileName;
  attachment.Body = bAttachment.body;
  attachment.ParentId = account.Id;
  insert attachment;
}
// Turn email body into note
Note note = new Note();

note.Title = email.fromName + ' (' + DateTime.now() + ')';
note.Body = email.plainTextBody;
note.ParentId = account.Id;
insert note;
result.success = true;
    } catch (Exception e) {
      result.success = false;
      result.message = 'Oops, I failed.';
    }
   
    return result;
  }
}

Here's the test class:
@IsTest
private class EmailDemoReceiveHandlerTests {

        // Create a new email and envelope object
        Messaging.InboundEmail email  = new Messaging.InboundEmail();
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        
        // Set up your data if you need to
        
        // Create the email body
        email.plainTextBody = 'This should become a note';
        email.fromAddress ='test@test.com<script type="text/javascript">/* <![CDATA[ */(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();/* ]]> */</script>';
        String contactEmail = 'jsmith@salesforce.com<script type="text/javascript">/* <![CDATA[ */(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();/* ]]> */</script>';
        email.ccAddresses = new String[] {'Jon Smith <' + contactEmail + '>'};
        email.subject = 'Dummy Account Name 123';
        
        EmailDemoReceive edr = new EmailDemoReceive();
        
        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);
        
    } 
}


ShashForceShashForce
If you see the test method closely, there is a line missing. The top class is defined (EmailDemoReceiveHandlerTests), but there is a line missing to define the test class. You will also notice that there is an extra "}" in the code. So, obviously, one line is missing in the code. Please define the signature for the test method and you should be good.
This was selected as the best answer
BakTBakT

Ermahgerd! LOL

This is embarassing. Thanks a lot! You would think they check the code they post on the tutorials.