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

Apex email services-email to apex.



I have an situation on my hand.I work on a student enrollment application.


1)So, the task is a college has an request information form , so, when a student enters the data in it then it should create a  contact for that student if the record is not present and create a task...


2) I have an junior day information form , where the student enters the data then a corresponding contact is created and then an event is generated for this contact....


Any solution , idea and hints are appreciated....Please ...Please....This has to be done using email to apex .....???


How can I do that ...Please help ....



thank you








I am trying to create an contact and create a task along with that contact , but I am receiving an error as


Error: Compile Error: unexpected token: try at line 72 column 0


Below is the code

Help me Please !!!!


* Email services are automated processes that use Apex classes
* to process the contents, headers, and attachments of inbound
* emails.

global class processApplicant implements
Messaging.InboundEmailHandler {

Contact[] myContact = new Contact[0];
Task[] newTask = new Task[0];

global Messaging.InboundEmailResult handleInboundEmail(
Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result =
new Messaging.InboundEmailresult();

// Captures the sender's email address
String emailAddress = envelope.fromAddress;

// Retrieves the sender's first and last names
String firstname = email.fromname.substring(0,email.fromname.indexOf(' '));
String lastname = email.fromname.substring(email.fromname.indexOf(' '));

// Retrieves content from the email.
// Splits each line by the terminating newline character
// and looks for the position of the phone number and city

String[] emailBody = email.plainTextBody.split('\n', 0);
String PhoneNumber = emailBody[0].substring(6);
String address=emailBody[1].substring(5);
String city = emailBody[2].substring(4);
String state=emailBody[3].substring(3);
String zip=emailBody[4].substring(2);
String area_of_interest=emailBody[5].substring(1);
String HighSchoolGraduationDate=emailBody[6].substring(0);
// Creates a new contact from the information
// retrieved from the inbound email
newContact.add(new Contact(email = E-mailAddress,
FirstName = firstname,
LastName = lastname,
Phone = PhoneNumber,
Mailingcity = city,
MailingState= state,
MailingZip= zip,
EnrollmentrxRx__Program_of_Interest__c= area_of_interest,
High_School_Grad_Year__c=HighSchoolGraduationDate ));

upsert newContact;

catch (System.DmlException e)
System.debug('ERROR: Not able to create candidate: ' + e);

// Sends the email response
myContact = createContact.newContact
(FirstName, LastName, email, PhoneNumber);

    // Try to lookup any contacts based on the email from address
    // If there is more than 1 contact with the same email address
    // an exception will be thrown and the catch statement will be called
try {
       Contact vCon = [Select Id, FirstName,LastName, email
       From Contact  
       Where email = :email.fromAddress and email.fromAddress=E-mailAddress
       Limit 1];

// Add a new Task to the contact record we just found above
 newTask.add(new Task(Description = requestinformation,
     Priority = 'Normal',
     Status = 'Inbound Email',
     Subject = email.subject,
     IsReminderSet = true,
     ReminderDateTime =,
     WhoId = vCon.Id));

// Insert the new Task and it will be created and appended to the contact record
     insert newTask;
System.debug('New Task Object: ' + newTask );
   // If there is an exception with the query looking up
   // the contact this QueryException will be called.
   // and the exception will be written to the Apex Debug logs

   catch (System.QueryException e) {
   System.debug('Query Issue: ' + e);
// Set the result to true, no need to send an email back to the user
// with an error message

  result.success = true;

  // Return the result for the Email Service
  return result;