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

email to lead with Attachments
Hi,
I am trying to create an Email to lead.i mean when i use the email service link to send the mail to SF, a lead is generated.As this is working ,but when i am trying to attach a file with the Email.The file is not comin as attachments.My code is under.Can any one try to solve my problem.
Global class unsubscribe implements Messaging.inboundEmailHandler{ Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope env ) { // Create an inboundEmailResult object for returning //the result of the Apex Email Service Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); String strEmailId = email.fromAddress; String strSubject = email.subject; String myText=email.plainTextBody; String myFromName = email.fromName; Lead l; //Create a new test Lead and insert it in the Test Method integer iCount; iCount = [select count() from Lead where Email=:email.fromAddress]; if (iCount==0) { l = new Lead( lastName= myFromName, //strEmailId, Company=myFromName, Email=strEmailId, LeadSource='OnlineEnquiry', Description=strSubject+'\n'+myText, OwnerId='00590000000OJ0w'); //Change this id with user's id to whome you want to assign this lead. insert l; } result.success = true; return result; for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) { Attachment attachment = new Attachment(); attachment.Name = tAttachment.fileName; attachment.Body = Blob.valueOf(tAttachment.body); attachment.ParentId = l.Id; insert attachment; } for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) { Attachment attachment = new Attachment(); attachment.Name = bAttachment.fileName; attachment.Body = bAttachment.body; attachment.ParentId =l.Id; insert attachment; } result.success = true; return result; } /* Only for test case */ // Test method to ensure you have enough code coverage // Have created two methods, one that does the testing // with a valid "unsubcribe" in the subject line // and one the does not contain "unsubscribe" in the // subject line static testMethod void testUnsubscribe() { // Create a new email and envelope object Messaging.InboundEmail email = new Messaging.InboundEmail() ; Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); // Create a new test Lead and insert it in the Test Method Lead l = new lead(firstName='Rasmus', lastName='abc', Company='Salesforce', Email='rmencke@salesforce.com',LeadSource='OnlineEnquiry',Description='dsdfdgf' ); insert l; // Create a new test Contact and insert it in the Test Method Contact c = new Contact(firstName='Rasmus', lastName='Mencke', Email='rmencke@salesforce.com'); insert c; // test with subject that matches the unsubscribe statement email.subject = 'test unsubscribe test'; email.fromName = 'abhi k'; email.fromAddress = 'abhilash@rixyncs.co.in'; email.subject='test'; email.plainTextBody='zsdzdf'; // call the class and test it with the data in the testMethod unsubscribe unsubscribeObj = new unsubscribe(); unsubscribeObj.handleInboundEmail(email, env ); } /* static testMethod void testTrigger() { Lead l = new Lead(lastName='testTrigger',Status='Qualified',Email='abhi@rixyncs.co.in', Company='Rixyncs',MobilePhone='919901379361'); insert l; // SMS__c s = new SMS__c(To_Lead__c=l.Id,Message__c='Test'); // insert s; Database.LeadConvert lc = new database.LeadConvert(); lc.setLeadId(l.Id); lc.setConvertedStatus('Qualified'); Database.LeadConvertResult lcr = Database.convertLead(lc); }*/ }
Please help me.
Thanks
Nasir
Hi Nasir,
i think u r tring , if inbound email contians any attchment then it will attches to newly created Lead... so please
check with this code
if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
Attachment attachment = new Attachment();
// attach to the newly created Lead record
attachment.ParentId =l.id;
attachment.Name = email.binaryAttachments[i].filename;
attachment.Body = email.binaryAttachments[i].body;
insert attachment;
}
}
Hi
I tried to edit my code with yours with the binary attachments code.But it did't worked.I had send the mail with an attachment but attchements did't appear.Here is the modified code.
Well one more thing binary attachment is to attach image,vedio and textAttachments is for .vcf and .vcs extension.
and in my mail i am sending a .doc file.
here is the mdified code
I am having a similar problem, I am trying to attach two incoming attachments to a Lead record, one textAttachment (vcf file) and one binaryAttachment (jpeg image).
The text one works fine, but it seems the email.binaryAttachments list is always empty. Has anyone got a solution for this?