Developers

Email to Custom Object: Mapping Inbound Emails to Activity Timeline

By Sureka A R K

Welcome! In this article, we’ll dive into the process of setting up an Apex Email Service in Salesforce to efficiently map inbound emails to custom objects. We’ll cover everything you need to know – from configuring the email service to integrating it seamlessly with Outlook. You’ll discover how to ensure replies are accurately linked to your records using the Message-ID, enabling efficient communication tracking without relying on subject lines.

By following this article, you’ll unlock the full potential of streamlining email handling and improving overall workflow efficiency. Whether you’re managing customer support or sales inquiries, mastering the Apex Email Service ensures that your emails are properly mapped, tracked, and linked to the correct Salesforce records. If you’re ready to enhance how your Salesforce environment handles emails, you’re in the right place. Let’s dive in and simplify your email processes together!

1. Create a New Apex Class

Firstly, let’s create a handy Apex class called EmailtoCustomObject! This class is designed to make your Salesforce processes smoother by mapping inbound emails directly to your custom object records, without relying on tricky subject lines.

Plus, it handles attachments and even sends notifications to the record owner. Check out my step-by-step code on GitHub.

2. Steps to Generate Salesforce Email Address

This next process ensures your system is ready to handle inbound emails seamlessly, including attachments. 

  1. In the Salesforce Setup, search Email Services in the Quick Find box and select it. Click New Email Service to create a new service configuration.
  2. Provide a name for the email service and associate it with the relevant Apex class that will process the inbound emails. Set Accept Attachments to All for handling email attachments effectively.
  3. Leave the Accept Email From field empty to allow emails from any sender.
  4. Click Save to create the email service, making it active and ready to handle email processing.
  1. After saving, navigate to the Email Service Details page by clicking on the email service you created. Then, click New Email Address to add a new email address for the service.
  2. Enter a name for the email address and set the Email Address Local Part to define the unique part before the “@” symbol.
  3. Choose the Context User whose context will be used to create email messages, appearing in the Created By field of related records.
  4. Leave the Accept Email From field blank to accept emails from any domain, or specify domains (e.g., gmail.com) for restrictions. Click Save to finalize.
  1. After saving, Salesforce generates an email address by combining the Local Part you specified with a Salesforce-provided domain.

3. Set Up an Outlook Email Rule

Setting up an Outlook email rule is a quick way to ensure important emails are automatically redirected to your Salesforce-generated email address

  1. Use an existing Outlook email address or create a new one. Open the Outlook web or desktop app and log in.
  2. Click the Settings icon (gear) in the top-right corner, search for Rules in the settings search bar, and select it.
  3. Click Add New Rule and define the conditions to filter emails, such as sender addresses or specific keywords.
  4. For the action, choose Redirect to and enter the Salesforce-generated email address in the corresponding field.
  5. Click Save to activate the redirection rule and ensure filtered emails are forwarded to Salesforce.

4. Add the Outlook Email Address to Salesforce’s Org-Wide Email Addresses

In this step, you’ll add your Outlook email address to Salesforce as an Org-Wide Email Address. This allows you to use the address in the “From” field when sending emails from Salesforce.

  1. In Salesforce Setup, search for Org-Wide Email Addresses in the Quick Find box and click on it.
  2. Click the Add button, enter a Display Name, and provide your Outlook Email Address.
  3. Check the box for “Allow All Profiles to Use This From Address” or select specific profiles as needed.
  4. Click Save, and Salesforce will send a verification email to the Outlook address for confirmation.
  1. Open your Outlook account, find the email from Salesforce, and click the link in it to verify your email address.
  1. Once verified, your Outlook email address will show up as an option in the From field when sending emails from Salesforce.

5. Steps to Compose and Send an Email from a Custom Object Record

Next up, you’ll learn how to compose and send an email directly from a custom object record in Salesforce, which ensures your emails are logged and tracked efficiently

  1. Open the record of the custom object (App__c) you want to send the email from and find the Email Activity section.
  2. Click Compose or New Email to open the email editor and select your verified Outlook email address from the From dropdown.
  3. If the Org-Wide Email Address doesn’t show in Lightning Experience, switch to Salesforce Classic to verify. Once visible in Classic, it will appear in Lightning after some time.
  4. Enter the recipient’s email address in the To field, add the subject, and write the email body. Click Send to send the email.

6. Verify and Reply to Received Email

For step six, you’ll verify that emails are being delivered successfully, ensuring replies are processed correctly by Salesforce and confirming that replies are mapped back to the original record.

  1. Ensure the email is delivered to the recipient’s inbox, and any replies are sent to the Salesforce-generated email address.
  1. The reply triggers the email service in Salesforce, invoking an Apex class that maps the Message-ID from the response to the original outbound email.
  2. Salesforce automatically creates an EmailMessage record and links it to the original custom object.
  3. The reply is displayed in the Activity Timeline on the custom object record without needing the subject line for mapping.

7. Enable Debug Logs Before Replying to Salesforce’s Email

This last step ensures everything runs smoothly by enabling debug logs to monitor and troubleshoot the email service.

  1. Click the gear icon and select Setup, then search for Debug Logs in the Quick Find box.
  2. Click New to create a debug log and select the User whose context you want to log (e.g., the user triggering the email service or Apex class).
  3. Set the Debug Level to include Apex Code, choosing a detailed level like FINEST or DEBUG.
  4. Click Save, then perform the action (such as replying to the email) that triggers the Apex class.
  5. After the action, return to the Debug Logs page, locate the log entry, and check the details for successful execution or errors.

Constraints

Before wrapping up, it’s important to be aware of a few constraints that can impact the functionality of this email setup.

1. Dependency on Salesforce-Generated Address as the Sender

Inbound emails will map to the activity timeline only if the sender’s address matches the Salesforce-generated address. 

The setup depends on the Outlook forwarding rule, so any issues in Outlook forwarding could disrupt mapping.

2. Requirement of an Outbound Email for Record Mapping

An initial outbound email from Salesforce is required to establish a thread and store the Message-ID for reference in the Responses field. 

Mapping relies on the Message-ID rather than the subject line, allowing flexibility in the subject content for future replies.

Summary 

The Apex Email Service links emails to custom records using the Message-ID for accurate tracking. It works with a Salesforce-generated email and Outlook forwarding, making replies easy to map without relying on subject lines. With this setup, managing communication becomes simple, efficient, and perfectly organized.

I hope this post was insightful, and I look forward to sharing more useful information soon!

The Author

Sureka A R K

Sureka is a developer with over two years of experience in Salesforce, B2B Commerce Cloud, and Service Cloud. She specializes in custom Apex solutions, integration with AWS services, and enhancing contact center productivity.

Leave a Reply

Comments:

    Puneet
    July 09, 2025 11:50 am
    Thanks for sharing this helpful post! I really appreciate the effort you put into it. One small suggestion: I noticed that there are DML and SOQL operations inside a `for` loop. In Salesforce, this can lead to hitting governor limits, especially in bulk operations. To align with best practices, it's generally recommended to move those operations outside the loop and use collections (like lists or maps) to handle the data more efficiently. Just thought I'd mention it in case you're planning to update the post or optimize the code further. Thanks again for the great content—looking forward to more!
    Uomo Stronzo
    March 18, 2026 11:11 pm
    Please add me to your mailing list.
    Ross Collie
    March 19, 2026 9:30 am
    Hi! You can subscribe to our newsletter on our homepage, or by hitting the Newsletter button at the top of an article!