I'm struggling with the following problem for two days. I want to create a test class for my apex class. here is code of the apex class
global class Email2Lead 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();
//use default assignment rule
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
try {
//select lead
Lead k = this.get_recent_lead(email.fromAddress);
//select contact
Contact c = this.get_contact(email.fromAddress);
//brand new mail?
//Boolean first_touch = (k != null) & (c != null);
//not a contact, not a lead? create a new one!
//if (k != null && c != null) {
//create new lead
k = new Lead(
firstname = this.getContactFirstname(email.fromName),
lastname = this.getContactLastname(email.fromName),
company = this.getContactCompany(email.fromAddress),
email = email.fromAddress,
//email_origin__c = env.fromAddress,
leadsource = 'bant.io',
Description = email.plainTextBody
//lead_source_detail__c = 'joinus@wework.com'
);
//use lead assignment rules
k.setOptions(dmo);
//dml
insert k;
//re-query
k = [ SELECT id, ownerId, isConverted, convertedContactId, convertedAccountId
FROM Lead
WHERE id = :k.id
LIMIT 1
];
// }
//collect what
Id whatId = null;
}
catch(Exception pEx) {
result.success = false; //default value
system.debug(pEx.getMessage());
throw new Email2LeadException(pEx.getMessage());
}
return result;
}
private Lead get_recent_lead(String pEmail) {
Lead[] Ks = [ SELECT id, email, isConverted, convertedContactId, convertedContact.ownerId, ownerId, convertedAccountId
FROM Lead
WHERE email = :pEmail
ORDER BY createddate DESC
LIMIT 1
];
return Ks.size() > 0 ? Ks[0] : null;
}
private Contact get_contact(String pEmail) {
Contact[] Ks = [ SELECT id, AccountId, ownerId
FROM Contact
WHERE email = :pEmail
ORDER BY createddate DESC
LIMIT 1
];
return Ks.size() > 0 ? Ks[0] : null;
}
private String getContactFirstname(String pFrom) {
String result;
try {
result = pFrom.substring(0,pFrom.indexOf(' '));
}
catch(Exception pEx) {
result = ''; //default value
}
return result;
}
private String getContactLastname(String pFrom) {
String result;
try {
result = pFrom.indexOf(' ') > 0 ?
pFrom.substring(pFrom.indexOf(' ')) :
pFrom;
}
catch(Exception pEx) {
result = ''; //default value
}
return result;
}
private String getContactCompany(String pEmail) {
String result;
try {
result = pEmail.indexOf('@') > 0 ?
pEmail.substring(pEmail.indexOf('@') + 1) :
pEmail;
}
catch(Exception pEx) {
result = ''; //default value
}
return result;
}
public class Email2LeadException extends Exception{}
}
I am new to development and to salesforce. Anybody can help me out?