You need to sign in to do that
Don't have an account?
Self-Registration - Customer Portal / Sites
Hi Everyone,
I hope that someone out there might be able to provide me with a little help with some of my code. I am currently trying to modify the Sites self-registration page to work like the Customer Portal page. This modification would allow for a portal user to be created if the contact card has the "Self-Register in Portal" checkbox selected and they enter their email on the SiteRegister page.
I will provide both the VF page and APEX:
1. SiteRegister (VF)
<apex:page id="loginPage" showHeader="true" controller="SiteRegisterController" title="{!$Label.site.register}"> <apex:composition template="{!$Site.Template}"> <apex:define name="body"> <style> .tabNavigation {display:none;} </style> <style> .sidebarCell {display:none;} </style> <style>.sidebarDiv {display:none;} </style><center> <apex:panelGrid bgcolor="004f8c" columns="1"> <br/> <br/> <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="1" styleClass="topPanelContainer"> <br/> <apex:outputPanel layout="block" styleClass="topPanel"> <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="2"> <apex:image url="https://c.na8.content.force.com/servlet/servlet.ImageServer?id=015C0000001MSFY&oid=00D80000000bQYx&lastMod=1304543302000"/> <apex:panelGroup > <br/> <apex:outputText styleClass="title" value="{!$Label.site.user_registration}"/> <br/> <apex:form id="theForm" forceSSL="true"> <apex:pageMessages id="error"/> <apex:panelGrid columns="2" style="margin-top:1em;"> <apex:outputLabel value="{!$Label.site.email}" for="email1"/> <apex:inputText required="true" id="email1" value="{!email1}"/> <apex:commandButton action="{!registerUser}" value="{!$Label.site.submit}" id="submit"/> </apex:panelGrid> </apex:form> <br/> </apex:panelGroup> </apex:panelGrid> </apex:outputPanel> <c:SitePoweredBy /> </apex:panelGrid> </apex:panelGrid> </center> <br/> </apex:define> </apex:composition> <site:googleAnalyticsTracking /> </apex:page>
2. SiteRegisterController (APEX)
/** * An apex class that creates a portal user */ public with sharing class SiteRegisterController { public String email1 {get; set;} public SiteRegisterController () { } public PageReference registerUser() { List<User> alreadyExists = [Select Id from User WHERE username =:email1 AND IsActive = true LIMIT 1]; if (alreadyExists.size() > 0) { PageReference page = System.Page.SiteLogin; page.setRedirect(true); return page; } else { List<Contact> c = [Select Id, AccountId, FirstName, LastName, Email FROM Contact WHERE CanAllowPortalSelfReg = true AND Email =:email1]; if (!c.isEmpty()) { Profile profile = [select Id, name from Profile where Name=:'Customer Portal Manager 1']; System.debug('***** ContactID1--> ' + c[0].id); SiteRegisterController.createUser(c[0].id, c[0].email, c[0].firstname, c[0].lastname, profile.id); ApexPages.Message myMsg1 = new ApexPages.Message(ApexPages.Severity.INFO, 'Successfully registered your Account. Please await for an email containing futher instructions'); ApexPages.addMessage(myMsg1); //PageReference page = System.Page.SiteRegisterConfirm; //page.setRedirect(true); //return page; return null; } else { ApexPages.Message myMsg2 = new ApexPages.Message(ApexPages.Severity.ERROR, 'This is currently an invitation only system. Please contact the system administrator to gain access.' + ' ' + c.size()); ApexPages.addMessage(myMsg2); //PageReference page = System.Page.Exception; //page.setRedirect(true); //return page; return null; } } } public static void createUser(String contactid, String email, String firstname, String lastname, String profileid) { Database.DMLOptions dmo = new Database.DMLOptions(); dmo.EmailHeader.triggerUserEmail = true; User u = new User(); u.ContactId = contactid; u.Username = email; u.Alias = u.Username.split('@').get(0); u.Email = email; u.Firstname = firstname; u.Lastname = lastname; u.ProfileID = profileid; u.communityNickname = u.Username.split('@').get(0); //u.PortalRole = 'Test Customer Account Customer User'; u.LanguageLocaleKey = 'en_US'; u.LocaleSidKey = 'en_CA'; u.TimeZoneSidKey = 'America/Los_Angeles'; u.EmailEncodingKey='UTF-8'; System.debug('***** Username--> ' + u.Username); System.debug('***** ContactId--> ' + u.ContactId); System.debug('***** Email--> ' + u.Email); System.debug('***** Firstname--> ' + u.Firstname); System.debug('***** Lastname--> ' + u.Lastname); System.debug('***** ProfileID--> ' + u.ProfileID); System.debug('***** PortalRole--> ' + u.PortalRole); System.debug('***** Alias--> ' + u.Alias); u.setOptions(dmo); insert u; System.Debug('***** UserId-->' + u.Id); } // Test method to bring this class's test coverage over the required 75% @IsTest(SeeAllData=true) static void testRegistration() { SiteRegisterController controller = new SiteRegisterController(); controller.email1 = 'tmowbrey@belmar.ca'; controller.registerUser(); // registerUser will always return null when the page isn't accessed as a guest user //System.assert(controller.registerUser() == null); controller.email1 = 'idonotexist@domain.com'; controller.registerUser(); Contact c = new Contact(firstName='TestFirstName', lastName='TestLastName', email='iamauser@domain.com', accountid='001U0000006KtbB'); insert c; System.debug('***** ContactIDTest--> ' + c.id); Profile profile = [select Id, name from Profile where Name=:'Customer Portal Manager 1']; SiteRegisterController.createUser(c.id, c.email, c.firstname, c.lastname, profile.id); controller.email1 = 'iamauser@domain.com'; controller.registerUser(); } }
The tests are correctly creating a portal user for tmowbrey@belmar.ca as this contact exists (has no portal user created) and has the Self-Register checkbox selected.
The issue occurs when actually deploying and using the code in actuallity. Everytime I put in tmowbrey@belmar.ca I receive "'This is currently an invitation only system. Please contact the system administrator to gain access." with a c.size() of 0. I can not figure out why.
Please let me know if I can provide more information.
Regards,
Tyler Mowbrey
Sounds like it might be permissions/sharing settings. Would the Site Guest user have access to the Contact and Account that you're referencing?
All Answers
Sounds like it might be permissions/sharing settings. Would the Site Guest user have access to the Contact and Account that you're referencing?
You were correct. The issue was around the fact that I have accounts/contacts set to private and needed to create a sharing rule to allow the guest user to see my contacts.
Thanks for the help!