You need to sign in to do that
Don't have an account?
Timo Mildenberger 17
Extend my Testclass for Email Service Handler
Hi all,
i wrote an email handler to create emailMessage with attachements and Lead with files for a recruiting process to work as Email2Lead.
I use Lead Object for Candidates-
Here is my email handler class:
I'm no developing expert and my testclass has only 43% coverage. How can i extend testing to cover ContentVrsion, Attachements and the created lead record?
Here is my testclass:
Many thanks in advance!
Timo
i wrote an email handler to create emailMessage with attachements and Lead with files for a recruiting process to work as Email2Lead.
I use Lead Object for Candidates-
Here is my email handler class:
/** * Email services are automated processes that use Apex classes * to process the contents, headers, and attachments of inbound * email. */ global class CreateLeadFrmEmail implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); String EmailFromName= email.fromName; // Create Lead Lead c = new Lead(); c.Email = email.fromAddress; c.FirstName = EmailFromName.substring(0,EmailFromName.indexOf(' ')); c.LastName = EmailFromName.substring(EmailFromName.indexOf(' ')+1); c.Company = ''; insert c; Id LeadOwner = [SELECT OwnerId FROM Lead WHERE Id =:c.Id].OwnerId; // Create Email-Message EmailMessage e = new EmailMessage(); e.FromAddress = email.fromAddress; e.Status = '0'; e.FromName = email.fromName; e.Incoming = true; e.Subject = email.Subject; e.htmlBody = email.htmlBody; insert e; // Save attachments, if any List<Attachment> attachments = new List<Attachment>(); if(email.textAttachments != null) { for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) { Attachment attachment = new Attachment(); attachment.Name = tAttachment.fileName; attachment.Body = Blob.valueOf(tAttachment.body); attachment.ParentId = e.Id; attachments.add(attachment); ContentVersion cVersion = new contentVersion(); cVersion.ContentLocation = 'S'; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork. cVersion.PathOnClient = tAttachment.fileName; //File name with extention cVersion.Origin = 'H'; //C-Content Origin. H-Chatter Origin. cVersion.OwnerId = LeadOwner; //Owner of the file cVersion.Title = tAttachment.fileName; //Name of the file cVersion.VersionData = Blob.valueOf(tAttachment.body);//File content cVersion.SharingOption = 'R'; insert cVersion; //After saved the Content Version, get the ContentDocumentId Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId; //Insert ContentDocumentLink ContentDocumentLink cDocLink = new ContentDocumentLink(); cDocLink.ContentDocumentId = conDocument; //Add ContentDocumentId cDocLink.LinkedEntityId = c.Id; //Add attachment parentId cDocLink.ShareType = 'I'; //V - Viewer permission. C - Collaborator permission. I - Inferred permission. //cDocLink.Visibility = 'SharedUsers';//AllUsers, InternalUsers, SharedUsers Insert cDocLink; } } if(email.binaryAttachments != null) { for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) { Attachment attachment = new Attachment(); attachment.Name = bAttachment.fileName; attachment.Body = bAttachment.body; attachment.ParentId = e.Id; attachments.add(attachment); ContentVersion cVersion = new contentVersion(); cVersion.ContentLocation = 'S'; //S-Document is in Salesforce. E-Document is outside of Salesforce. L-Document is on a Social Netork. cVersion.PathOnClient = bAttachment.fileName; //File name with extention cVersion.Origin = 'H'; //C-Content Origin. H-Chatter Origin. cVersion.OwnerId = LeadOwner; //Owner of the file cVersion.Title = bAttachment.fileName; //Name of the file cVersion.VersionData = bAttachment.body;//File content cVersion.SharingOption = 'R'; insert cVersion; //After saved the Content Version, get the ContentDocumentId Id conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id].ContentDocumentId; //Insert ContentDocumentLink ContentDocumentLink cDocLink = new ContentDocumentLink(); cDocLink.ContentDocumentId = conDocument; //Add ContentDocumentId cDocLink.LinkedEntityId = c.Id; //Add attachment parentId cDocLink.ShareType = 'I'; //V - Viewer permission. C - Collaborator permission. I - Inferred permission. //cDocLink.Visibility = 'InternalUsers';//AllUsers, InternalUsers, SharedUsers Insert cDocLink; } } // insert attachments if(attachments.size() > 0) { insert attachments; } // Add Email Message Relation for id of the sender EmailMessageRelation emr = new EmailMessageRelation(); emr.emailMessageId = e.Id; emr.relationId = c.Id; // Lead id of the sender emr.relationType = 'FromAddress'; insert emr; result.success = true; return result; } }
I'm no developing expert and my testclass has only 43% coverage. How can i extend testing to cover ContentVrsion, Attachements and the created lead record?
Here is my testclass:
@isTest public class CreateLeadFrmEmailTest { //Test Method for main class static testMethod void CreateLeadFrmEmail() { // create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); // setup the data for the email email.subject = 'Test Email'; email.fromAddress = 'someaddress@email.com'; email.plainTextBody = 'email body\n2225256325\nTitle'; email.fromName = 'Test Candidate'; // add an Binary attachment Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment(); attachment.body = blob.valueOf('my attachment text'); attachment.fileName = 'textfileone.txt'; attachment.mimeTypeSubType = 'text/plain'; email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment }; // add an Text attachment Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment(); attachmenttext.body = 'my attachment text'; attachmenttext.fileName = 'textfiletwo3.txt'; attachmenttext.mimeTypeSubType = 'texttwo/plain'; email.textAttachments = new Messaging.inboundEmail.TextAttachment[] { attachmenttext }; // call the email service class and test it with the data in the testMethod CreateLeadFrmEmail testInbound=new CreateLeadFrmEmail (); testInbound.handleInboundEmail(email, env); } }
Many thanks in advance!
Timo
I hope you are doing well .....!!
Please set -------> c.Company = 'test'; In CreateLeadFrmEmail class line number 21
Hope this helps you.
If this helps kindly mark it as solved so that it may help others in the future.
Thanks & Regards,
Foram Rana