Background: New users need to complete a series of training exercises. We have been manually validating that these are completed. I'm now working on a 'validator' visualforce page that will check if these were completed.
Question: One thing that I want is to track the total number of exercises vs the total completed. I've got something that appears to work, but not sure if it will always work, b/c in this SE post, I learned that the order of getters is not predictable.
My controller has a separate validation method for each exercise.
My approach to tracking the totals is to have 2 class variables: completed, and attempted. Within each validation method I set attempted +=1, and if completed successfully, also set completed +=1.
I have getters for getCompletedStatus and getExercisesToDo that I use in my VF validator page to display counts, render output panels, etc, and the result of these methods is dependent on the values of the attempted and completed variables.
This appears to be working properly from a few manual tests, but if the getters could fire in any order, then getCompleted and getAttempted could fire at any point, before the rest of the methods, so the numbers could be off?
If so, what are approaches that would consistently work? Do 'regular' (non-getter/setter) methods all fire before getters/setters? I that case I could use a method to validate each exercise, and a separate getter for each validation method. Is that a good pattern to use?
Controller Code (any and all suggestions on how to improve are also welcome):
public class UserOnboardingValidationExtension {
public integer Completed = 0;
public integer Attempted = 0;
public UserOnboardingValidationExtension(ApexPages.StandardController stdController) {
}
public user u = [SELECT Id,FullPhotoUrl,SmallPhotoUrl, AboutMe From USER Where Id =:UserInfo.GetuserId()];
public User getUser () {
return u;
}
public boolean getHasCreatedChatterPost() {
boolean cpPass = FALSE;
this.Attempted +=1;
list <feeditem> ChatterPosts = [SELECT Id from FeedItem Where InsertedById =: u.Id];
IF(ChatterPosts.size() >= 1){
cpPass = TRUE;
this.Completed += 1;
system.debug('Completed = ' + completed);
}
return cpPass;
}
public boolean getHasCreatedContact(){
boolean conPass = FALSE;
this.attempted += 1;
list <Contact> cList = [SELECT Name FROM Contact Where CreatedById =: u.Id];
integer conCount = clist.size();
IF (conCount >= 1){
conPass = TRUE;
this.Completed += 1;
system.debug('Completed = ' + completed);
}
return conPass;
}
public boolean getHasCreatedAccount(){
boolean accPass = FALSE;
this.attempted += 1;
list <Account> alist = [SELECT Name from Account Where CreatedById =: u.id];
integer AccountCount = alist.size();
IF (AccountCount >= 1){
accPass = TRUE;
this.Completed += 1;
system.debug('Completed = ' + completed);
}
return accPass;
}
public boolean getHasSharedContact(){
boolean sharePass = FALSE;
this.attempted += 1;
list<ContactShare> cshareList = [SELECT id, UserOrGroupId FROM ContactShare where ContactId IN (SELECT Id FROM Contact Where CreatedById =: u.Id) AND RowCause = 'Manual'];
integer ShareCount = cshareList.size();
IF(ShareCount >= 1){
sharePass = TRUE;
this.Completed += 1;
system.debug('Completed = ' + completed);
}
return sharePass;
}
public boolean getHasAddedPhoto(){
boolean photoPass = TRUE;
this.attempted += 1;
string photoUrl = u.FullPhotoUrl;
system.debug('photoURL = ' + photoUrl);
IF(photoUrl.endsWith('/profilephoto/005/F')){
photoPass = FALSE;
}
ELSE {
this.Completed += 1;
system.debug('Completed = ' + completed);
}
return photoPass;
}
public boolean getHasAddedBio(){
boolean bioPass = FALSE;
this.attempted += 1;
string aboutme = u.AboutMe;
IF(aboutme != NULL){
bioPass = TRUE;
this.Completed += 1;
system.debug('Completed = ' + completed);
}
return bioPass;
}
public boolean getHasClosedTask(){
boolean taskPass = FALSE;
this.attempted += 1;
list<Task> tasklist = new list <task>([SELECT Id,IsClosed from Task Where OwnerId =: u.Id]);
IF(tasklist.size() >= 1) {
taskPass = TRUE;
this.Completed += 1;
}
return taskPass;
}
public boolean getCompletedStatus() {
system.debug('this.completed = ' + this.completed);
system.debug('this.attempted = ' + this.attempted);
boolean MadeIt = FALSE;
IF(this.completed == this.attempted){
MadeIt = TRUE;}
system.debug('madeit = ' + madeIt);
Return MadeIt;
}
public integer getExercisesToDo() {
integer att = this.attempted;
integer comp = this.completed;
integer toDo = att - comp;
system.debug('exercisesRemaining' + toDo);
RETURN toDo;
}
/* not needed until they can auto-request upgrade
public PageReference save() {
update u;
return null;
}
*/
}