Skip to main content
32 votes

How to Test Messaging.sendEmail

You can use the Limits.getEmailInvocations method . Test.startTest(); methodThatSendsEmail(); Integer invocations = Limits.getEmailInvocations(); Test.stopTest(); system.assertEquals(1, ...
Adrian Larson's user avatar
  • 151k
14 votes
Accepted

How to Test Messaging.sendEmail

With respect to your edited question, as @AdrianLarson answered, your schedulable class still doesn't run until immediately after Test.stopTest(). Asserting the change in Limits.getEmailInvocations() ...
crmprogdev's user avatar
10 votes

How can I convert my htmlBody to plainTextBody using Apex/Regex

I think rather than using any RegEx, you can just use a built in String method: stripHtmlTags. Salesforce String Class So it would look like: String plainText = htmlBody.stripHtmlTags();
techbusinessman's user avatar
10 votes

Issue Setting ContentVersion Owner

Add this line: newContentVersion.firstPublishLocationId= user.Id; A more detailed explanation: When documents are created via apex (contentversion object), you need to specify the library otherwise ...
oozkan's user avatar
  • 581
9 votes

Salesforce Email Service Debugging

The likely answer comes from the comment by @MarcDBehr Email Services can have several Email Addresses. Each Email Address has a context user (i.e. running user) You need to enable debug logging for ...
cropredy's user avatar
  • 72.1k
8 votes
Accepted

How to leverage org-wide email address within an email service?

2 options: Option 1 1) Create a class which implements the inboundHandler. global class VerifyOrgWideEmailAddress implements Messaging.InboundEmailHandler { global Messaging....
Sam's user avatar
  • 4,383
7 votes

How to unzip a zipfile attachment in apex and process each file in the zip attachment

You can do this natively in Apex using the Zippex library which is open source. https://github.com/pdalcol/Zippex Your code would be similar to this: Blob zipBlob; //This needs to contain the zip ...
Pedro Dal Col's user avatar
7 votes
Accepted

custom Email Service - inline images appear as '[Image is no longer available]' upon Reply/ReplyAll

This is how I solved the problem, after saving off the attachments then I updated the message body to replace the cid references with URL to the files. For some magical reason, Salesforce will ...
Doug Ayers's user avatar
  • 4,196
7 votes
Accepted

Regex including curly brackets

Since backslashes are the escape character in Apex string literals, if you want a literal backlash, you need to escape it with a backslash. So the right regex to use would be \\{.*\\}
Derek F's user avatar
  • 62.7k
7 votes
Accepted

Create a Salesforce file from an email attachment

Salesforce files have superseded attachments. A single attachment can have only one parent and access to attachments is controlled by the parent record. If we want to attach same file on two different ...
Pranay Jaiswal's user avatar
6 votes

How to test if outbound email is send

In addition to the other answers, there is ApexMocks and the fflib Enterprise Pattern - specifically the Unit of Work Layer Here's an example: First, the class/method that sends the email: public ...
cropredy's user avatar
  • 72.1k
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
6 votes
Accepted

Regex implementation in Salesforce: getting from known string to end of the line

You need to turn on multi-line mode in order for the $ specifier to match at a line terminator (rather than at the end of the string). Multi-line mode is activated with the token (?m). As Derek F. ...
David Reed's user avatar
  • 93.2k
5 votes

Apex: HTML email status to get open count

The EmailStatus object is not queryable directly (please vote for this idea), but it can be queried indirectly from Leads and Contacts as a child relationship sub-query as noted by Jenny Bennett on ...
Doug Ayers's user avatar
  • 4,196
5 votes
Accepted

Can an Apex Email Handler parse an attached Excel file?

Short Answer: No its not posssible to read Excel in Apex. Long Answer: Excel files(xls,xlsx etc) are not a text based file. They are binary files. Thus unlike CSV(text based) they cant be read line ...
Pranay Jaiswal's user avatar
5 votes
Accepted

Inbound Email handler - Status being set as 'Sent' instead of 'New'

I think you can set the status value with the sequence number. 0 - New 1 - Read 2 - Replied 3 - Sent 4 - Forwarded 5 - Draft So it could be like this EmailMessage emailMessage = new EmailMessage();...
Naval Sharma's user avatar
  • 1,697
5 votes
Accepted

Apex SingleEmail using template and userId

This is expected behavior the User fields you are referencing are the sending user fields User fields are also known as Sending User fields for administrators and users with the “Manage Users” ...
thegogz's user avatar
  • 368
5 votes
Accepted

sending html email from APEX using templates

If you are using an email template, want to send an email using that template (and can afford for Salesforce to perform a query to load that template) and have mailmerge in the template, the easiest ...
Phil W's user avatar
  • 37k
5 votes

Sending email functionality is not working for site guest user of org

Make sure your Guest User has a valid email assigned. This is discussed in the article listed here.
Mohith Shrivastava's user avatar
4 votes

Email html body to plain text body

Use the String function stripHtmlTags() and it will do the work for you. String1.stripHtmlTags()
Ani's user avatar
  • 51
4 votes

Detect which email services address used by sender in Apex inbound email handler

One strategy is to use a different context user for each inbound email services address. Then, in the inbound email services handler, you can use UserInfo class to route logic accordingly if (...
cropredy's user avatar
  • 72.1k
4 votes

Parse a email body for Salesforce Id

I've actually written something like this before. Here's a pared down version of what you're looking for: public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, ...
sfdcfox's user avatar
  • 497k
4 votes
Accepted

How to insert FeedItem from Apex Email Service and set the Network ID?

Ok, figured it out thanks to Neil Hayek of Salesforce. Answer is super simple! Set the community id in the FeedItem.NetworkScope field. FeedItem fi = new FeedItem( parentId = 'chatter group id ...
Doug Ayers's user avatar
  • 4,196
4 votes
Accepted

Email Service not routing error email

It seems to be the ill-documented case that Enable Error Routing applies only, or at least primarily, to conditions listed under Failure Response Settings for which the Bounce Message option is ...
Thomas Taylor's user avatar
4 votes
Accepted

Is it possible to send email on behalf of another user?

I suspect it isn't currently possible. You could use setSenderDisplayName(String displayName) to change the from Name, but it won't change to reply email address. The alternative is to use an ...
Daniel Ballinger's user avatar
4 votes

Issue Setting ContentVersion Owner

I found from document : FirstPublishLocationId is id of the location where the version was first published. If the version is first published into a user's personal library or My Files, the field ...
Prasenjit Barua's user avatar
4 votes
Accepted

Send email with attachments

06A is the key prefix for ContentDocumentLink, which points to the bug here: system.debug(fileAttachments); mail.setEntityAttachments(attach); You are calling setEntityAttachments() ...
David Reed's user avatar
  • 93.2k
4 votes
Accepted

Insert Thread ID in the Email Subject option not visible on Email-to-Case

As far as I can see there is a new default for any org created after Winter'21 (which is the release being deployed by Salesforce). Long story short: I think you cannot change it, if your org is ...
Jeferson Chaves's user avatar
3 votes

Calling Email Service from Process builder

Assuming the DML event is on Case, then the Process Builder flows on Case will be evaluated using their triggering criteria to see which are true. For those Process Builder flows triggered true, if ...
cropredy's user avatar
  • 72.1k
3 votes

Can't activate Email Service

This error indicates that the packaged Apex Class has not yet been compiled. Typically, Apex Classes are compiled when saved. However, Apex Classes associated with Managed Packages cannot be saved so ...
Satheesh Kumar Natesan's user avatar

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