You need to sign in to do that
Don't have an account?
Parth Srivastava
help with test class of standard controller of case object: please find below code of main class
public class ShowRelatedDataClass {
// Properties
private ID CaseId ;
public List<String> items {get;set;}
static Map<String,Map<Id,List<sObject>>> allvalues;
public List<HealthCloudGA__EhrCondition__c> EHRConditions {get;set;}
public List<HealthCloudGA__EhrObservation__c> EHRObservations {get;set;}
public List<HealthCloudGA__EhrMedicationPrescription__c> EHRMedicationPrescriptions {get;set;}
public List<HealthCloudGA__EhrMedicationStatement__c> EHRMedicationStatements {get;set;}
// Constructor
public ShowRelatedDataClass(ApexPages.StandardController controller) {
caseId = ((Case)controller.getRecord()).Id;
case CaseRecord = [Select AccountId From Case Where Id = : caseId];
EHRConditions = new List<HealthCloudGA__EhrCondition__c>();
EHRConditions = [Select Name From HealthCloudGA__EhrCondition__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
EHRObservations = new List<HealthCloudGA__EhrObservation__c>();
EHRObservations = [Select Name From HealthCloudGA__EhrObservation__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
EHRMedicationPrescriptions = new List<HealthCloudGA__EhrMedicationPrescription__c>();
EHRMedicationPrescriptions = [Select Name From HealthCloudGA__EhrMedicationPrescription__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
EHRMedicationStatements = new List<HealthCloudGA__EhrMedicationStatement__c>();
EHRMedicationStatements = [Select Name From HealthCloudGA__EhrMedicationStatement__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
}
// Method to create map of Data of different tables
static Map<Id,List<sObject>> parentmap(String object_name){
String FieldSet = '';
if(object_name == 'HealthCloudGA__CarePlanProblem__c')
FieldSet = ' id,Name , HealthCloudGA__CarePlan__c ';
if(object_name == 'HealthCloudGA__CarePlanGoal__c')
FieldSet = ' Id, Name, HealthCloudGA__CarePlanProblem__c ';
if(object_name == 'Task')
FieldSet = ' Id, Subject, HealthCloudGA__CarePlanGoal__c ';
List<sObject> c2 = Database.query('select '+FieldSet +' from ' + object_name );
Map<Id,List<sObject>> m2 = new Map<Id,List<sObject>>();
String parentid = '';
for (sObject o2:c2) {
parentid = null;
if (object_name == 'HealthCloudGA__CarePlanProblem__c') {
parentid = ((HealthCloudGA__CarePlanProblem__c) o2).HealthCloudGA__CarePlan__c;
}
if (object_name == 'HealthCloudGA__CarePlanGoal__c') {
parentid = ((HealthCloudGA__CarePlanGoal__c) o2).HealthCloudGA__CarePlanProblem__c;
}
if (object_name == 'Task') {
parentid = ((Task) o2).HealthCloudGA__CarePlanGoal__c ;
}
if (parentid == null) continue;
if (m2.containsKey(parentid)) {
((List<sObject>) m2.get(parentid)).add(o2);
} else {
m2.put(parentid, new List<sObject>{o2});
}
}
return m2;
}
// Method to build pageblock Tables
public Component.Apex.PageBlockTable getHierarchy() {
Map<Id,Case> c1 = new Map<Id,Case>([select id, CaseNumber from Case Where Id = : caseId ]);
allvalues = new Map<String,Map<Id,List<sObject>>>();
allvalues.put('HealthCloudGA__CarePlanProblem__c',parentmap('HealthCloudGA__CarePlanProblem__c'));
allvalues.put('HealthCloudGA__CarePlanGoal__c',parentmap('HealthCloudGA__CarePlanGoal__c'));
allvalues.put('Task',parentmap('Task'));
items = new List<String>();
Component.Apex.PageBlockTable myTable = new Component.Apex.PageBlockTable(var='item');
myTable.expressions.value = '{!items}';
Component.Apex.column clm = new Component.Apex.column(headerValue= 'Name');
myTable.childComponents.add( clm );
Component.Apex.OutputText outText = new Component.Apex.OutputText( );
outText.expressions.value = '{!item}';
outText.escape = false;
clm.childComponents.add( outText );
for (Id id1 :c1.keyset()) {
Case o1 = (Case) c1.get(id1);
items.add(o1.CaseNumber);
getNodes('HealthCloudGA__CarePlanProblem__c',o1.id);
}
return myTable;
}
// Method to add child data.
public void getNodes(String object_name,String parentid) {
String name = '';
String id = '';
Map<Id,List<sObject>> map1 = (Map<Id,List<sObject>>) allvalues.get(object_name);
if (object_name == 'HealthCloudGA__CarePlanProblem__c') {
if (map1.containsKey(parentid)) {
List<HealthCloudGA__CarePlanProblem__c> lcob = (List<HealthCloudGA__CarePlanProblem__c>) map1.get(parentid);
for (HealthCloudGA__CarePlanProblem__c cob:lcob) {
name = cob.name;
items.add(' ' + name);
getNodes('HealthCloudGA__CarePlanGoal__c',cob.id);
}
}
} else if (object_name == 'HealthCloudGA__CarePlanGoal__c') {
if (map1.containsKey(parentid)) {
List<HealthCloudGA__CarePlanGoal__c> lcob = (List<HealthCloudGA__CarePlanGoal__c>) map1.get(parentid);
for (HealthCloudGA__CarePlanGoal__c cob:lcob) {
name = cob.name;
items.add(' ' + name);
getNodes('Task',cob.id);
}
}
} else if (object_name == 'Task') {
if (map1.containsKey(parentid)) {
List<Task> lcob = (List<Task>) map1.get(parentid);
for (Task cob:lcob) {
name = cob.Subject;
items.add(' '+name);
}} } }}
// Properties
private ID CaseId ;
public List<String> items {get;set;}
static Map<String,Map<Id,List<sObject>>> allvalues;
public List<HealthCloudGA__EhrCondition__c> EHRConditions {get;set;}
public List<HealthCloudGA__EhrObservation__c> EHRObservations {get;set;}
public List<HealthCloudGA__EhrMedicationPrescription__c> EHRMedicationPrescriptions {get;set;}
public List<HealthCloudGA__EhrMedicationStatement__c> EHRMedicationStatements {get;set;}
// Constructor
public ShowRelatedDataClass(ApexPages.StandardController controller) {
caseId = ((Case)controller.getRecord()).Id;
case CaseRecord = [Select AccountId From Case Where Id = : caseId];
EHRConditions = new List<HealthCloudGA__EhrCondition__c>();
EHRConditions = [Select Name From HealthCloudGA__EhrCondition__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
EHRObservations = new List<HealthCloudGA__EhrObservation__c>();
EHRObservations = [Select Name From HealthCloudGA__EhrObservation__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
EHRMedicationPrescriptions = new List<HealthCloudGA__EhrMedicationPrescription__c>();
EHRMedicationPrescriptions = [Select Name From HealthCloudGA__EhrMedicationPrescription__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
EHRMedicationStatements = new List<HealthCloudGA__EhrMedicationStatement__c>();
EHRMedicationStatements = [Select Name From HealthCloudGA__EhrMedicationStatement__c Where HealthCloudGA__Account__c =: CaseRecord.AccountId];
}
// Method to create map of Data of different tables
static Map<Id,List<sObject>> parentmap(String object_name){
String FieldSet = '';
if(object_name == 'HealthCloudGA__CarePlanProblem__c')
FieldSet = ' id,Name , HealthCloudGA__CarePlan__c ';
if(object_name == 'HealthCloudGA__CarePlanGoal__c')
FieldSet = ' Id, Name, HealthCloudGA__CarePlanProblem__c ';
if(object_name == 'Task')
FieldSet = ' Id, Subject, HealthCloudGA__CarePlanGoal__c ';
List<sObject> c2 = Database.query('select '+FieldSet +' from ' + object_name );
Map<Id,List<sObject>> m2 = new Map<Id,List<sObject>>();
String parentid = '';
for (sObject o2:c2) {
parentid = null;
if (object_name == 'HealthCloudGA__CarePlanProblem__c') {
parentid = ((HealthCloudGA__CarePlanProblem__c) o2).HealthCloudGA__CarePlan__c;
}
if (object_name == 'HealthCloudGA__CarePlanGoal__c') {
parentid = ((HealthCloudGA__CarePlanGoal__c) o2).HealthCloudGA__CarePlanProblem__c;
}
if (object_name == 'Task') {
parentid = ((Task) o2).HealthCloudGA__CarePlanGoal__c ;
}
if (parentid == null) continue;
if (m2.containsKey(parentid)) {
((List<sObject>) m2.get(parentid)).add(o2);
} else {
m2.put(parentid, new List<sObject>{o2});
}
}
return m2;
}
// Method to build pageblock Tables
public Component.Apex.PageBlockTable getHierarchy() {
Map<Id,Case> c1 = new Map<Id,Case>([select id, CaseNumber from Case Where Id = : caseId ]);
allvalues = new Map<String,Map<Id,List<sObject>>>();
allvalues.put('HealthCloudGA__CarePlanProblem__c',parentmap('HealthCloudGA__CarePlanProblem__c'));
allvalues.put('HealthCloudGA__CarePlanGoal__c',parentmap('HealthCloudGA__CarePlanGoal__c'));
allvalues.put('Task',parentmap('Task'));
items = new List<String>();
Component.Apex.PageBlockTable myTable = new Component.Apex.PageBlockTable(var='item');
myTable.expressions.value = '{!items}';
Component.Apex.column clm = new Component.Apex.column(headerValue= 'Name');
myTable.childComponents.add( clm );
Component.Apex.OutputText outText = new Component.Apex.OutputText( );
outText.expressions.value = '{!item}';
outText.escape = false;
clm.childComponents.add( outText );
for (Id id1 :c1.keyset()) {
Case o1 = (Case) c1.get(id1);
items.add(o1.CaseNumber);
getNodes('HealthCloudGA__CarePlanProblem__c',o1.id);
}
return myTable;
}
// Method to add child data.
public void getNodes(String object_name,String parentid) {
String name = '';
String id = '';
Map<Id,List<sObject>> map1 = (Map<Id,List<sObject>>) allvalues.get(object_name);
if (object_name == 'HealthCloudGA__CarePlanProblem__c') {
if (map1.containsKey(parentid)) {
List<HealthCloudGA__CarePlanProblem__c> lcob = (List<HealthCloudGA__CarePlanProblem__c>) map1.get(parentid);
for (HealthCloudGA__CarePlanProblem__c cob:lcob) {
name = cob.name;
items.add(' ' + name);
getNodes('HealthCloudGA__CarePlanGoal__c',cob.id);
}
}
} else if (object_name == 'HealthCloudGA__CarePlanGoal__c') {
if (map1.containsKey(parentid)) {
List<HealthCloudGA__CarePlanGoal__c> lcob = (List<HealthCloudGA__CarePlanGoal__c>) map1.get(parentid);
for (HealthCloudGA__CarePlanGoal__c cob:lcob) {
name = cob.name;
items.add(' ' + name);
getNodes('Task',cob.id);
}
}
} else if (object_name == 'Task') {
if (map1.containsKey(parentid)) {
List<Task> lcob = (List<Task>) map1.get(parentid);
for (Task cob:lcob) {
name = cob.Subject;
items.add(' '+name);
}} } }}
All Answers
Can you suggest a few changes