You need to sign in to do that
Don't have an account?
Controller Extension Test Class
Please help me with the Test Class for the below Controller :
public class ActivitySearchController {
Public List<Task> TaskList {get;set;}
String currentId =ApexPages.CurrentPage().getparameters().get('id');
public ActivitySearchController(ApexPages.StandardController controller) {
//Lead ID
if (currentId.startsWith('00Q')) {
try{
List <Lead> leadList = [SELECT id, Email, Alternate_Email__c FROM Lead WHERE ID=:currentId];
String ldEmail = leadList[0].Email;
String ldAltEmail = leadList[0].Alternate_Email__c;
Set<String> emailIds = new Set<string>();
if(ldEmail!=null){
emailIds.add(ldEmail);
}
if(ldAltEmail!=null){
emailIds.add(ldAltEmail);
}
List <Lead> rldLead = [SELECT id, Email, Alternate_Email__c FROM Lead Where Email IN:emailIds OR Alternate_Email__c IN:emailIds];
List <Account> accLst = [SELECT id, PersonEmail, Alternate_Email__c FROM Account Where PersonEmail IN:emailIds OR Alternate_Email__c IN:emailIds];
Set<Id> leadID = new Set<Id>();
Set<Id> accIds = new Set<Id>();
for(Lead lE : rldLead){
leadID.add(lE.id);
}
for(Account acc : accLst){
accIds.add(acc.id);
}
TaskList = [Select id, Subject, Comments_Short__c, who.Type, What.Type, Priority, Status, ActivityDate FROM Task
WHERE (accountid IN:accIds OR whoId IN:leadID) AND Status = 'Open' ORDER BY createddate DESC];
if(TaskList.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
}
catch(Exception e){
system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
}
}
//Account ID
else if (currentId.startsWith('001p')) {
try{
List <Account> accList = [SELECT id, PersonEmail, Alternate_Email__c FROM Account WHERE ID=:currentId];
String ldEmail = accList[0].PersonEmail;
String ldAltEmail = accList[0].Alternate_Email__c;
Set<String> emailIds = new Set<string>();
if(ldEmail!=null){
emailIds.add(ldEmail);
}
if(ldAltEmail!=null){
emailIds.add(ldAltEmail);
}
List <Lead> rldLead = [SELECT id, Email, Alternate_Email__c FROM Lead Where Email IN:emailIds OR Alternate_Email__c IN:emailIds];
List <Account> accLst = [SELECT id, PersonEmail, Alternate_Email__c FROM Account Where PersonEmail IN:emailIds OR Alternate_Email__c IN:emailIds];
Set<Id> leadID = new Set<Id>();
Set<Id> accIds = new Set<Id>();
for(Lead lE : rldLead){
leadID.add(lE.id);
}
for(Account acc : accLst){
accIds.add(acc.id);
}
TaskList = [Select id, Subject, Comments_Short__c, who.Type, What.Type, Priority, Status, ActivityDate FROM Task
WHERE (accountid IN:accIds OR whoId IN:leadID) AND Status = 'Open' ORDER BY createddate DESC LIMIT 10];
if(TaskList.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
}
catch(Exception e){
system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
}
}
//Opportunity ID
else if (currentId.startsWith('006p')) {
try{
List <Opportunity> oppList = [SELECT id, AccountId FROM Opportunity WHERE ID=:currentId];
String oppID = oppList[0].Accountid;
List <Account> oppAccList = [SELECT id, PersonEmail, Alternate_Email__c FROM Account WHERE ID =: oppID];
String ldEmail = oppAccList[0].PersonEmail;
String ldAltEmail = oppAccList[0].Alternate_Email__c;
Set<String> emailIds = new Set<string>();
if(ldEmail!=null){
emailIds.add(ldEmail);
}
if(ldAltEmail!=null){
emailIds.add(ldAltEmail);
}
List <Lead> rldLead = [SELECT id, Email, Alternate_Email__c FROM Lead Where Email IN:emailIds OR Alternate_Email__c IN:emailIds];
List <Account> accLst = [SELECT id, PersonEmail, Alternate_Email__c FROM Account Where PersonEmail IN:emailIds OR Alternate_Email__c IN:emailIds];
Set<Id> leadID = new Set<Id>();
Set<Id> accIds = new Set<Id>();
for(Lead lE : rldLead){
leadID.add(lE.id);
}
for(Account acc : accLst){
accIds.add(acc.id);
}
TaskList = [Select id, Subject, Comments_Short__c, who.Type, What.Type, Priority, Status, ActivityDate FROM Task
WHERE (accountid IN:accIds OR whoId IN:leadID) AND Status = 'Open' ORDER BY createddate DESC LIMIT 10];
if(TaskList.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
}
catch(Exception e){
system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
}
}
}
}
public class ActivitySearchController {
Public List<Task> TaskList {get;set;}
String currentId =ApexPages.CurrentPage().getparameters().get('id');
public ActivitySearchController(ApexPages.StandardController controller) {
//Lead ID
if (currentId.startsWith('00Q')) {
try{
List <Lead> leadList = [SELECT id, Email, Alternate_Email__c FROM Lead WHERE ID=:currentId];
String ldEmail = leadList[0].Email;
String ldAltEmail = leadList[0].Alternate_Email__c;
Set<String> emailIds = new Set<string>();
if(ldEmail!=null){
emailIds.add(ldEmail);
}
if(ldAltEmail!=null){
emailIds.add(ldAltEmail);
}
List <Lead> rldLead = [SELECT id, Email, Alternate_Email__c FROM Lead Where Email IN:emailIds OR Alternate_Email__c IN:emailIds];
List <Account> accLst = [SELECT id, PersonEmail, Alternate_Email__c FROM Account Where PersonEmail IN:emailIds OR Alternate_Email__c IN:emailIds];
Set<Id> leadID = new Set<Id>();
Set<Id> accIds = new Set<Id>();
for(Lead lE : rldLead){
leadID.add(lE.id);
}
for(Account acc : accLst){
accIds.add(acc.id);
}
TaskList = [Select id, Subject, Comments_Short__c, who.Type, What.Type, Priority, Status, ActivityDate FROM Task
WHERE (accountid IN:accIds OR whoId IN:leadID) AND Status = 'Open' ORDER BY createddate DESC];
if(TaskList.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
}
catch(Exception e){
system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
}
}
//Account ID
else if (currentId.startsWith('001p')) {
try{
List <Account> accList = [SELECT id, PersonEmail, Alternate_Email__c FROM Account WHERE ID=:currentId];
String ldEmail = accList[0].PersonEmail;
String ldAltEmail = accList[0].Alternate_Email__c;
Set<String> emailIds = new Set<string>();
if(ldEmail!=null){
emailIds.add(ldEmail);
}
if(ldAltEmail!=null){
emailIds.add(ldAltEmail);
}
List <Lead> rldLead = [SELECT id, Email, Alternate_Email__c FROM Lead Where Email IN:emailIds OR Alternate_Email__c IN:emailIds];
List <Account> accLst = [SELECT id, PersonEmail, Alternate_Email__c FROM Account Where PersonEmail IN:emailIds OR Alternate_Email__c IN:emailIds];
Set<Id> leadID = new Set<Id>();
Set<Id> accIds = new Set<Id>();
for(Lead lE : rldLead){
leadID.add(lE.id);
}
for(Account acc : accLst){
accIds.add(acc.id);
}
TaskList = [Select id, Subject, Comments_Short__c, who.Type, What.Type, Priority, Status, ActivityDate FROM Task
WHERE (accountid IN:accIds OR whoId IN:leadID) AND Status = 'Open' ORDER BY createddate DESC LIMIT 10];
if(TaskList.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
}
catch(Exception e){
system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
}
}
//Opportunity ID
else if (currentId.startsWith('006p')) {
try{
List <Opportunity> oppList = [SELECT id, AccountId FROM Opportunity WHERE ID=:currentId];
String oppID = oppList[0].Accountid;
List <Account> oppAccList = [SELECT id, PersonEmail, Alternate_Email__c FROM Account WHERE ID =: oppID];
String ldEmail = oppAccList[0].PersonEmail;
String ldAltEmail = oppAccList[0].Alternate_Email__c;
Set<String> emailIds = new Set<string>();
if(ldEmail!=null){
emailIds.add(ldEmail);
}
if(ldAltEmail!=null){
emailIds.add(ldAltEmail);
}
List <Lead> rldLead = [SELECT id, Email, Alternate_Email__c FROM Lead Where Email IN:emailIds OR Alternate_Email__c IN:emailIds];
List <Account> accLst = [SELECT id, PersonEmail, Alternate_Email__c FROM Account Where PersonEmail IN:emailIds OR Alternate_Email__c IN:emailIds];
Set<Id> leadID = new Set<Id>();
Set<Id> accIds = new Set<Id>();
for(Lead lE : rldLead){
leadID.add(lE.id);
}
for(Account acc : accLst){
accIds.add(acc.id);
}
TaskList = [Select id, Subject, Comments_Short__c, who.Type, What.Type, Priority, Status, ActivityDate FROM Task
WHERE (accountid IN:accIds OR whoId IN:leadID) AND Status = 'Open' ORDER BY createddate DESC LIMIT 10];
if(TaskList.size() == 0)
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
}
catch(Exception e){
system.debug('getlinenumber-->'+ e.getMessage() +' line '+ e.getLineNumber());
}
}
}
}
Same thing goes for the exception, however, given the relative simplicity of the code I see no reason to have the code within try and catch blocks.
I suggest you get rid of the catch block unless you know a way or scenario in which an exception would be thrown, and if so, you can edit the tests to be able to test that scenario.
For the no tasks scenario, see updated test class below. I have created an additional NoTasks test method for each scenario
All Answers
{
Apexpages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,''+'No Tasks to Display'));
}
catch(Exception e){
Can you let me know what addition should be added to the Test Class.
Same thing goes for the exception, however, given the relative simplicity of the code I see no reason to have the code within try and catch blocks.
I suggest you get rid of the catch block unless you know a way or scenario in which an exception would be thrown, and if so, you can edit the tests to be able to test that scenario.
For the no tasks scenario, see updated test class below. I have created an additional NoTasks test method for each scenario