9 votes
Accepted

SaveAsActivity is not allowed with whatId that is not supported as a task whatId

The error message can be a little confusing, but this error can be thrown if your main object (relatedTo in your template) doesn't have the "Allow Activities" option checked. Checking this option ...
9 votes

System.LimitException: Too many SOQL queries: 101

Rishi Golyan correctly identified the issue, however, Messaging.renderStoredEmailTemplate() works on single ids rather than a list of ids. So, simply moving that method call to be outside of your ...
  • 54.9k
9 votes
Accepted

System.LimitException: Too many SOQL queries: 101

Hi You are getting the error because you are using Messaging.renderStoredEmailTemplate() in for loop as this is counted towards the SOQL governor limit as one query. To avoid the exception you have to ...
8 votes
Accepted

Salesforce Storage Issue - Hitting Data Storage Limits with Email Messages

My org has run into similar issues with the amount of data storage consumed by EmailMessage. Unfortunately, EmailMessage is a difficult object to work with. From my own testing: There are two fields ...
  • 54.9k
7 votes
Accepted

Different between RelatedToId vs ParentId In EmailMessage

EmailMessage is only available for organizations that use Email-to-Case or Enhanced Email, which is automatically enabled for most customers. If you want to be able to relate Email Message records ...
  • 5,744
6 votes

Different between RelatedToId vs ParentId In EmailMessage

Originally, EmailMessage was only available for use with the Case object. The ParentId represents the Case the email is associated with. Now that email messages are available for use with other ...
  • 40.7k
6 votes
Accepted

How to use an Apex Email Service to store emails on a record EXACTLY as Salesforce does?

From what I understand, You don't need to create a task to insert an EmailMessage. Email message has relatedToId field which you can use to directly link it to any desired record. As relatedToId is ...
6 votes

How to get the Id of an EmailMessage after sending a SingleEmailMessage with Messaging.SendEmail()?

You can't get the Id on the same transaction because the email messages are sent after the transaction succeeds. See items 19 and 20 on the Triggers and Order of Execution: Commits all DML ...
5 votes
Accepted

How to pull hrefs from htmlbody

Your expression is almost correct. I would use <a.+<\/a>. You don't know if href will be the first property (or if there will be multiple spaces after a), and you don't want to skip links ...
  • 147k
5 votes
Accepted

What happens when I delete the Task related to an EmailMessage?

Note: Some of the following is conclusions my team reached for which we could not find explicit documentation; Salesforce support eventually confirmed our findings, just this past week. The ...
  • 11.4k
5 votes
Accepted

RelatedToId field on EmailMessage Not Writeable

According to the documentation for the EmailMessage Object, this field can only be set on insert not update
4 votes
Accepted

Inserted EmailMessage always "Outbound"?

You need to set the Incomng field to true. See the documentation: Field Incoming Type boolean Properties Create, Defaulted on create, Filter Description Indicates whether ...
  • 462k
4 votes
Accepted

Trigger on EmailMessage to capture when an email is opened

There is not currently a way to capture this. The updating of EmailMessage.Status from 0 (New) to 1 (Read) that occurs when a user opens the message does not fire triggers. You can vote to change this ...
3 votes
Accepted

System.NullPointerException: Attempt to de-reference a null object: ()

Line 12 seems to be List attachmentsToInsert = new List(); witch cannot throw that error. Since the error message says the trace is from a trigger and I don't see anything in your code to execute the ...
  • 53.6k
3 votes

Different between RelatedToId vs ParentId In EmailMessage

If we activate Enhanced Email then only Related To field is available, otherwise not. Parent Id is always be available in EmailMessage object. Enhanced Email elevates email to a standard ...
3 votes

How to pull hrefs from htmlbody

You might want to use the XmlStreamReader class instead. Basically, you just loop through the stream, watching for href attribute. Something like this: XmlStreamReader reader = new XmlStreamReader(...
  • 462k
3 votes
Accepted

Dynamically create html table rows based on List of records

I had the same problem before and came up with a solution. Basically, you can append the table to your setHtmlBody text value by passing in a list of records (be sure to change to appropriate ...
  • 1,560
3 votes
Accepted

Cloning attachment from Email Object to Case Object

The following answer is based on crop1645's comment to the question: trigger CaseAttachment on Attachment (after insert) { Set<Id> Parents = new Set<Id>(); List<Attachment> ...
  • 9,281
3 votes
Accepted

Parsing / Displaying TextBody from EmailMessage

It's not the line breaks that are the problem, it's how HTML treats line breaks by default (white-space: normal), which collapses multiple white space characters (space, tab, new line, and carriage ...
  • 462k
3 votes
Accepted

Impossible to test Task created from Messaging.sendEmail?

There are some things that are impossible to test in Salesforce. Things like field history and email tasks are generated during the "transaction finalization" step of a transaction, which ...
  • 462k
2 votes

Set default "From" address for all outgoing emails from Salesforce

You need to set OW email address here. Administer >Email Administration > Organization-Wide Email Addresses. For the WF you need to set default WF email address under Workflow>Setting. Hope this ...
2 votes
Accepted

Test class for trigger on Case and case comment objects

You can use the below test class : To insert opportunityLineItem, we have to insert opportunity, product etc. Take help from the below code. Let me know if you have any issues @isTest private class ...
  • 2,225
2 votes
Accepted

Error: invalid parameter value on EmailMessage-Email Message Layout - Email-to-Case.layout

I've figured out that this deployment error comes from including this related list metadata in the layouts/EmailMessage-Email Message Layout.layout file: <relatedLists> <fields&...
  • 2,112
2 votes

Error: invalid parameter value on EmailMessage-Email Message Layout - Email-to-Case.layout

Running a deploy from the same repository to a different target org did not generate the error; for now, I'm chalking this up as a platform quirk specific to the one org. EDIT: We refreshed the org ...
2 votes

EmailMessage query returns 'No such column'

You need to enable the Enhanced Email feature in order to use that field in apex. Also, if you want to relate the email message with a User, Lead or Contact, you need to use the EmailMessageRelation ...
2 votes
Accepted

How to mass delete EmailMessage?

Due to an unidentified issue in my organization, some EmailMessage records were created without some references, and even I, as administrator, couldn't delete or read the records by the UI. I could ...
2 votes
Accepted

How can I know if a case has unread emails?

Best solution I've seen so far is a scheduled apex class that just goes through all cases and updates status rather than relying on a trigger.
  • 3,622
2 votes

Can I edit the TextBody of EmailMessage object when registering a case from Email-to-Case?

The EmailMessage object permits the TextBody field to be updated if the status = Draft and other conditions as shown below update() is supported when an email record is in Draft status, and ...
  • 66.9k
2 votes
Accepted

EmailMessage.RelatedToId not valid for Lead in Process Builder

I updated the object to Allow Activities and it worked. This happened because on EmailMessage's RelatedTo field, the object in question was not listed. By Allowing Activities, that object gets placed ...
2 votes

Case Status Changing from Email Message Workflow not working

Figured out the issue. I didn't test by sending and replying to the Proxy email designated in the email-to-case setting. It also requires the setting of including the thread ID in the subject or body ...

Only top scored, non community-wiki answers of a minimum length are eligible