
This code sample shows how to leverage the Force.com Email Services to create a new idea for Salesforce Ideas from inbound emails. It should serve as a nice example of using the Email Services, and also how to interact with Ideas!
For this sample to work, make sure you have enabled Ideas for your org. To enable Ideas go to:
Setup | App Setup | Customize | Ideas | Settings and enable Ideas
Here's the sample code for our inbound message handler. We've split the class over a few methods, starting with the method that reacts to the inbound emails:
Global class IdeasSample implements Messaging.inboundEmailHandler{
Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env ) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
// Create the new Idea from the incoming email
createIdea(email.subject, email.plainTextBody, env.fromAddress);
// Set the result to true, no need to send an email back to the user
// with an error message
result.success = true;
// Return the result for the Apex Email Service
return result;
}
We have a helper sendEmail() method sends an email to the person who created the idea:
public static void sendEmail(String s, List<Idea> myIdea) {
// Send an email back to the from email address of who created the idea
String[] toAddresses = new String[] {s};
// new instance of a single email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setReplyTo('ideas@acme.com');
mail.setSenderDisplayName('Salesforce Ideas!');
mail.setSubject('New Idea['+myIdea[0].Id+'] created - '+ myIdea[0].Title);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Your Idea: ' + myIdea[0].Title +' has been created \n Check out your new idea \n\n To watch you idea in salesforce.com click on this link \n https://na1-blitz04.soma.salesforce.com/ideas/viewIdea.apexp?id='+myIdea[0].Id);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
The following method does the hard work of creating a new idea. As you can see, it's pretty much simply creating a new Idea object and adding a body and title:
public static Idea[] createIdea(String IdeaTitle, String ideaBody, String fromEmail) {
// instance of a new Idea
Idea[] newIdea = new Idea[0];
// create a new Idea
newIdea.add(new Idea(Body = ideaBody,
Title = IdeaTitle));
insert newIdea;
sendEmail(fromEmail,newIdea);
return newIdea;
}
Finally, here's a test method!
static testMethod void myTestMethod() {
// Create a new email, envelope object and Attachment
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
email.subject = 'Improve performance on the new Formular 1 racecar';
env.fromAddress = 'user@acme.com';
// call the class and test it with the data in the testMethod
IdeasSample ideaTestObj = new IdeasSample();
ideaTestObj.handleInboundEmail(email, env );
}
}