You need to sign in to do that
Don't have an account?
Navya sree 4
Test Class coverage for Help?
Hi
Can anyone help me with test class
global class CreateTaskOnContactFromEmail implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
String myPlainText= '';
string sub = '';
List<Attachment> attachments = new List<Attachment>();
String docUrl = '';
myPlainText = email.plainTextBody;
sub = email.subject;
sub.contains('ref:');
Task newTask = new Task();
Account vAcc = new Account();
EmailMessage emailMsg = new EmailMessage();
try {
vAcc = [SELECT Id, Name, PersonEmail,OwnerId,Owner.Name FROM Account WHERE PersonEmail =:email.fromAddress AND IsPersonAccount = True
LIMIT 1];
Contact vCon = [SELECT Id, Name, Email FROM Contact WHERE Email = :email.fromAddress LIMIT 1];
if(vAcc.id != NULL){
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[]{vAcc.OwnerId};
message.subject = 'Task is Created for your Account';
String emailBody = 'Hi'+' '+vAcc.Owner.Name+'\n'+'A new Task is created for your Account'+' '+vAcc.Name;
message.plainTextBody = emailBody;
Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{message});
if(email.textAttachments != null)
{
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
String body = tAttachment.body;
Blob b = blob.valueOf(tAttachment.body);
String str = EncodingUtil.base64Encode(b);
ContentVersion conVer = new ContentVersion();
conVer.VersionData = EncodingUtil.base64Decode(str);
conVer.Title = tAttachment.fileName;
conVer.ContentLocation = 'S';
conVer.PathOnClient = tAttachment.fileName;
insert conVer;
conVer = [select ContentDocumentId from ContentVersion where id = :conVer.id limit 1];
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = vAcc.id;
cdl.ContentDocumentId = conVer.ContentDocumentId;
cdl.ShareType = 'I';
cdl.Visibility = 'InternalUsers';
insert cdl;
docUrl += tAttachment.fileName + '\n';
}
}
if(email.binaryAttachments != null)
{
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
ContentVersion conVer = new ContentVersion();
conVer.VersionData = bAttachment.body;
conVer.Title = bAttachment.fileName;
conVer.ContentLocation = 'S';
conVer.PathOnClient = bAttachment.fileName;
insert conVer;
conVer = [select ContentDocumentId from ContentVersion where id = :conVer.id limit 1];
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = vAcc.id;
cdl.ContentDocumentId = conVer.ContentDocumentId;
cdl.ShareType = 'I';
cdl.Visibility = 'InternalUsers';
insert cdl;
docUrl += bAttachment.fileName + '\n';
}
}
if(attachments.size() > 0){insert attachments;
system.debug(attachments[0]);}
emailMsg.RelatedToId =vAcc.Id;
emailMsg.TextBody = email.plainTextBody;
emailMsg.Subject = email.subject;
emailMsg.FromAddress = email.fromAddress;
emailMsg.Status = '2';
emailMsg.Incoming= True;
emailMsg.Attachment__c = docUrl;
insert emailMsg;
}
}catch (QueryException e) {
UTIL_LoggingService.logHandledException(e, UserInfo.getOrganizationId(),UserInfo.getOrganizationName(),'CreateTaskOnContactFromEmail',
'handleInboundEmail','', system.LoggingLevel.FINEST);
}
result.success = true;
return result;
}
}
Thanks
Can anyone help me with test class
global class CreateTaskOnContactFromEmail implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
String myPlainText= '';
string sub = '';
List<Attachment> attachments = new List<Attachment>();
String docUrl = '';
myPlainText = email.plainTextBody;
sub = email.subject;
sub.contains('ref:');
Task newTask = new Task();
Account vAcc = new Account();
EmailMessage emailMsg = new EmailMessage();
try {
vAcc = [SELECT Id, Name, PersonEmail,OwnerId,Owner.Name FROM Account WHERE PersonEmail =:email.fromAddress AND IsPersonAccount = True
LIMIT 1];
Contact vCon = [SELECT Id, Name, Email FROM Contact WHERE Email = :email.fromAddress LIMIT 1];
if(vAcc.id != NULL){
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[]{vAcc.OwnerId};
message.subject = 'Task is Created for your Account';
String emailBody = 'Hi'+' '+vAcc.Owner.Name+'\n'+'A new Task is created for your Account'+' '+vAcc.Name;
message.plainTextBody = emailBody;
Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{message});
if(email.textAttachments != null)
{
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
String body = tAttachment.body;
Blob b = blob.valueOf(tAttachment.body);
String str = EncodingUtil.base64Encode(b);
ContentVersion conVer = new ContentVersion();
conVer.VersionData = EncodingUtil.base64Decode(str);
conVer.Title = tAttachment.fileName;
conVer.ContentLocation = 'S';
conVer.PathOnClient = tAttachment.fileName;
insert conVer;
conVer = [select ContentDocumentId from ContentVersion where id = :conVer.id limit 1];
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = vAcc.id;
cdl.ContentDocumentId = conVer.ContentDocumentId;
cdl.ShareType = 'I';
cdl.Visibility = 'InternalUsers';
insert cdl;
docUrl += tAttachment.fileName + '\n';
}
}
if(email.binaryAttachments != null)
{
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
ContentVersion conVer = new ContentVersion();
conVer.VersionData = bAttachment.body;
conVer.Title = bAttachment.fileName;
conVer.ContentLocation = 'S';
conVer.PathOnClient = bAttachment.fileName;
insert conVer;
conVer = [select ContentDocumentId from ContentVersion where id = :conVer.id limit 1];
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.LinkedEntityId = vAcc.id;
cdl.ContentDocumentId = conVer.ContentDocumentId;
cdl.ShareType = 'I';
cdl.Visibility = 'InternalUsers';
insert cdl;
docUrl += bAttachment.fileName + '\n';
}
}
if(attachments.size() > 0){insert attachments;
system.debug(attachments[0]);}
emailMsg.RelatedToId =vAcc.Id;
emailMsg.TextBody = email.plainTextBody;
emailMsg.Subject = email.subject;
emailMsg.FromAddress = email.fromAddress;
emailMsg.Status = '2';
emailMsg.Incoming= True;
emailMsg.Attachment__c = docUrl;
insert emailMsg;
}
}catch (QueryException e) {
UTIL_LoggingService.logHandledException(e, UserInfo.getOrganizationId(),UserInfo.getOrganizationName(),'CreateTaskOnContactFromEmail',
'handleInboundEmail','', system.LoggingLevel.FINEST);
}
result.success = true;
return result;
}
}
Thanks
It is giving only 30% coverage only
Thanks