You need to sign in to do that
Don't have an account?

How to create and test APEX code for converting Emails to Leads
Hi Guys
Im trying to do apex code for converting emails to leads. I have configured my classes as follows but I cant seem to test them and in turn the whole thing does not work. Could anyone assist...im new to Apex Development:
Im trying to do apex code for converting emails to leads. I have configured my classes as follows but I cant seem to test them and in turn the whole thing does not work. Could anyone assist...im new to Apex Development:
/** * Email services are automated processes that use Apex classes * to process the contents, headers, and attachments of inbound * email. */ global class EmailReceive implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { Lead lead; Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); try { //Look for lead whos name is the email and create it if necessary if ([select count() from Lead where Name = :email.FromAddress] == 0) { lead = new Lead(); lead.LastName = email.FromAddress; lead.Email = email.FromAddress; insert lead; } else { //Lead already exists lead = [select Id from Lead where Name = :email.FromAddress]; } result.success = true; } catch (Exception e) { result.success = false; result.message = 'Error processing email...'; } return result; } }Below is my testing class
@isTest private class EmailReceiveTest { static testMethod void myUnitTest() { // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); //Create the email body email.plainTextBody = 'This should become a note'; email.fromAddress = 'test@test.com'; String contactEmail = 'jsmith@salesforce.com'; email.subject = 'Dummy Account Name 123'; EmailReceive er = new EmailReceive(); Test.startTest(); Messaging.InboundEmailResult result = er.handleInboundEmail(email, env); Test.stopTest(); System.assert (result.success, 'InboundEmailResult returned a failure message'); Lead [] leadDb = [SELECT Id FROM Lead where LastName=:email.FromAddress]; System.assertEquals (1, leadDb.size(),'Lead was not inserted'); } }
following are the changes I made -
1) Your query in line 13 was returning NULL.
2) Your Lead creation at lines 14-17 did not contain mandatory field 'COMPANY'.
3) in line 13 you are 'Name' with email.FromAddress and in line 15 , you were editing LastName to contain email.FromAddress.
Please refer the code below -
Create an Email Service to point to the above code. Create a new mail address and send a mail to that newly created mail address to see your code working. And see your lead is created!
I have:
The lead.Company and lead.LastName don't seem to work and I can't figure out how to get what I want there. Ideally I'd be grabbing domain less the TLD to use as the company name.
In an incredibly ideal world, I'd also be setting the lead.Lead_Source__c as an IF statement based on the to: email address for the incoming email. We'll have sales@ and partners@ and while I could set up a separate service for each, it'd be nice to have a single service that handles both.
Any assistance would be greatly appreciated. I had the service working with the example provided, just trying to make it a bit more directly useful for the team.