Skip to content

Instantly share code, notes, and snippets.

@cdcarter
Created May 28, 2016 03:26
Show Gist options
  • Save cdcarter/5a8fcf72e9d0be7c88942a365f5f6936 to your computer and use it in GitHub Desktop.
Save cdcarter/5a8fcf72e9d0be7c88942a365f5f6936 to your computer and use it in GitHub Desktop.
global class Summer16EmailAction {
@invocableMethod
global static void renderEmailToChatter(List<EmailRequest> emails) {
List<ConnectApi.BatchInput> batchInputs = new List<ConnectApi.BatchInput>();
for(EmailRequest email : emails) {
ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.subjectId = email.getFeedId();
ConnectApi.MessageBodyInput body = new ConnectApi.MessageBodyInput();
body.messageSegments = new List<ConnectApi.MessageSegmentInput>();
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = email.getPlainTextBody();
body.messageSegments.add(textSegment);
input.body = body;
ConnectApi.BatchInput batchInput = new ConnectApi.BatchInput(input);
batchInputs.add(batchInput);
}
ConnectApi.ChatterFeeds.postFeedElementBatch(Network.getNetworkId(), batchInputs);
}
global class EmailRequest {
@invocableVariable(required=true) global Id whoId;
@invocableVariable(required=true) global Id whatId;
@invocableVariable(required=true) global Id templateId;
@invocableVariable(required=true) global Id feedId;
private Messaging.SingleEmailMessage msg;
global Id getFeedId() {return this.feedId;}
global String getPlainTextBody() { return this.render().getPlainTextBody();}
private Messaging.SingleEmailMessage render() { return this.msg = Messaging.renderStoredEmailTemplate(templateId, whoId, whatId);}
}
}
@JodieM
Copy link

JodieM commented Jun 17, 2016

Hi Christian, I am just looking at this now and trying to get it working.

It works fabulously well sending a HTML email from a Contact. I'm now trying to get it working with a VF Email Template on a Custom Object. It's just not doing the post to Chatter.

My coding skills are very limited so I am not sure how I debug the code to see where the problem lies. (The other actions on the process are working fine - eg it is actually sending the email).

For my inputs I have feedID = the Record ID of the custom object record I'm starting with. templateID is just static like your example - the ID of my VF Email Template. WhoID is the Contact ID (it is a lookup on the custom object). WhatID is the record ID of the custom object record I'm starting with - eg the record that renders the email template.

Is it something to do with email.getPlainTextBody()? Can a VF email be converted to Plain Text after it is rendered?

I suppose I should start with adding some debug statements and see where it is getting to.
And then adding another text segment with some static text to see if that posts.

Any other ideas?

Thank you for doing this, it is really going to help, once I can get it working.

@JodieM
Copy link

JodieM commented Jun 23, 2016

OK, I put some debug statements in there, and this is all it's showing

[26]|DEBUG|ConnectApi.MessageBodyInput[messageSegments=(ConnectApi.TextSegmentInput[text=])]

I added a debug of

System.debug(email.getPlainTextBody());

and it just returned nothing.

The email is actually sending, and the rest of the process is working.

So I'm not sure where to turn next with it. I may have to just give up on this idea for now. So close, yet so far, because it works perfectly well for HTML emails.

@cdcarter
Copy link
Author

@jodim -

Id contactId = '0033600000J3DZ7';
Id accountId = '0013600000QG6qS';
Id templateId = '00X36000001996H'
Messaging.SingleEmailMessage msg = Messaging.renderStoredEmailTemplate(templateId, contactId, accountId);
system.debug(msg.getPlainTextBody());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment