VisualForce Page:
<apex:page id="communitiesSelfRegPage" showHeader="true" controller="CommunitiesSelfRegController" title="{!$Label.site.user_registration}">
<script type="text/javascript">
function getEnteredCommunityCode(){
var enteredCode = document.getElementById("communitiesSelfRegPage:theForm:CommunityCode");
//alert(enteredCode.value);
return enteredCode.value;
}
function getSelectedCampus(){
var campus= document.getElementById("communitiesSelfRegPage:theForm:sl_campus");
//alert("hello");
return campus.options[campus.selectedIndex].value;
}
function getSelectedLevel(){
var level= document.getElementById("communitiesSelfRegPage:theForm:sl_year");
//alert(level.options[level.selectedIndex].value);
return level.options[level.selectedIndex].value;
}
</script>
<apex:define name="body">
<center>
<apex:form id="theForm" forceSSL="true">
<apex:pageMessages id="error"/>
<apex:panelGrid columns="2" style="margin-top:1em;">
<apex:outputLabel value="First Name" for="firstName"/>
<apex:inputText required="true" id="firstName" value="{!firstName}" label="First Name"/>
<apex:outputLabel value="Last Name" for="lastName"/>
<apex:inputText required="true" id="lastName" value="{!lastName}" label="Last Name"/>
<apex:outputLabel value="{!$Label.site.community_nickname}" for="communityNickname"/>
<apex:inputText required="true" id="communityNickname" value="{!communityNickname}" label="{!$Label.site.community_nickname}"/>
<apex:outputLabel value="{!$Label.site.email}" for="email"/>
<apex:inputText required="true" id="email" value="{!email}" label="{!$Label.site.email}"/>
<apex:outputLabel value="{!$Label.site.password}" for="password"/>
<apex:inputSecret id="password" value="{!password}"/>
<apex:outputLabel value="{!$Label.site.confirm_password}" for="confirmPassword"/>
<apex:inputSecret id="confirmPassword" value="{!confirmPassword}"/>
<apex:outputLabel value="{!$Label.CommunityCode}" for="CommunityCode"/>
<apex:inputText required="true" id="CommunityCode" value="{!CommunityCode}" label="{!$Label.CommunityCode}" οnchange="loadCampusJavascript(getEnteredCommunityCode())"/>
<apex:outputLabel value="{!$Label.Campus}" for="sl_campus" id="lbl_campus" style="display:{!campus_display_status}"/>
<apex:selectlist multiselect="false" id="sl_campus" οnchange="loadYearJavascript(getSelectedCampus())" style="display:{!campus_display_status}" size="1" value="{!studentCampus}" >
<apex:selectOptions value="{!options_Campus}"/>
</apex:selectlist>
<apex:outputLabel value="{!$Label.Year_Level}" for="sl_year" id="lbl_year" style="display:{!yearlevel_display_status}"/>
<apex:selectlist multiselect="false" id="sl_year" style="display:{!yearlevel_display_status}" size="1" required="true" value="{!studentYearLevel}" οnchange="enableBtnJavascript(getSelectedLevel())" >
<apex:selectOptions value="{!options_Years}"/>
</apex:selectlist>
<apex:inputText id="MsgDebug" value="{!msgDebug}" style="display:none;"></apex:inputText>
<apex:commandButton action="{!registerUser}" value="{!$Label.site.submit}" id="submit" disabled="{!btnDisable}"/>
</apex:panelGrid>
<br/>
</apex:form>
<apex:form >
<apex:actionfunction name="loadCampusJavascript" action="{!loadCampus}" rerender="communitiesSelfRegPage:theForm:sl_campus,communitiesSelfRegPage:theForm:lbl_campus,communitiesSelfRegPage:theForm:sl_year,communitiesSelfRegPage:theForm:lbl_year,communitiesSelfRegPage:theForm:MsgDebug">
<apex:param name="firstParam" assignTo="{!para1}" value="" />
</apex:actionfunction>
</apex:form>
<apex:form >
<apex:actionfunction name="loadYearJavascript" action="{!loadYear}" rerender="communitiesSelfRegPage:theForm:sl_year,communitiesSelfRegPage:theForm:lbl_year,communitiesSelfRegPage:theForm:MsgDebug,communitiesSelfRegPage:theForm:submit">
<apex:param name="campusParam" assignTo="{!paracampus}" value="" />
</apex:actionfunction>
</apex:form>
<apex:form >
<apex:actionfunction name="enableBtnJavascript" action="{!enableBtn}" rerender="communitiesSelfRegPage:theForm:submit">
<apex:param name="yearParam" assignTo="{!parayear}" value="" />
</apex:actionfunction>
</apex:form>
<div id="output"></div>
</center>
<br/>
</apex:define>
</apex:page>
Apex controller:
/**
* An apex page controller that supports self registration of users in communities that allow self registration
*/
public with sharing class CommunitiesSelfRegController {
public PageReference enableBtn() {
if(parayear!='0'){
btnDisable='false';
}else
{
btnDisable='true';
}
return null;
}
string btnDisable ='true';
public String getBtnDisable() {
return btnDisable ;
}
public void setBtnDisable(string n) {
btnDisable = n;
}
//control the visibility of campus seleclist
string Campus_display_status='none';
//control the visibility of yearlevel seleclist
string Yearlevel_display_status='none';
string messageDebug;
public String getMsgDebug() {
return messageDebug;
}
public void setMsgDebug(string input){
messageDebug = input;
}
List<SelectOption> Selectlist_campus= new List<SelectOption>();
public String getCampus_display_status() {
return Campus_display_status;
}
public String getYearlevel_display_status() {
return Yearlevel_display_status;
}
public List<SelectOption> getOptions_Campus() {
return Selectlist_campus;
}
List<SelectOption> Selectlist_years = new List<SelectOption>();
public List<SelectOption> getOptions_Years() {
return Selectlist_years;
}
//year level based on campus and community code
public void insertYearLevelOptions(string campus, string code){
List<SchoolCampusYear__c> lst_years = new List<SchoolCampusYear__c>();
try{
//get campus list based on entered Community Code
lst_years = (List<SchoolCampusYear__c>)[SELECT Year_Level__c FROM SchoolCampusYear__c WHERE CommunityCode__c=:code and Campus__c=:campus];
}
catch(QueryException q)
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Query Error - year level');
ApexPages.addMessage(msg);
}
integer count_years = lst_years.size();
//messageDebug =paracampus;
messageDebug = string.valueof(count_years );
Selectlist_years.Clear();
Selectlist_years.add(new SelectOption('0',' '));
for(integer i=0;i<count_years ;i++)
{
string year_value = lst_years.get(i).Year_Level__c;
string year_label = lst_years.get(i).Year_Level__c;
Selectlist_years.add(new SelectOption(year_value ,'Year '+year_label ));
}
}
//campus selectlist onchange event
public PageReference loadYear() {
Yearlevel_display_status='block';
if(paracampus!='0'){
btnDisable='false';
}else
{
btnDisable='true';
}
insertYearLevelOptions(paracampus,para1);
return null;
}
//community code field onkeyup event
public PageReference loadCampus() {
List<SchoolCampus__c> lst_campus = new List<SchoolCampus__c>();
try{
//get campus list based on entered Community Code
lst_campus = (List<SchoolCampus__c>)[SELECT Campus_Name__c FROM SchoolCampus__c WHERE Community_Code__c=:para1];
}
catch(QueryException q)
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Query Error');
ApexPages.addMessage(msg);
}
//fill the campuses into the select list
integer count_campus = lst_campus.size();
if(count_campus>1){
Campus_display_status = 'block';
Yearlevel_display_status='none';
}
else if(count_campus==1){
Yearlevel_display_status='block';
Campus_display_status = 'none';
messageDebug ='hello 2';
string only_campus = lst_campus.get(0).Campus_Name__c;
//second paramenter para1
studentCampus =only_campus;
insertYearLevelOptions(only_campus ,para1);
}
else
{
}
Selectlist_campus.Clear();
Selectlist_campus.add(new SelectOption('0' ,' '));
for(integer i=0;i<count_campus ;i++)
{
string campus_value = lst_campus.get(i).Campus_Name__c;
string campus_label = lst_campus.get(i).Campus_Name__c;
Selectlist_campus.add(new SelectOption(campus_value ,campus_label ));
}
return null;
}
public String firstName {get; set;}
public String lastName {get; set;}
public String email {get; set;}
public String password {get; set {password = value == null ? value : value.trim(); } }
public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
public String CommunityCode {get;set;}
public String studentYearLevel { get; set; }
public String studentCampus { get; set; }
string para1;
public void setPara1(string n){
para1= n;
}
public string getPara1(){
return para1;
}
string paracampus;
public void setParacampus(string n){
paracampus= n;
}
public string getParacampus(){
return paracampus;
}
string parayear;
public void setParayear(string n){
parayear= n;
}
public string getParayear(){
return parayear;
}
public CommunitiesSelfRegController() {}
private boolean isValidPassword() {
return password == confirmPassword;
}
public PageReference registerUser() {
// it's okay if password is null - we'll send the user a random password in that case
if (!isValidPassword()) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
ApexPages.addMessage(msg);
return null;
}
User[] userList = [SELECT ID,Email FROM User WHERE Email=:email];
if(!userList.isEmpty()){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'User existing already.');
ApexPages.addMessage(msg);
//return null;
string userId = userList[0].ID;
string userName = email;
if (userId != null) {
if (password != null && password.length() > 1) {
return Site.login(userName, password, null);
}
else {
PageReference page = System.Page.CommunitiesSelfRegConfirm;
page.setRedirect(true);
return page;
}
}
}
String profileId = null; // To be filled in by customer.
String roleEnum = null; // To be filled in by customer.
String accountId = ''; // To be filled in by customer.
String userName = email;
PortalCodeMapping__c codeMapping = [SELECT Account_ID__c,Customer_Type__c FROM PortalCodeMapping__c WHERE Portal_Code__c =: CommunityCode];
accountId = codeMapping.Account_ID__c;
//codeMapping.Account_ID__c =
//check if the email is occupied by an existing contact
Contact[] thisContactList = [SELECT Id,AccountId FROM Contact where Email =: email];
if(thisContactList.isEmpty()){
}
else{
Integer contactListSize = thisContactList.size();
if(contactListSize >1){
}
else
{
thisContactList[0].AccountId = accountId;
try {
update thisContactList[0];
}
catch (DmlException e) {
// Process exception here
}
}
}
//string contactId= thisContact.Id;
//if(contactId == null || contactId ==''){
//}
//create new user
User u = new User();
u.Username = userName;
u.Email = email;
u.FirstName = firstName;
u.LastName = lastName;
u.CommunityNickname = communityNickname;
u.ProfileId = profileId;
u.CommunityCode__c= CommunityCode;
//if it's a new contact
String userId = Site.createPortalUser(u, accountId, password);
//save community code into the created contact
Contact NewContact = [SELECT CommunityCode__c FROM Contact WHERE Email=:email];
NewContact.CommunityCode__c = CommunityCode;
NewContact.Student_year_level__c = studentYearLevel ;
NewContact.Student_Campus__c=studentCampus;
try {
update NewContact;
}
catch (DmlException e) {
// Process exception here
}
if (userId != null) {
if (password != null && password.length() > 1) {
return Site.login(userName, password, null);
}
else {
PageReference page = System.Page.CommunitiesSelfRegConfirm;
page.setRedirect(true);
return page;
}
}
return null;
}
}