function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AkashAkash 

Attachments in inboundemail

Hi,

I am trying to attach an attachment in email for test method but I am not able to attach it. My code for email method is:

global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env)
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();      
        string FileNames='';
        Integer RecCounter = 0 ;
        Integer ColCounter = 0 ;
                     
         List<Messaging.InboundEmail.TextAttachment> Obj =  new List<Messaging.InboundEmail.TextAttachment>();
         string UniquePhoneNumbers = '';
               
                                    
        if (email.textAttachments != null)
        {                                  
                //Attahcment found...
                Obj  = email.textAttachments;          
                Integer i;
}
}

and test method code is:

public static testMethod void testTasks() {

// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();


List<Messaging.InboundEmail.TextAttachment> objList =  new List<Messaging.InboundEmail.TextAttachment>();
Messaging.InboundEmail.TextAttachment obj = new Messaging.InboundEmail.TextAttachment();

obj.body = 'test';
obj.fileName = 'C:\test.vcf';
obj.mimeTypeSubType = 'application/octet-stream';

objList.add(obj);

email.textAttachments = objList;
}

The test method is not taking the attachment.

Can you please help me in attaching an attachment in my test method? Please treat this as very urgent.

Thanks
AkashAkash
Hi !!
 
Please reply back as this is very urgent.
 
Thanks
Ron HessRon Hess
i'm not sure i understand, you are asking the test method to read from your hard disk?

I don't think that is going to work. 

what is the error you see?
What do you expect to happen ?

attachments normally come from within the platform
AkashAkash
Hi Ron,
 
We have written a method which reads email attachments and performs some validations on it.
 
The method's signature is as follows:
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope env)
{
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();      
        string FileNames='';
        Integer RecCounter = 0 ;
        Integer ColCounter = 0 ;
    try
    {                          
         List<Messaging.InboundEmail.TextAttachment> Obj =  new List<Messaging.InboundEmail.TextAttachment>();
         string UniquePhoneNumbers = '';
               
                                    
        if (email.textAttachments != null)
        {                                  
                //Attahcment found...
                Obj  = email.textAttachments;          
                Integer i;
                for(i = 0 ;i < Obj.size();i++)
                {                  
                  if (FileNames=='')
                    FileNames=Obj[i].fileName;
                  else
                    FileNames=FileNames +','+ Obj[i].fileName;
                }
 }
     }
..........
 }
 
Here the test is not covered after the line 'if (email.textAttachments != null)'. Most of our code and validations are written after the 'if' loop and while runing test it is not covered. Hence we know that its not reading the attachment when we pass it from test method.       
 
When we write test method for it, and pass text attachment to it and run our test the method's lines where we perform validation on text attachments are not
covered.
 
The test method is as follows:
public static testMethod void testTasks() {
// Create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail();
Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

List<Messaging.InboundEmail.TextAttachment> objList =  new List<Messaging.InboundEmail.TextAttachment>();
Messaging.InboundEmail.TextAttachment obj = new Messaging.InboundEmail.TextAttachment();
obj.body = 'test';
obj.fileName = 'test.txt';
obj.mimeTypeSubType = 'text';
obj.charset = 'utf-8';
objList.add(obj);
email.textAttachments = objList;
EmailExample taskObj = new EmailExample();
taskObj.handleInboundEmail(email, env);
}
 
We want to know if the way we attach text attachments to email is correct or do we need to write some other lines of code?
 
Thanks
Ron HessRon Hess
This looks correct to my eye, so even after you construct a text attachment, in your test method, it is not there when the test is run?

the other method is to perform all your logic to deal with a text attachment in a separate method that you can then call directly from your testMethod


something like this :

Messaging.InboundEmailResult handleInboundEmail() {
....
if (email.textAttachments != null) { processTextAttachment( email.textAttachments) }
...
}


then move your untested code to the new function processTextAttachment()

then in your test method, call this new function directly

testMethod t1() {
...
obj.body = 'test';
obj.fileName = 'test.txt';
obj.mimeTypeSubType = 'text';
obj.charset = 'utf-8';

 processTextAttachment( obj)

}


now you can easily test code that otherwise would not get covered.

i'm really stumped as to why you don't see textAttachments as not null, but refactoring to add testability is perfectly valid solution.




Rasmus MenckeRasmus Mencke
Take a look at this post I made a while ago, explains how you can get coverage for your attachments in your testMethods

http://wiki.apexdevnet.com/index.php/Code_Sample_-_Testing_Email_Services_with_Inbound_Attachments