-
ChatterFeed
-
1Best Answers
-
0Likes Received
-
0Likes Given
-
1Questions
-
7Replies
Help in increasing test coverage from 66%
I would like to increase the test code coverage of the following class which currently is 66%
Controller:
public class SendEmail { private Attachment attachment; private String intvId; private Interview__c interview; private EmailTemplate eTemplate; private Contact con; private Candidates__c cand; private User user; public SendEmail(ApexPages.StandardController controller) { intvId = ApexPages.currentPage().getParameters().get('id'); if (intvId == null) { interview = new Interview__c(); } else { interview = [select Name,id,Client_Names__r.Email,Candidate_Names__c,Client_Names__c,Interview_Date__c,Interview_Time__c,Address_of_Interview__c from Interview__c where id = :intvId]; con = [Select Id,Name,FirstName,Email from Contact where Id = :interview.Client_Names__c]; cand = [Select Id,Name,First_Name__c from Candidates__c where Id = :interview.Candidate_Names__c]; eTemplate = [Select id,Body,Subject,DeveloperName from EmailTemplate where DeveloperName = 'Interview_Client_Email' ]; user = [Select Id,FirstName,LastName from User where Id = :UserInfo.getUserId()]; } } public Interview__c getInterview() { return interview; } public EmailTemplate getETemplate() { return eTemplate; } public Contact getCon() { return con; } public Candidates__c getCand() { return cand; } public User gerUser() { return user; } public PageReference send() { // Define the email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); String emailMessage = 'Dear ' +con.FirstName +',' +'\n<br/>'+ +'\n<br/>'+ 'I am pleased to confirm your interview with our candidate ' + cand.First_Name__c +'.' +'We have scheduled the interview for ' + interview.Interview_Date__c +' at ' + interview.Interview_Time__c +' at ' + interview.Address_of_Interview__c +'.' +'\n<br/>'+ 'As I mentioned previously, it is our pleasure to have the chance ' + 'to work with you and I wish you the very best of luck with this candidate.' +'\n<br/>'+ +'\n<br/>'+ +'Kindest regards,' +'\n<br/>'+ +user.FirstName+' ' +user.LastName; email.setTargetObjectId(con.Id); email.setSubject('Interview Confirmation'); email.setPlainTextBody(emailMessage); email.setHtmlBody(emailMessage); email.setBccSender(true); List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :interview.Candidate_Names__c and (NOT Name like 'NDA%') and(Not Name like 'P33 Consultant NDA%') ]) { // Add to attachment file list Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); } email.setFileAttachments(fileAttachments); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); PageReference pg = new PageReference('https://na1.salesforce.com/'+intvId); pg.setRedirect(true); return pg; } } |
Test Case:
@istest private class SendEmailTest { static testmethod void testSendEmail(){ Profile p = [select id from profile where name='System Administrator']; User u = new User(alias = 'utest', email='unit.test@unit.test.com', emailencodingkey='UTF-8', lastname='Unit Test', languagelocalekey='en_US', localesidkey='en_GB', profileid = p.Id, timezonesidkey='Europe/London', username='unit.test@unit.test.com'); System.runAs(u) { Contact con = new Contact( Lastname = 'test contact', Email = 'test@test.com'); insert con; Candidates__c cand = new Candidates__C ( First_Name__c = 'Test Name', Name = 'Test Last name', Candidate_Address__c = 'Test Address', Main_Email__c = 'test@test1.com', Candidate_Source__c = 'Test Source'); insert cand; String folder = [select id,name from Folder where type='Email'][0].id; EmailTemplate eTemp = new EmailTemplate ( Subject = 'Test Subject', Body = 'Test Body', folderId = folder, TemplateType = 'Text', Name = 'Test Name', DeveloperName = 'test'); insert eTemp; Interview__c intv = new Interview__c ( Client_Names__c = con.Id, Candidate_Names__c = cand.Id, Interview_Date__c = Date.today(), Interview_Time__c = 'time', Address_of_Interview__c = 'test address'); insert intv; ApexPages.StandardController controller = new ApexPages.StandardController(intv); ApexPages.currentPage().getParameters().put('id' , intv.id); //Create instance of controller SendEmail testSend = new SendEmail(controller); test.startTest(); //call the method that you want to test using instance of class PageReference pg = testSend.Send(); List<Interview__c> intrv = [SELECT ID, Name,Client_Names__c FROM Interview__c WHERE ID = :intv.Id]; System.assertEquals(intv.Client_Names__c,intrv[0].Client_Names__c); test.stopTest(); } } } |
- Sayasoni
- July 25, 2011
- Like
- 0
Send Mass Email from a custom Object
Hi good People,
Am trying to implement Sending Mass email on a Candidate Custom Object. I understand the standard Messaging.MassEmailMessage is not supported by custom objects.How do i get to use Messaging.SingleEmailMessage to send mass email by looping through the selected records?
public class WrapperMassEmail {
public String aMessage {
get; set;
}
public List<string> candidateList1=new List<string>();
//Our collection of the class/wrapper objects cCandidate
public List<cCandidate> candidateList {get; set;}
//This method uses a simple SOQL query to return a List of Candidates
public List<cCandidate> getCandidates(){
if(candidateList == null){
candidateList = new List<cCandidate>();
for(Candidates__c c : [select Id, First_Name__c,Name, Main_Email__c from Candidates__c limit 10]){
candidateList.add(new cCandidate(c));
}
}
return candidateList;
}
public PageReference processSelected(){
/*We create a new list of Candidates that we be populated only with Candidates
if they are selected*/
List<Candidates__c> selectedCandidates = new List<Candidates__c>();
this.aMessage = '';
/*We will cycle through our list of cCandidate and will check to see if the
selected property is set to true, if it is we add the Contact to the
selectedCandidates list. */
for(cCandidate cCon : getCandidates()){
if(cCon.selected == true){
selectedCandidates.add(cCon.con);
}
}
/* Now we have our list of selected candidates and can perform any type of
logic we want, sending emails, updating a field on the Contact, etc */
System.debug('These are the selected Candidates...');
Integer numselectedCandidates = selectedCandidates.size();
Integer counter = 0;
System.Debug(selectedCandidates);
for(Candidates__c con : selectedCandidates){
counter++;
if(counter==numselectedCandidates) {
this.aMessage += con.Id + ','+ con.First_Name__c+',' + con.Main_Email__c;
candidateList1.add(con.Id);
} else {
this.aMessage += con.Id + ','+con.First_Name__c+','+ con.Main_Email__c+', ';
candidateList1.add(con.Id);
}
}
return null;
}
/* This is our wrapper/container class. A container class is a class, a data
structure, or an abstract data type whose instances are collections of other
objects. In this example a wrapper class contains both the standard salesforce
object Contact and a Boolean value */
public class cCandidate
{
public Candidates__c con {get; set;}
public Boolean selected {get; set;}
/*This is the contructor method. When we create a new cCandidate object we pass a
candi that is set to the con property. We also set the selected value to false*/
public cCandidate(Candidates__c c){
con = c;
selected = false;
}
}
public PageReference send()
{
EmailTemplate eTemplate = [Select id,Body,Subject,DeveloperName from EmailTemplate where DeveloperName = 'Interview_Client_Email' ];
//Trying to get the ids of the selected candidates
List<Candidates__c> cand = [Select Id,Name,First_Name__c,Main_Email__c from Candidates__c where Id = :];
String[] address = new String[] {
cand[0].Main_Email__c
};
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(eTemplate.id);
email.setToAddresses(address);
email.setBccSender(true);
email.setSaveAsActivity(true);
PageReference Page = new PageReference('/' + Schema.getGlobalDescribe().get('Candidates__c').getDescribe().getKeyPrefix() + '/o');
Page.setRedirect(true);
return Page;
}
}
mail.setBccSender(true);
mail.setSaveAsActivity(true);
- Saya
- August 01, 2011
- Like
- 0
Send Mass Email from a custom Object
Hi good People,
Am trying to implement Sending Mass email on a Candidate Custom Object. I understand the standard Messaging.MassEmailMessage is not supported by custom objects.How do i get to use Messaging.SingleEmailMessage to send mass email by looping through the selected records?
public class WrapperMassEmail {
public String aMessage {
get; set;
}
public List<string> candidateList1=new List<string>();
//Our collection of the class/wrapper objects cCandidate
public List<cCandidate> candidateList {get; set;}
//This method uses a simple SOQL query to return a List of Candidates
public List<cCandidate> getCandidates(){
if(candidateList == null){
candidateList = new List<cCandidate>();
for(Candidates__c c : [select Id, First_Name__c,Name, Main_Email__c from Candidates__c limit 10]){
candidateList.add(new cCandidate(c));
}
}
return candidateList;
}
public PageReference processSelected(){
/*We create a new list of Candidates that we be populated only with Candidates
if they are selected*/
List<Candidates__c> selectedCandidates = new List<Candidates__c>();
this.aMessage = '';
/*We will cycle through our list of cCandidate and will check to see if the
selected property is set to true, if it is we add the Contact to the
selectedCandidates list. */
for(cCandidate cCon : getCandidates()){
if(cCon.selected == true){
selectedCandidates.add(cCon.con);
}
}
/* Now we have our list of selected candidates and can perform any type of
logic we want, sending emails, updating a field on the Contact, etc */
System.debug('These are the selected Candidates...');
Integer numselectedCandidates = selectedCandidates.size();
Integer counter = 0;
System.Debug(selectedCandidates);
for(Candidates__c con : selectedCandidates){
counter++;
if(counter==numselectedCandidates) {
this.aMessage += con.Id + ','+ con.First_Name__c+',' + con.Main_Email__c;
candidateList1.add(con.Id);
} else {
this.aMessage += con.Id + ','+con.First_Name__c+','+ con.Main_Email__c+', ';
candidateList1.add(con.Id);
}
}
return null;
}
/* This is our wrapper/container class. A container class is a class, a data
structure, or an abstract data type whose instances are collections of other
objects. In this example a wrapper class contains both the standard salesforce
object Contact and a Boolean value */
public class cCandidate
{
public Candidates__c con {get; set;}
public Boolean selected {get; set;}
/*This is the contructor method. When we create a new cCandidate object we pass a
candi that is set to the con property. We also set the selected value to false*/
public cCandidate(Candidates__c c){
con = c;
selected = false;
}
}
public PageReference send()
{
EmailTemplate eTemplate = [Select id,Body,Subject,DeveloperName from EmailTemplate where DeveloperName = 'Interview_Client_Email' ];
//Trying to get the ids of the selected candidates
List<Candidates__c> cand = [Select Id,Name,First_Name__c,Main_Email__c from Candidates__c where Id = :];
String[] address = new String[] {
cand[0].Main_Email__c
};
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(eTemplate.id);
email.setToAddresses(address);
email.setBccSender(true);
email.setSaveAsActivity(true);
PageReference Page = new PageReference('/' + Schema.getGlobalDescribe().get('Candidates__c').getDescribe().getKeyPrefix() + '/o');
Page.setRedirect(true);
return Page;
}
}
mail.setBccSender(true);
mail.setSaveAsActivity(true);
- Saya
- August 01, 2011
- Like
- 0
Help in increasing test coverage from 66%
I would like to increase the test code coverage of the following class which currently is 66%
Controller:
public class SendEmail { private Attachment attachment; private String intvId; private Interview__c interview; private EmailTemplate eTemplate; private Contact con; private Candidates__c cand; private User user; public SendEmail(ApexPages.StandardController controller) { intvId = ApexPages.currentPage().getParameters().get('id'); if (intvId == null) { interview = new Interview__c(); } else { interview = [select Name,id,Client_Names__r.Email,Candidate_Names__c,Client_Names__c,Interview_Date__c,Interview_Time__c,Address_of_Interview__c from Interview__c where id = :intvId]; con = [Select Id,Name,FirstName,Email from Contact where Id = :interview.Client_Names__c]; cand = [Select Id,Name,First_Name__c from Candidates__c where Id = :interview.Candidate_Names__c]; eTemplate = [Select id,Body,Subject,DeveloperName from EmailTemplate where DeveloperName = 'Interview_Client_Email' ]; user = [Select Id,FirstName,LastName from User where Id = :UserInfo.getUserId()]; } } public Interview__c getInterview() { return interview; } public EmailTemplate getETemplate() { return eTemplate; } public Contact getCon() { return con; } public Candidates__c getCand() { return cand; } public User gerUser() { return user; } public PageReference send() { // Define the email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); String emailMessage = 'Dear ' +con.FirstName +',' +'\n<br/>'+ +'\n<br/>'+ 'I am pleased to confirm your interview with our candidate ' + cand.First_Name__c +'.' +'We have scheduled the interview for ' + interview.Interview_Date__c +' at ' + interview.Interview_Time__c +' at ' + interview.Address_of_Interview__c +'.' +'\n<br/>'+ 'As I mentioned previously, it is our pleasure to have the chance ' + 'to work with you and I wish you the very best of luck with this candidate.' +'\n<br/>'+ +'\n<br/>'+ +'Kindest regards,' +'\n<br/>'+ +user.FirstName+' ' +user.LastName; email.setTargetObjectId(con.Id); email.setSubject('Interview Confirmation'); email.setPlainTextBody(emailMessage); email.setHtmlBody(emailMessage); email.setBccSender(true); List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :interview.Candidate_Names__c and (NOT Name like 'NDA%') and(Not Name like 'P33 Consultant NDA%') ]) { // Add to attachment file list Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment(); efa.setFileName(a.Name); efa.setBody(a.Body); fileAttachments.add(efa); } email.setFileAttachments(fileAttachments); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); PageReference pg = new PageReference('https://na1.salesforce.com/'+intvId); pg.setRedirect(true); return pg; } } |
Test Case:
@istest private class SendEmailTest { static testmethod void testSendEmail(){ Profile p = [select id from profile where name='System Administrator']; User u = new User(alias = 'utest', email='unit.test@unit.test.com', emailencodingkey='UTF-8', lastname='Unit Test', languagelocalekey='en_US', localesidkey='en_GB', profileid = p.Id, timezonesidkey='Europe/London', username='unit.test@unit.test.com'); System.runAs(u) { Contact con = new Contact( Lastname = 'test contact', Email = 'test@test.com'); insert con; Candidates__c cand = new Candidates__C ( First_Name__c = 'Test Name', Name = 'Test Last name', Candidate_Address__c = 'Test Address', Main_Email__c = 'test@test1.com', Candidate_Source__c = 'Test Source'); insert cand; String folder = [select id,name from Folder where type='Email'][0].id; EmailTemplate eTemp = new EmailTemplate ( Subject = 'Test Subject', Body = 'Test Body', folderId = folder, TemplateType = 'Text', Name = 'Test Name', DeveloperName = 'test'); insert eTemp; Interview__c intv = new Interview__c ( Client_Names__c = con.Id, Candidate_Names__c = cand.Id, Interview_Date__c = Date.today(), Interview_Time__c = 'time', Address_of_Interview__c = 'test address'); insert intv; ApexPages.StandardController controller = new ApexPages.StandardController(intv); ApexPages.currentPage().getParameters().put('id' , intv.id); //Create instance of controller SendEmail testSend = new SendEmail(controller); test.startTest(); //call the method that you want to test using instance of class PageReference pg = testSend.Send(); List<Interview__c> intrv = [SELECT ID, Name,Client_Names__c FROM Interview__c WHERE ID = :intv.Id]; System.assertEquals(intv.Client_Names__c,intrv[0].Client_Names__c); test.stopTest(); } } } |
- Sayasoni
- July 25, 2011
- Like
- 0
Send an Email button (on the tasks related list) for custom objects.
So I've done some searching and this seems to be a fairly common problem. I am in fact under the impression that there is no easy fix. Any guidance or direction would be greatly appreciated.
In a nutshell:
I created a custom object to represent certain types of clients. I cannot however send them an email directly from the records send an email button. The task looks for a contact and does not accept an arbitrary email address in its "to" field.
Any help would be greatly appreciated.
- DF
- February 03, 2011
- Like
- 0
Sending an email on a click of a button
Hi
I have an Invoice object and I want to create a button that on-click will send an email with the related Invoice information to the customer.
I have created an email template for this but I'm not sure how to write the APEX code such that when I mark an Invoice and press the button an email will be sent to the contact designated in the invoice including the rest of the detailes in the invoice.
If not APEX is there any other way to achieve the same?
Thanks
- Kruvi
- November 26, 2010
- Like
- 0
Not Able to send MassMail from Apex Class.
Hi,
I'm developing apex class to send mass emails from selected records, when i select records and click on send button it show runtime error.
I'm getting this error message
System.NullPointerException: Attempt to de-reference a null object
Wat may be the problem? Please help me to rectify this.
Apex Class code:
public class wrapperClassController3 {
public String aMessage {
get; set;
}
public List<ID> studentids;
public List<string> studentList1=new List<string>();
//public String[] studentList1 = new List<String>();
//Our collection of the class/wrapper objects cStudent
public List<cStudent> studentList {get; set;}
//This method uses a simple SOQL query to return a List of Students
public List<cStudent> getStudents(){
if(studentList == null){
studentList = new List<cStudent>();
for(Student__c c : [select Id, First_Name__c,Last_Name__c, Email__c from Student__c limit 10]){
studentList.add(new cStudent(c));
}
}
return studentList;
}
public PageReference processSelected(){
/*We create a new list of Student that we be populated only with Students
if they are selected*/
List<Student__c> selectedStudents = new List<Student__c>();
this.aMessage = '';
/*We will cycle through our list of cStudents and will check to see if the
selected property is set to true, if it is we add the Contact to the
selectedStudentss list. */
for(cStudent cCon : getStudents()){
if(cCon.selected == true){
selectedStudents.add(cCon.con);
}
}
/* Now we have our list of selected contacts and can perform any type of
logic we want, sending emails, updating a field on the Contact, etc */
System.debug('These are the selected Students...');
Integer numselectedStudents = selectedStudents.size();
Integer counter = 0;
System.Debug(selectedStudents);
for(Student__c con : selectedStudents){
counter++;
//system.debug(con);
if(counter==numselectedStudents) {
this.aMessage += con.Id + ','+ con.First_Name__c+',' + con.Email__c;
studentList1.add(con.Id);
} else {
this.aMessage += con.Id + ','+con.First_Name__c+','+ con.Email__c+', ';
}
}
return null;
}
/* This is our wrapper/container class. A container class is a class, a data
structure, or an abstract data type whose instances are collections of other
objects. In this example a wrapper class contains both the standard salesforce
object Contact and a Boolean value */
public class cStudent
{
public Student__c con {get; set;}
public Boolean selected {get; set;}
/*This is the contructor method. When we create a new cContact object we pass a
Contact that is set to the con property. We also set the selected value to false*/
public cStudent(Student__c c){
con = c;
selected = false;
}
}
// public class mail
//{
// public String subject { get; set; }
// public String body{ get; set; }
public PageReference send()
{
Messaging.MassEmailMessage email = new Messaging.MassEmailMessage();
// String[] toAddresses = new String[] {};
List<String> TargetObjectIds = new List<String>();
//for(String s:studentList1)
//{
// String[] toAddresses = new String[] {s};
// TargetObjectIds.add(s);
//}
// ID s;
for(ID s:studentids)
{
TargetObjectIds.add(s);
}
email.setSubject( 'subject' );
//String[] toMassTargetObjects = new String[]{toMassTargetObjects};
email.setTargetObjectIds(TargetObjectIds);
//email.setPlainTextBody( body );
email.setReplyTo('chiranjeevi@datasisar.com');
email.setSenderDisplayName('Customer Support');
try{
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.MassEmailMessage[] {email});
PageReference Page = new PageReference('/mailinfo');
Page.setRedirect(true);
return Page;
// return null;
}
catch(Exception e)
{
return null;
}
}
//}
}
Visualforce Code.
<apex:page controller="wrapperClassController3">
<apex:outputPanel id="selected"><b>Selected: {!aMessage}</b></apex:outputPanel>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Process Selected" action="{!processSelected}" rerender="selected"/>
<apex:commandButton value="Send" action="{!send}"/>
</apex:pageBlockButtons>
<!-- In our table we are displaying the cStudent records -->
<apex:pageBlockTable value="{!students}" var="c" id="table">
<apex:column >
<!-- This is our selected Boolean property in our wrapper class -->
<apex:inputCheckbox value="{!c.selected}"/>
</apex:column>
<!-- This is how we access the student values within our cStudent container/wrapper -->
<apex:column value="{!c.con.First_Name__c}" />
<apex:column value="{!c.con.Last_Name__c}" />
<apex:column value="{!c.con.Email__c}" />
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Thank you.
- Coder
- December 19, 2009
- Like
- 0