Skip to main content
11 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 ...
Derek F's user avatar
  • 62.5k
10 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 ...
Renato Oliveira's user avatar
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 ...
Derek F's user avatar
  • 62.5k
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 ...
Rishi Golyan's user avatar
9 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 ...
Mr.Frodo's user avatar
  • 5,802
8 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 ...
crmprogdev's user avatar
8 votes

How to create an EmailMessage record with DML having HasAttachment to be 'true'?

I spent close to 4 hours on this and figured it out. The key thing is to populate Attachment.ParentId with EmailMessage Id. Here is the sample code: EmailMessage emailMessage = new EmailMessage(); ...
javanoob's user avatar
  • 8,907
7 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 ...
Renato Oliveira's user avatar
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 ...
Pranay Jaiswal's user avatar
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 ...
Jason Clark's user avatar
  • 11.9k
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 ...
Adrian Larson's user avatar
  • 150k
5 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 ...
Thomas Taylor's user avatar
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
Bryan Anderson's user avatar
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 ...
sfdcfox's user avatar
  • 495k
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 ...
Santanu Boral's user avatar
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 ...
Eric's user avatar
  • 54.2k
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(...
sfdcfox's user avatar
  • 495k
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 ...
sfdcfox's user avatar
  • 495k
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 ...
sfdcfox's user avatar
  • 495k
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&...
abd3721's user avatar
  • 2,162
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 ...
Brian Mansfield's user avatar
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 ...
Renato Oliveira's user avatar
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 ...
Fernando Gavinho's user avatar
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.
smohyee's user avatar
  • 3,846
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 ...
cropredy's user avatar
  • 72k
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 ...
Luke Buthman's user avatar
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 ...
EC Justin's user avatar
2 votes

How get Lead Id in apex trigger

For objects other than Lead and Contact The before trigger on EmailMessage should include a value for RelatedToId which is the parent of the email message (e.g., the Opportunity.Id). Your trigger will ...
cropredy's user avatar
  • 72k
2 votes
Accepted

Receiving ILLEGAL_OPERATION error on EmailMessage Update from ContentDocumentLink trigger

EmailMessage can be updated only when it's in the Draft status. update() is supported when an email record is in Draft status, and IsPrivateDraft is false. It is also supported if Status and ...
Paweł Borys's user avatar
2 votes
Accepted

EmailMessage not showing on lead's Activity Timeline

You also need to update the status field on the EmailMessage object. Set it to 3 to indicate the message was sent. EmailMessage m = new EmailMessage( Subject = 'email.subject', TextBody = 'email....
Rajiv's user avatar
  • 364

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