You need to sign in to do that
Don't have an account?
Can Somebody Help me with this Test Class
global class SendNPSbutton {
@AuraEnabled
public static void sendNPSEmail(Id cId)
{
EmailTemplate emiailTemp = new EmailTemplate();
String htmlBody;
String plainBody;
String subject;
List<String> sendTo = new List<String>();
contact cc = [select id from contact limit 1];
Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cId LIMIT 1];
if(cpps.Id != null)
{
if(cpps.RecordType.Name =='Listing Team'){
sendTo.add(cpps.Seller_Email__c);
emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
subject = emiailTemp.Subject;
htmlBody= emiailTemp.HtmlValue;
htmlBody = htmlBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__r.name);
htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
plainBody = emiailTemp.Body;
plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
}else{
sendTo.add(cpps.Buyer_Email__c);
emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_nps'];
subject = emiailTemp.Subject;
htmlBody= emiailTemp.HtmlValue;
htmlBody = htmlBody.replace('{!Closing__c.Buyer_Account__c}', cpps.Appointment__r.Name);
htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
plainBody = emiailTemp.Body;
plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
}
// process the merge fields
// String subject = emailTemplate.Subject;
// subject = subject.replace('{!Contact.FirstName}', c.FirstName);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(emiailTemp.Id);
List<String> ccTo = new List<String>();
ccTo.add('xyz@gomail.com');
mail.setCcAddresses(ccTo);
mail.setReplyTo('xyz@gomail.com');
mail.setSenderDisplayName('XYZ');
mail.setTargetObjectId(cc.id);
mail.setTreatTargetObjectAsRecipient(false);
mail.setWhatId(cpps.Id);
mail.setToAddresses(sendTo);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody(htmlBody);
mail.setSubject(subject);
mail.setPlainTextBody(plainBody);
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
TEST Class:
@istest(SeeAllData=true)
public class SendNPSButton_test{
static testmethod void testvalidate(){
List<String> sendTo = new List<String>();
Account acc = new Account();
acc.Name = 'Name test';
insert acc;
Closing__c cl = new Closing__c();
//cl.RecordTypeId=Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Buying Team').getRecordTypeId();
cl.Buyer_Email__c = 'ravi.7293@gmail.com';
cl.Seller_Email__c ='ravi.7293@gmail.com';
cl.Seller_Account__c = acc.Id;
insert cl;
EmailTemplate emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
/*Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cl.Id LIMIT 1];*/
system.debug('cl>>: '+ cl);
Test.startTest();
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.setTemplateId(emiailTemp.Id);
//mail.setToAddresses(sendTo);
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('cl11>>: '+ cl.Seller_Email__c);
system.debug('cID>>: '+ cl.ID);
SendNPSbutton.sendNPSEmail(cl.Id);
//Integer emailInvocations = Limits.getEmailInvocations();
Test.stopTest();
//system.assertEquals(1, emailInvocations, 'An email should be sent');
}
}
System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Email address is invalid: null: [toAddresses, null]
Try the below test class :
@isTest
public class SendNPSButton_test{
static testmethod void testvalidate(){
Account acc = new Account();
acc.Name = 'Name test';
// Add the remaining mandatory fields to insert the Seller Account
insert acc;
Id ClosingRecordTypeId = Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Listing Team').getRecordTypeId();
Closing__c cl = new Closing__c();
cl.RecordTypeId=ClosingRecordTypeId;
cl.Buyer_Email__c = 'ravi.7293@gmail.com';
cl.Seller_Email__c ='ravi.7293@gmail.com';
cl.Seller_Account__c = acc.Id;
// Add the remaining mandatory fields to insert the Closing Record
insert cl;
Test.startTest();
SendNPSbutton.sendNPSEmail(cl.Id);
Test.stopTest();
}
}
Thanks,
Maharajan.C
@Maharajan.C
system.QueryException: List has no rows for assignment to SObject
If i try to deploy it. I am getting code coverage issue. (Your code coverage is 74%. You need at least 75% coverage to complete this deployment.)
Contact also needs to inserted in your test class:
@isTest
public class SendNPSButton_test{
static testmethod void testvalidate(){
Account acc = new Account();
acc.Name = 'Name test';
// Add the remaining mandatory fields to insert the Seller Account
insert acc;
Contact con = new Contact();
con.LastName = 'Test Con';
con.AccountId = acc.Id;
// Add the remaining mandatory fields to insert the Contact
insert con;
Id ClosingRecordTypeId = Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Listing Team').getRecordTypeId();
Closing__c cl = new Closing__c();
cl.RecordTypeId=ClosingRecordTypeId;
cl.Buyer_Email__c = 'ravi.7293@gmail.com';
cl.Seller_Email__c ='ravi.7293@gmail.com';
cl.Seller_Account__c = acc.Id;
// Add the remaining mandatory fields to insert the Closing Record
insert cl;
Test.startTest();
SendNPSbutton.sendNPSEmail(cl.Id);
Test.stopTest();
}
static testmethod void testvalidate1(){
Account acc = new Account();
acc.Name = 'Name test';
// Add the remaining mandatory fields to insert the Seller Account
insert acc;
Contact con = new Contact();
con.LastName = 'Test Con';
con.AccountId = acc.Id;
// Add the remaining mandatory fields to insert the Contact
insert con;
Closing__c cl = new Closing__c();
cl.Buyer_Email__c = 'ravi.7293@gmail.com';
cl.Seller_Email__c ='ravi.7293@gmail.com';
cl.Seller_Account__c = acc.Id;
// Add the remaining mandatory fields to insert the Closing Record
insert cl;
Test.startTest();
SendNPSbutton.sendNPSEmail(cl.Id);
Test.stopTest();
}
}
Thanks,
Maharajan.C
I tried the test class in this way and i see coverage issue when i tried deploying it.
@istest
public class SendNPSButton_test{
static testmethod void testvalidate(){
Test.startTest();
List<String> sendTo = new List<String>();
Account acc = new Account();
acc.Name = 'Name test';
insert acc;
Closing__c cl = new Closing__c();
if(cl.Seller_Email__c != null){
//cl.RecordTypeId=Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Buying Team').getRecordTypeId();
cl.Buyer_Email__c = 'ravigutha@markspain.com';
cl.Seller_Email__c ='ravigutha@markspain.com';
cl.Seller_Account__c = acc.Id;
insert cl;
EmailTemplate emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
/*Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cl.Id LIMIT 1];*/
system.debug('cl>>: '+ cl);
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.setTemplateId(emiailTemp.Id);
//mail.setToAddresses(sendTo);
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('cl11>>: '+ cl.Seller_Email__c);
SendNPSbutton.sendNPSEmail(cl.Id);
//Integer emailInvocations = Limits.getEmailInvocations();
Test.stopTest();
//system.assertEquals(1, emailInvocations, 'An email should be sent');
}
}
}
or add a screenshot of it?
Did you have tried the test class which i have recently posted.
Thanks,
Maharajan.C