+ Start a Discussion
RiojinRiojin 

Hi i need code coverage for this class, anybody can help me?

global class EmailTemplate implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

Products er = new Products__c();

String[] values = email.plainTextBody.split(',');
er.Contact__c = values[0];

String s = values[1];
Integer i = (Integer.valueof(s.trim()));
er.Amount__c = i;

String dt = values[2];
String[] stringDate = dt.split('/');
Integer m = Integer.valueOf(stringDate[0]);
Integer d = Integer.valueOf(stringDate[1]);
Integer y = Integer.valueOf(stringDate[2]);
Date product = date.newInstance(y,m,d);
er.Date__c = product;

er.Type__c = values[3];

er.Description__c = values[4];
er.Comments__c = values[5];

insert er;

return result;

 

Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
RiojinRiojin

I found a solution!

 

Code Coverage 100%

 

 

static testMethod void testMe() {
// 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
Products__c er = new Products__c();
email.plainTextBody = '003E000000Fz7taIAB,888,7/19/2012,Local Transportation,No Descriotion,No comments';
String[] values = email.plainTextBody.split(',');
er.Contact__c = values[0];

String s = values[1];
Integer i = (Integer.valueof(s.trim()));
er.Amount__c = i;

String dt = values[2];
String[] stringDate = dt.split('/');
Integer m = Integer.valueOf(stringDate[0]);
Integer d = Integer.valueOf(stringDate[1]);
Integer y = Integer.valueOf(stringDate[2]);
Date reimb = date.newInstance(y,m,d);
er.Date__c = reimb;

er.Type__c = values[3];

er.Description__c = values[4];
er.Comments__c = values[5];

// call the email service class and test it with the data in the testMethod
EmailTemplate emailProcess = new EmailTemplate();
emailProcess.handleInboundEmail(email, env);

// query for the contact the email service created
Products__c p1 = [select Contact__c, Amount__c, Date__c, Type__c,Description__c,Comments__c
from Products__c where Id = :er.id ];

System.assertEquals(p1.Contact__c,values[0]);
System.assertEquals(p1.Amount__c,i);
System.assertEquals(p1.Date__c,reimb);

All Answers

sfdc_olegsfdc_oleg

This article has a test method for inboundemailhandler interface

 

http://na1.salesforce.com/help/doc/en/code_inbound_email.htm

 

 

Hope this helps.

RiojinRiojin

I found a solution!

 

Code Coverage 100%

 

 

static testMethod void testMe() {
// 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
Products__c er = new Products__c();
email.plainTextBody = '003E000000Fz7taIAB,888,7/19/2012,Local Transportation,No Descriotion,No comments';
String[] values = email.plainTextBody.split(',');
er.Contact__c = values[0];

String s = values[1];
Integer i = (Integer.valueof(s.trim()));
er.Amount__c = i;

String dt = values[2];
String[] stringDate = dt.split('/');
Integer m = Integer.valueOf(stringDate[0]);
Integer d = Integer.valueOf(stringDate[1]);
Integer y = Integer.valueOf(stringDate[2]);
Date reimb = date.newInstance(y,m,d);
er.Date__c = reimb;

er.Type__c = values[3];

er.Description__c = values[4];
er.Comments__c = values[5];

// call the email service class and test it with the data in the testMethod
EmailTemplate emailProcess = new EmailTemplate();
emailProcess.handleInboundEmail(email, env);

// query for the contact the email service created
Products__c p1 = [select Contact__c, Amount__c, Date__c, Type__c,Description__c,Comments__c
from Products__c where Id = :er.id ];

System.assertEquals(p1.Contact__c,values[0]);
System.assertEquals(p1.Amount__c,i);
System.assertEquals(p1.Date__c,reimb);

This was selected as the best answer