April 23, 2018

Simple Forgot Username feature in Salesforce Community

1. Create Forgot Username Lightning Component and Apex Class.

Lightning Component:

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global" controller="ForgotUsernameController">

    <aura:attribute name="email" type="String"/>
    <aura:attribute name="searchBool" type="Boolean"/>
    <aura:attribute name="userRecord" type="User"/>
    <div class="slds-grid slds-grid_vertical slds-grid_vertical-align-center">
        <div class="slds-col">
            <span>Enter your email address</span>
        <div class="slds-col">
            <span><lightning:input type="text" value="{!v.email}"/><br/></span>
        <div class="slds-col">
            <span><lightning:button label="Find Username" onclick="{!c.findUser}"/></span>
     <div class="slds-grid slds-grid_vertical slds-grid_vertical-align-center">
        <div class="slds-col">
            <aura:if isTrue="{!v.searchBool}">
                    <aura:if isTrue="{!v.userRecord}">
                        Username is {!v.userRecord.Username}<br/><br/>
                        User Active? {!v.userRecord.IsActive}
                        <aura:set attribute="else">
                            User not matched with this email.

Lightning Controller:

findUser : function(component, event, helper) {
        component.set("v.searchBool", true);
        var action = component.get("c.fetchUser");
            emailAddress : component.get("v.email")
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.userRecord", response.getReturnValue());

Apex Class:

public class ForgotUsernameController {
    public static User fetchUser(String emailAddress) {
        List < User > listUsers = [ SELECT Id, Username, IsActive FROM User WHERE Email =: emailAddress ];
        if ( listUsers.size() > 0 ) {
            User usr = listUsers[0];
            return usr;
        return null;


2. Create Forgot Username page in Salesforce Community with Page Access set to Public.

3. Drag and Drop the Forgot Username Lightning Component.

4. Open the Forgot Username page in a new browser to test it.

No comments:

Post a Comment