You need to sign in to do that
Don't have an account?
Rahul Mehata
I want to increase code coverage for below class
I am not able to cover the below lines in test class. please help.
Contact con = new contact(); con.id= rel.From_Individual__c; con.firstname= rel.From_Individual__r.firstname; con.lastname= rel.From_Individual__r.lastname; con.email= rel.From_Individual__r.email; conListForAdvisor.add(con); } if(conListForAdvisor.size()>0){ update relationListForAdvisor; update conListForAdvisor; } for(Event ev : newEvents){ System.debug(ev); if(ev.eventName != null && ev.eventName != ''){ cEvent = new Event__c(); String parishRegion = getCRSRegion(University); cEvent.Name = ev.eventName; cEvent.Status__c = 'Completed'; cEvent.Description__c = ev.eventDescription; if(ev.eventCheckAllThatApply != null) { cEvent.Check_all_that_apply_to_your_campus__c = String.valueOf(ev.eventCheckAllThatApply).remove('(').remove(')').replace(',',';'); } if(ev.eventNuberAttended != null && ev.eventNuberAttended != ''){ cEvent.Number_Attended__c = Decimal.ValueOf(ev.eventNuberAttended); } if(ev.eventLetters != null && ev.eventLetters != ''){ cEvent.If_you_hand_wrote_letters_please_list__c = Decimal.ValueOf(ev.eventLetters); /*if(Decimal.ValueOf(ev.eventLetters) > 0 ){ cEvent.Action__c = 'EN - Directing a LETTER WRITING campaign'; }*/ } String evSelectedOptions = ''; for(String s: ev.eventCheckAllThatApply) { evSelectedOptions += s + ' '; } //evSelectedOptions.addAll(ev.eventCheckAllThatApply); System.debug('>>>evSelectedOptions: ' + evSelectedOptions); if(evSelectedOptions.contains('Chapter meetings')) { cEvent.Action__c = 'ED - Coordinating a MEETING'; } else if(evSelectedOptions.contains('Legislative Visit')) { cEvent.Action__c = 'EN - Facilitating a CONGRESSIONAL VISIT'; } else { cEvent.Action__c = 'ED - Providing general OUTREACH'; } cEvent.Category__c = catagory; String campaignName = ''; // hold the program initiative to attach the event to cEvent.Parish_or_School__c = University; cEvent.Primary_Institution__c = University; cEvent.CRS_Region__c = 'Replicator; ' + parishRegion; if(ev.eventDate != null && ev.eventDate !=''){ cEvent.Start_Date__c = date.ValueOf(ev.eventDate); cEvent.End_Date__c = date.ValueOf(ev.eventDate); cEvent.Event_Date_Time__c = cEvent.End_Date__c; } cEvent.Association_if_other__c = ''; // reset after the field has been used. lstEvents.add(cEvent); } } if(lstEvents.size() > 0){ insert lstEvents; for(Event__c events : lstEvents){ eventIds.add(events.Id); } eventId = lstEvents[0].Id; }
global with sharing class ae_UniversitySurveyCtlr{
public static final String catagory = 'SO1 - Shaping Institutions';
public Event__c cEvent {get;set;}
// public String zipcode{get;set;}
// public String selectedItem {get;set;}
public boolean EventSpecific {get;set;}
//public String parisName {get;set;}
public String selectedparisName {get;set;}
//public static String selectedCon {get;set;}
//public static String selectedSFId {get; set;}
//public boolean isLastNameNull {get;set;}
public String eventId {get;set;}
public String states{get;set;}
//public String EventName {get;set;}
public String selectedEventName{get;set;}
//public String selectedContactId {get;set;}
string accId;
public Account acc {get; set;}
public Relationship__c rel {get; set;}
//public Date eventEndDate{get;set;}
// public Date endDate {get; set;}
//public String selectedConForFaculty{get;set;}
public String newInds {get; set;}
public List<SelectOption> events {get; set;}
public String newIndsForAdvisor{get; set;}
public String saveCBError {get; set;}
// public String ceName{get;set;}
// public String EndDateUpdateIndex{get;set;}
//public String EndDateToUpdate{get;set;}
public List<Contact> lstIndividual {get; set;}
public List<Contact>lstIndividualForAdvisor{get; set;}
public String University{get; set;}
public String selectedState{get; set;}
public List<Relationship__c> relationListForAmbassador{get;set;}
public List<Relationship__c> relationListForAdvisor{get;set;}
public List<Event__c> eventsList{get;set;}
public List<Social_Media__c> socialMediasList{get;set;}
public String selectedNewInstitution {get;set;}
public String BillCity {get;set;}
public String InstitutionSubSubType {get;set;}
public String street {get;set;}
public String state {get;set;}
public String zip {get;set;}
public List<String> eventIds {get;set;}
public Map<String,Contact> mapContacts;
public String NewmanCenter{get;set;}
public Map<Id,Id> mapContactToRelationship;
public string newUniversity{get;set;}
public string SocialMediaForfb{get;set;}
public string ProfileNameForfb{get;set;}
public string yesNoValueForfb{get;set;}
public string NoOfFollowersForfb{get;set;}
public string SocialMediaForTweet{get;set;}
public string ProfileNameForTweet{get;set;}
public string yesNoValueForTweet{get;set;}
public string NoOfFollowersForTweet{get;set;}
public string SocialMediaForInstagram{get;set;}
public string ProfileNameForInstagram{get;set;}
public string NoOfFollowersForInstagram{get;set;}
public string SocialMediaForSnapchat{get;set;}
public string ProfileNameForSnapchat{get;set;}
public string NoOfFollowersForSnapchat{get;set;}
public string SocialMediaForOther{get;set;}
public string ProfileNameForOther{get;set;}
public string yesNoValueForOther{get;set;}
public string NewtxtSocialMediaNameForother{get;set;}
public string NoOfFollowersForOther{get;set;}
public string EmailList{get;set;}
public string officeDepartment{get;set;}
public string otherOffice{get;set;}
public List<SelectOption> options {get;set;}
public List<SelectOption> SelectedEventOptions{get;set;}
public String checkAmbassadorIsChecked{get;set;}
public String Comment{get;set;}
Relationship_Type__c relationType = [select id,Name from Relationship_Type__c where Name = 'Student Ambassador'];
Relationship_Type__c relationTypeForStaff = [select id,Name from Relationship_Type__c where Name = 'Staff'];
public ae_UniversitySurveyCtlr(){}
public ae_UniversitySurveyCtlr(ApexPages.StandardController controller){
eventIds = new List<String>();
cEvent = new Event__c();
rel = new Relationship__c();
acc = new Account();
//selectedSFId = '';
states = 'AK';
EventSpecific = false;
//isLastNameNull = false;
mapContacts = new Map<String,Contact>();
mapContactToRelationship = new Map<Id,Id>();
newUniversity = '';
/**
* Get all contacts and store in map to check FirstName and LastName.
*/
/*for(Contact cn : [Select Id,FirstName,LastName,AccountId,(Select Id,From_Individual__c,First_Relationship_Type__r.Name from Relationships__r) from Contact]){
String contactName = '';
if(cn.FirstName != null){
contactName = cn.FirstName;
}
if(cn.LastName != null){
contactName = contactName+''+cn.LastName;
}
contactName = contactName.toLowerCase();
mapContacts.put(contactName,cn);
/**
* Check if contact have 'Student Ambassador' Relationship.
*/
/*for(Relationship__c reln : cn.Relationships__r){
if(reln.First_Relationship_Type__r.Name == 'Student Ambassador'){
mapContactToRelationship.put(cn.Id,reln.Id);
break;
}
}
}*/
selectedUniversity();
getuniversityitems();
initRT();
}
public void initRT() {
}
/*public void eventAction(){
}*/
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult = Additional_Institution_Address__c.State__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry f : ple){
options.add(new SelectOption(f.getLabel(),f.getValue()));
}
return options;
}
public List<SelectOption> getNewmanCenterOptions() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('No','No'));
options.add(new SelectOption('Yes','Yes'));
return options;
}
public List<SelectOption> getEventsOptions() {
List<SelectOption> Options = new List<SelectOption>();
Options.add(new SelectOption('Rice bowl','Rice bowl'));
Options.add(new SelectOption('Fair Trade','Fair Trade'));
Options.add(new SelectOption('Helping Hands','Helping Hands'));
Options.add(new SelectOption('I am Migration','I am Migration'));
Options.add(new SelectOption('I am Trafficking','I am Trafficking'));
Options.add(new SelectOption('I am Climate Change','I am Climate Change'));
Options.add(new SelectOption('Legislative Visit','Legislative Visit'));
Options.add(new SelectOption('Advocacy','Advocacy'));
Options.add(new SelectOption('Social media','Social media'));
Options.add(new SelectOption('Chapter meetings','Chapter meetings'));
return Options;
}
/**
* Reorganize the code to create the record first and then send email, it should cover both actions for Fair Trade.
*/
public PageReference save(){
saveCBError = '';
SavePoint sp = Database.setSavepoint();
List<Event__c> lstEvents = new List<Event__c>();
List<Contact> conList = new List<contact>();
List<Contact> conListForAdvisor = new List<contact>();
List<Event> newEvents = new List<Event>();
if(selectedEventName != null && selectedEventName != '') {
newEvents = (List<Event>)JSON.deserialize(selectedEventName, List<Event>.class);
system.debug('selectedEventName55555'+newEvents);
system.debug('selectedEventName66666'+selectedEventName);
}
//parse Ambassador For update
List<Ambassador> UpdateAmbassador = new List<Ambassador>();
if(checkAmbassadorIsChecked != null && checkAmbassadorIsChecked != ''){
UpdateAmbassador = (List<Ambassador>)JSON.deserialize(checkAmbassadorIsChecked, List<Ambassador>.class);
system.debug('Records To update'+UpdateAmbassador);
}
List<Relationship__c> relationToUpdate = new List<Relationship__c>();
for(Ambassador amb : UpdateAmbassador){
Relationship__c rels = new Relationship__c();
rels.Id = amb.AmbassadorId;
if(amb.checkVal=='false'){
rels.End_Date__c = date.today();
relationToUpdate.add(rels);
}
}
if(relationToUpdate.size()>0){
update relationToUpdate;
}
/**
* parse Individuals posted
*/
List<Individual> newIndividuals = new List<Individual>();
set<String> fullNameSet = new set<String>();
if(newInds != null && newInds != '') {
newIndividuals = (List<Individual>)JSON.deserialize(newInds, List<Individual>.class);
}
system.debug('@@@@@newIndividuals'+newIndividuals);
/**
* Get all contacts and store in map to check FirstName and LastName.
*/
for(Individual ind : newIndividuals){
if(ind.lname==null) continue;
String fullName = ind.fname +' '+ ind.lname;
fullNameSet.add(fullName);
}
system.debug('FullNameSet'+fullNameSet);
for(Contact cn : [Select Id,FirstName,LastName,AccountId,(Select Id,From_Individual__c,First_Relationship_Type__r.Name from Relationships__r) from Contact where Name in : fullNameSet]){
String contactName = '';
if(cn.FirstName != null){
contactName = cn.FirstName;
}
if(cn.LastName != null){
contactName = contactName+''+cn.LastName;
}
contactName = contactName.toLowerCase();
mapContacts.put(contactName,cn);
/**
* Check if contact have 'Student Ambassador' Relationship.
*/
for(Relationship__c reln : cn.Relationships__r){
if(reln.First_Relationship_Type__r.Name == 'Student Ambassador'){
mapContactToRelationship.put(cn.Id,reln.Id);
break;
}
}
}
if(selectedNewInstitution != '' && selectedNewInstitution != null){
if(NewmanCenter == 'Yes'){
acc.Institution_Sub_sub_Type__c ='Newman Center';
acc.Institutional_Affiliation__c = '';
}
else{
acc.Institution_Sub_sub_Type__c ='Non-Applicable';
acc.Institutional_Affiliation__c ='Catholic';
}
acc.Institution_type__c ='Non-Profit Institution';
acc.Institution_sub_type__c =' College/University (Non-Profit)';
acc.Name= selectedNewInstitution;
acc.BillingCity = BillCity;
// acc.Institution_Sub_sub_Type__c = InstitutionSubSubType;
acc.BillingState = state;
acc.BillingStreet = street;
acc.BillingPostalCode = Zip;
if(EmailList !=null && EmailList != ''){
acc.Email_list_how_many__c = Decimal.ValueOf(EmailList);
}
if(officeDepartment !='Other'){
acc.What_office_department_supports_or_spons__c = officeDepartment;
}
else{
acc.What_office_department_supports_or_spons__c = otherOffice;
}
/**
* check if the institution already exists based on name, state and zipcode
*/
List<Account> lstAcc = [Select id from Account where billingstate=:state and name=:acc.Name and billingpostalcode=:zip];
if(lstAcc.size() > 0) {
saveCBError = Label.UniversitySurvey_Duplicate_Institution;
Database.rollback(sp);
return null;
}
insert acc;
University = acc.Id;
Additional_Institution_Address__c prefferredAdd = new Additional_Institution_Address__c();
prefferredAdd.Institution_Name__c = acc.Id;
prefferredAdd.City__c = BillCity;
prefferredAdd.Street__c = street;
prefferredAdd.State__c = state;
prefferredAdd.Zip_Postal_Code__c = Zip;
prefferredAdd.Preferred__c = true;
insert prefferredAdd;
}
saveCBError = '';
SavePoint sp = Database.setSavepoint();
List<Event__c> lstEvents = new List<Event__c>();
List<Contact> conList = new List<contact>();
List<Contact> conListForAdvisor = new List<contact>();
List<Event> newEvents = new List<Event>();
if(selectedEventName != null && selectedEventName != '') {
newEvents = (List<Event>)JSON.deserialize(selectedEventName, List<Event>.class);
system.debug('selectedEventName55555'+newEvents);
system.debug('selectedEventName66666'+selectedEventName);
}
//parse Ambassador For update
List<Ambassador> UpdateAmbassador = new List<Ambassador>();
if(checkAmbassadorIsChecked != null && checkAmbassadorIsChecked != ''){
UpdateAmbassador = (List<Ambassador>)JSON.deserialize(checkAmbassadorIsChecked, List<Ambassador>.class);
system.debug('Records To update'+UpdateAmbassador);
}
List<Relationship__c> relationToUpdate = new List<Relationship__c>();
for(Ambassador amb : UpdateAmbassador){
Relationship__c rels = new Relationship__c();
rels.Id = amb.AmbassadorId;
if(amb.checkVal=='false'){
rels.End_Date__c = date.today();
relationToUpdate.add(rels);
}
}
if(relationToUpdate.size()>0){
update relationToUpdate;
}
/**
* parse Individuals posted
*/
List<Individual> newIndividuals = new List<Individual>();
set<String> fullNameSet = new set<String>();
if(newInds != null && newInds != '') {
newIndividuals = (List<Individual>)JSON.deserialize(newInds, List<Individual>.class);
}
system.debug('@@@@@newIndividuals'+newIndividuals);
/**
* Get all contacts and store in map to check FirstName and LastName.
*/
for(Individual ind : newIndividuals){
if(ind.lname==null) continue;
String fullName = ind.fname +' '+ ind.lname;
fullNameSet.add(fullName);
}
system.debug('FullNameSet'+fullNameSet);
for(Contact cn : [Select Id,FirstName,LastName,AccountId,(Select Id,From_Individual__c,First_Relationship_Type__r.Name from Relationships__r) from Contact where Name in : fullNameSet]){
String contactName = '';
if(cn.FirstName != null){
contactName = cn.FirstName;
}
if(cn.LastName != null){
contactName = contactName+''+cn.LastName;
}
contactName = contactName.toLowerCase();
mapContacts.put(contactName,cn);
/**
* Check if contact have 'Student Ambassador' Relationship.
*/
for(Relationship__c reln : cn.Relationships__r){
if(reln.First_Relationship_Type__r.Name == 'Student Ambassador'){
mapContactToRelationship.put(cn.Id,reln.Id);
break;
}
}
}
if(selectedNewInstitution != '' && selectedNewInstitution != null){
if(NewmanCenter == 'Yes'){
acc.Institution_Sub_sub_Type__c ='Newman Center';
acc.Institutional_Affiliation__c = '';
}
else{
acc.Institution_Sub_sub_Type__c ='Non-Applicable';
acc.Institutional_Affiliation__c ='Catholic';
}
acc.Institution_type__c ='Non-Profit Institution';
acc.Institution_sub_type__c =' College/University (Non-Profit)';
acc.Name= selectedNewInstitution;
acc.BillingCity = BillCity;
// acc.Institution_Sub_sub_Type__c = InstitutionSubSubType;
acc.BillingState = state;
acc.BillingStreet = street;
acc.BillingPostalCode = Zip;
if(EmailList !=null && EmailList != ''){
acc.Email_list_how_many__c = Decimal.ValueOf(EmailList);
}
if(officeDepartment !='Other'){
acc.What_office_department_supports_or_spons__c = officeDepartment;
}
else{
acc.What_office_department_supports_or_spons__c = otherOffice;
}
/**
* check if the institution already exists based on name, state and zipcode
*/
List<Account> lstAcc = [Select id from Account where billingstate=:state and name=:acc.Name and billingpostalcode=:zip];
if(lstAcc.size() > 0) {
saveCBError = Label.UniversitySurvey_Duplicate_Institution;
Database.rollback(sp);
return null;
}
insert acc;
University = acc.Id;
Additional_Institution_Address__c prefferredAdd = new Additional_Institution_Address__c();
prefferredAdd.Institution_Name__c = acc.Id;
prefferredAdd.City__c = BillCity;
prefferredAdd.Street__c = street;
prefferredAdd.State__c = state;
prefferredAdd.Zip_Postal_Code__c = Zip;
prefferredAdd.Preferred__c = true;
insert prefferredAdd;
}
else {
Account a = new Account();
a.Id = University;
if(officeDepartment !='Other'&&(officeDepartment != '' && officeDepartment != null)){
a.What_office_department_supports_or_spons__c = officeDepartment;
}
else if(otherOffice != '' && otherOffice != null){
a.What_office_department_supports_or_spons__c = otherOffice;
}
update a;
}
// query lstIndividual
List<Event__c> listOfEvenToUpdate = new List<Event__c>();
try{
if(eventsList.size()>0){
System.debug('>>>' + eventsList);
update eventsList;
}
if(socialMediasList.size()>0){
update socialMediasList;
}
if(eventsList.size()>0){
eventId = eventsList[0].id;
}
for(Relationship__c rel: relationListForAmbassador){
Contact con = new contact();
con.id= rel.From_Individual__c;
con.firstname= rel.From_Individual__r.firstname;
con.lastname= rel.From_Individual__r.lastname;
con.email= rel.From_Individual__r.email;
conList.add(con);
}
if(conList.size()>0){
system.debug('>>>>>>>>>>>'+ conList);
update conList;
//update relationListForAmbassador;
}
for(Relationship__c rel: relationListForAdvisor){
Contact con = new contact();
con.id= rel.From_Individual__c;
con.firstname= rel.From_Individual__r.firstname;
con.lastname= rel.From_Individual__r.lastname;
con.email= rel.From_Individual__r.email;
conListForAdvisor.add(con);
}
if(conListForAdvisor.size()>0){
update relationListForAdvisor;
update conListForAdvisor;
}
for(Event ev : newEvents){
System.debug(ev);
if(ev.eventName != null && ev.eventName != ''){
cEvent = new Event__c();
String parishRegion = getCRSRegion(University);
cEvent.Name = ev.eventName;
cEvent.Status__c = 'Completed';
cEvent.Description__c = ev.eventDescription;
if(ev.eventCheckAllThatApply != null) {
cEvent.Check_all_that_apply_to_your_campus__c =
String.valueOf(ev.eventCheckAllThatApply).remove('(').remove(')').replace(',',';');
}
if(ev.eventNuberAttended != null && ev.eventNuberAttended != ''){
cEvent.Number_Attended__c = Decimal.ValueOf(ev.eventNuberAttended);
}
if(ev.eventLetters != null && ev.eventLetters != ''){
cEvent.If_you_hand_wrote_letters_please_list__c = Decimal.ValueOf(ev.eventLetters);
/*if(Decimal.ValueOf(ev.eventLetters) > 0 ){
cEvent.Action__c = 'EN - Directing a LETTER WRITING campaign';
}*/
}
String evSelectedOptions = '';
for(String s: ev.eventCheckAllThatApply) {
evSelectedOptions += s + ' ';
}
//evSelectedOptions.addAll(ev.eventCheckAllThatApply);
System.debug('>>>evSelectedOptions: ' + evSelectedOptions);
if(evSelectedOptions.contains('Chapter meetings')) {
cEvent.Action__c = 'ED - Coordinating a MEETING';
}
else if(evSelectedOptions.contains('Legislative Visit')) {
cEvent.Action__c = 'EN - Facilitating a CONGRESSIONAL VISIT';
}
else {
cEvent.Action__c = 'ED - Providing general OUTREACH';
}
cEvent.Category__c = catagory;
String campaignName = ''; // hold the program initiative to attach the event to
cEvent.Parish_or_School__c = University;
cEvent.Primary_Institution__c = University;
cEvent.CRS_Region__c = 'Replicator; ' + parishRegion;
if(ev.eventDate != null && ev.eventDate !=''){
cEvent.Start_Date__c = date.ValueOf(ev.eventDate);
cEvent.End_Date__c = date.ValueOf(ev.eventDate);
cEvent.Event_Date_Time__c = cEvent.End_Date__c;
}
cEvent.Association_if_other__c = ''; // reset after the field has been used.
lstEvents.add(cEvent);
}
}
if(lstEvents.size() > 0){
insert lstEvents;
for(Event__c events : lstEvents){
eventIds.add(events.Id);
}
eventId = lstEvents[0].Id;
}
public class ae_UniversitySurveyCtlrTest{
static testMethod void test_ae_UniversitySurvey(){
List<Account> testAccount = new List<Account>();
Account testAccount1 = new Account();
testAccount1.Name = 'Old College';
testAccount1.BillingStreet='test';
testAccount1.BillingPostalCode='543';
testAccount1.BillingCity = 'Boston';
testAccount1.BillingState = 'AK';
testAccount1.Institution_Type__c='Non-Profit Institution';
testAccount1.Institution_Sub_Type__c='College/University (Non-Profit)';
testAccount1.Institution_Sub_Sub_Type__c='Newman Center';
testAccount.add(testAccount1);
Account testAcc = new Account();
testAcc.Name = 'Old College';
testAcc.BillingStreet='test';
testAcc.BillingPostalCode='543';
testAcc.BillingState = 'AK';
testAcc.Institution_Type__c='Non-Profit Institution';
testAcc.Institution_Sub_Type__c='College/University (Non-Profit)';
testAcc.Institution_Sub_Sub_Type__c='Newman Center';
testAccount.add(testAcc);
insert testAccount;
insert this //// From_Individual__c /// then
List<Contact> lstcon = new List<Contact>();
Contact c = new Contact(AccountId = testAccount1.id,FirstName = From_Individual__c of first name , LastName = From_Individual__c of last name email= From_Individual__c of Email);
lstcon.add(c);
Contact c1 = new Contact(AccountId = testAcc.id,FirstName = From_Individual__c of first name , LastName = From_Individual__c of last name email= From_Individual__c of Email);
lstcon.add(c1);
insert lstcon;
go on you forget to insert From_Individual__c insert this then it will working
try it..
}
https://www.buydocument.net