-
ChatterFeed
-
0Best Answers
-
1Likes Received
-
0Likes Given
-
3Questions
-
2Replies
Apex Unit Tests - Increasing the Code Coverage Percentage
Hi,
I 'inherited' the maintenance of some Apex and Visualforce code that I want to make changes to and "push" using a Change Set from Sandbox to Sandbox and (ultimately) Production. There are Unit Tests associated with the Apex code (which I believe need to be run at a minimum of 75% in order to be able to be deployed to Production) that are currently achieving 74%.
I have not (yet) figured out how to increase the code coverage percentage above 74%.
I have attached the source code for the Apex Class and Apex Unit Test for the class.
And the Apex Test Class
Many Thanks!
I 'inherited' the maintenance of some Apex and Visualforce code that I want to make changes to and "push" using a Change Set from Sandbox to Sandbox and (ultimately) Production. There are Unit Tests associated with the Apex code (which I believe need to be run at a minimum of 75% in order to be able to be deployed to Production) that are currently achieving 74%.
I have not (yet) figured out how to increase the code coverage percentage above 74%.
I have attached the source code for the Apex Class and Apex Unit Test for the class.
public with sharing class AttendanceSelectController { // ApexPages.StandardSetController must be instantiated public Date dateToInput{ get; set; } public Id selectedProjectCentreId{get;set;} public List<Attendance__c> attendanceList; public AttendanceSelectController() { dateToInput=Date.Today(); } public List<SelectOption> getProjectCentreList() { List<Project_Centre__c> actList=[select Name,Id from Project_Centre__c]; List<SelectOption> options = new List<SelectOption>(); for (Project_Centre__c pc:actList) { options.add(new SelectOption(pc.Id,pc.Name)); } return options; } public List<SelectOption> getBehaviourItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('1','1')); options.add(new SelectOption('2','2')); options.add(new SelectOption('3','3')); options.add(new SelectOption('4','4')); options.add(new SelectOption('5','5')); return options; } public Pagereference next() { return Page.AttendanceInputPage; } public Pagereference Save() { // get rid of the behaviour score if they did not attend for (Attendance__c att : attendanceList) { if( !att.Present__c ){ att.Behaviour_Score__c = 0; } } upsert attendanceList; PageReference secondPage = Page.DMLThankYou; secondPage.setRedirect(true); return secondPage; } public Pagereference Back() { PageReference secondPage = Page.AttendanceSelectPage; secondPage.setRedirect(true); return secondPage; } public List<Attendance__c> getAttendanceList() { attendanceList = new List<Attendance__c>(); //get all the attendances for this account and this date and add them to our list List<Attendance__c> existingAttendances = [Select Id,Contact__c,Behaviour_Score__c,Date__c,Present__c,Note__c from Attendance__c WHERE Date__c = :dateToInput And Project_Centre__r.Id = :selectedProjectCentreId ]; List<ID> contactIdsDoneAlready = new List<ID>(); for (Attendance__c existingAtt : existingAttendances) { attendanceList.add(existingAtt); contactIdsDoneAlready.add( existingAtt.Contact__c ); } //get all the contacts that dont have an attendance for this date and then give them one List<Contact> contactList = [SELECT Name, ID FROM Contact Where Project_Centre__c = :selectedProjectCentreId AND Id not in :contactIdsDoneAlready and status__c = 'Active' order by Name]; for (Contact contact : contactList) { Attendance__c attendance = new Attendance__c(); attendance.Contact__c = contact.Id; attendance.Behaviour_Score__c = 5; attendance.Present__c = true; //attendance.Note__c =; attendance.Date__c = dateToInput; attendance.Project_Centre__c = selectedProjectCentreId; attendanceList.add(attendance); } return attendanceList ; } }
And the Apex Test Class
@isTest private class AttendanceControllerTestClass { static testMethod void testConstructor() { AttendanceSelectController controller = new AttendanceSelectController(); Project_Centre__c testAccount = new Project_Centre__c(); testAccount.Name = 'test Name'; insert testAccount; Contact testContact = new Contact(); RecordType testRecordType = [Select Id From RecordType where sobjecttype = 'Contact' and name = 'Contact - Service User']; testContact.RecordType = testRecordType; testContact.Project_Centre__r = testAccount; testContact.LastName = 'LastName'; insert testContact; controller.selectedProjectCentreId = testAccount.Id; controller.getProjectCentreList(); controller.next(); controller.back(); controller.getBehaviourItems(); List<Attendance__c> theList = controller.getAttendanceList(); controller.save(); controller.getAttendanceList(); controller.save(); } }Really would appreciate any help on this!
Many Thanks!
-
- Paul Fitzpatrick 19
- June 07, 2016
- Like
- 0
- Continue reading or reply
Apex Trigger to auto-populate a Lookup Field depending on the Logged-in User's Role
Hi,
I am trying to figure out a way to auto-populate a lookup field with a value on initial entry(clicking New Contact) into the Contact screen.
The lookup field is a lookup to a custom object called Project Centre ( API Name is Project_Centre__c ). The Project Centre can take on up to 5 different record values (won't list all 5, such as "Aisling Shangan" or "Aisling Woodhazel")
I wish to set the value in the Project Centre lookup field depending on the currently logged in User's Role. So for example, if the current logged-in User's Role is "Aisling Shangan Staff" then I wish the Project Centre lookup value on the Contact object to be set to the value of "Aisling Shangan".
I understand that writing an Apex trigger may be the only way of achieving this. I haven't written a trigger yet, so any help would be much appreciated!
I am trying to figure out a way to auto-populate a lookup field with a value on initial entry(clicking New Contact) into the Contact screen.
The lookup field is a lookup to a custom object called Project Centre ( API Name is Project_Centre__c ). The Project Centre can take on up to 5 different record values (won't list all 5, such as "Aisling Shangan" or "Aisling Woodhazel")
I wish to set the value in the Project Centre lookup field depending on the currently logged in User's Role. So for example, if the current logged-in User's Role is "Aisling Shangan Staff" then I wish the Project Centre lookup value on the Contact object to be set to the value of "Aisling Shangan".
I understand that writing an Apex trigger may be the only way of achieving this. I haven't written a trigger yet, so any help would be much appreciated!
-
- Paul Fitzpatrick 19
- June 03, 2016
- Like
- 0
- Continue reading or reply
Pitfalls of pushing Apex and Visualforce in Change Sets from Sandbox to Production
I have inherited the maintenance of some Visualforce and Apex code. I have downloaded the
Eclipse project, and am using the Force.com IDE. I created two Developer
Sandboxes (recently refreshed from Production), and want to "push" minor
changes between these two Sandboxes to make sure everything is Ok. I have
made Visualforce and Apex changes in one Sandbox and was able to deploy and
"see" the changes locally in that Sandbox, but I don't yet fully understand the
pitfalls of pushing "Apex and Visualforce" in Change Sets from Sandbox to
Sandbox(or ultimately Sandbox to "Production"). Can you please point me to relevant documentation! Thanks!
Eclipse project, and am using the Force.com IDE. I created two Developer
Sandboxes (recently refreshed from Production), and want to "push" minor
changes between these two Sandboxes to make sure everything is Ok. I have
made Visualforce and Apex changes in one Sandbox and was able to deploy and
"see" the changes locally in that Sandbox, but I don't yet fully understand the
pitfalls of pushing "Apex and Visualforce" in Change Sets from Sandbox to
Sandbox(or ultimately Sandbox to "Production"). Can you please point me to relevant documentation! Thanks!
-
- Paul Fitzpatrick 19
- June 02, 2016
- Like
- 1
- Continue reading or reply
Pitfalls of pushing Apex and Visualforce in Change Sets from Sandbox to Production
I have inherited the maintenance of some Visualforce and Apex code. I have downloaded the
Eclipse project, and am using the Force.com IDE. I created two Developer
Sandboxes (recently refreshed from Production), and want to "push" minor
changes between these two Sandboxes to make sure everything is Ok. I have
made Visualforce and Apex changes in one Sandbox and was able to deploy and
"see" the changes locally in that Sandbox, but I don't yet fully understand the
pitfalls of pushing "Apex and Visualforce" in Change Sets from Sandbox to
Sandbox(or ultimately Sandbox to "Production"). Can you please point me to relevant documentation! Thanks!
Eclipse project, and am using the Force.com IDE. I created two Developer
Sandboxes (recently refreshed from Production), and want to "push" minor
changes between these two Sandboxes to make sure everything is Ok. I have
made Visualforce and Apex changes in one Sandbox and was able to deploy and
"see" the changes locally in that Sandbox, but I don't yet fully understand the
pitfalls of pushing "Apex and Visualforce" in Change Sets from Sandbox to
Sandbox(or ultimately Sandbox to "Production"). Can you please point me to relevant documentation! Thanks!
-
- Paul Fitzpatrick 19
- June 02, 2016
- Like
- 1
- Continue reading or reply
Apex Trigger to auto-populate a Lookup Field depending on the Logged-in User's Role
Hi,
I am trying to figure out a way to auto-populate a lookup field with a value on initial entry(clicking New Contact) into the Contact screen.
The lookup field is a lookup to a custom object called Project Centre ( API Name is Project_Centre__c ). The Project Centre can take on up to 5 different record values (won't list all 5, such as "Aisling Shangan" or "Aisling Woodhazel")
I wish to set the value in the Project Centre lookup field depending on the currently logged in User's Role. So for example, if the current logged-in User's Role is "Aisling Shangan Staff" then I wish the Project Centre lookup value on the Contact object to be set to the value of "Aisling Shangan".
I understand that writing an Apex trigger may be the only way of achieving this. I haven't written a trigger yet, so any help would be much appreciated!
I am trying to figure out a way to auto-populate a lookup field with a value on initial entry(clicking New Contact) into the Contact screen.
The lookup field is a lookup to a custom object called Project Centre ( API Name is Project_Centre__c ). The Project Centre can take on up to 5 different record values (won't list all 5, such as "Aisling Shangan" or "Aisling Woodhazel")
I wish to set the value in the Project Centre lookup field depending on the currently logged in User's Role. So for example, if the current logged-in User's Role is "Aisling Shangan Staff" then I wish the Project Centre lookup value on the Contact object to be set to the value of "Aisling Shangan".
I understand that writing an Apex trigger may be the only way of achieving this. I haven't written a trigger yet, so any help would be much appreciated!
- Paul Fitzpatrick 19
- June 03, 2016
- Like
- 0
- Continue reading or reply
Pitfalls of pushing Apex and Visualforce in Change Sets from Sandbox to Production
I have inherited the maintenance of some Visualforce and Apex code. I have downloaded the
Eclipse project, and am using the Force.com IDE. I created two Developer
Sandboxes (recently refreshed from Production), and want to "push" minor
changes between these two Sandboxes to make sure everything is Ok. I have
made Visualforce and Apex changes in one Sandbox and was able to deploy and
"see" the changes locally in that Sandbox, but I don't yet fully understand the
pitfalls of pushing "Apex and Visualforce" in Change Sets from Sandbox to
Sandbox(or ultimately Sandbox to "Production"). Can you please point me to relevant documentation! Thanks!
Eclipse project, and am using the Force.com IDE. I created two Developer
Sandboxes (recently refreshed from Production), and want to "push" minor
changes between these two Sandboxes to make sure everything is Ok. I have
made Visualforce and Apex changes in one Sandbox and was able to deploy and
"see" the changes locally in that Sandbox, but I don't yet fully understand the
pitfalls of pushing "Apex and Visualforce" in Change Sets from Sandbox to
Sandbox(or ultimately Sandbox to "Production"). Can you please point me to relevant documentation! Thanks!
- Paul Fitzpatrick 19
- June 02, 2016
- Like
- 1
- Continue reading or reply