Simple Forgot Username feature in Salesforce Community/Experience Cloud

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="{!}"/><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("")
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.userRecord", response.getReturnValue());

Apex Class:

public without sharing 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.


  1. Hi I am trying to achieve this now, however keeps saying 'not found' etc even though as an admin I know that the email address does exist. I have added the apex class to the site guest profile too.

    Would you know why this is not working, possibly something that now needs to be altered?


    1. Andrew,
      Add without sharing keyword and see whether it works.