Salesforce Sites do not provide a global object to access logged in user details. To get the details of the current user and pass it to AptEdge, we’ll need to create an Apex class.

Create the Apex class

  1. Log into Salesforce as an administrator.
  2. Click the ⚙️ gear icon in the top-right corner of the page.
  3. Choose Setup to enter Salesforce Setup.
  4. In the Setup search bar, type Apex Classes, then choose Apex Classes.

image.png

  1. Click New to create a new Apex class.
  2. In the Apex code editor, paste in the following sample REST API. Note the class name that is defined in the code, which in this case is SiteUserInfo.
@RestResource(urlMapping='/siteUserInfo')

global with sharing class SiteUserInfo {
    global class UserInfoDTO {
        public String userId;
        public String name;
        public String email;
        public String userType;
        public Boolean isAuthenticated;
    }
    @HttpGet
    global static UserInfoDTO getUserDetails() {
        UserInfoDTO result = new UserInfoDTO();
        result.userId = UserInfo.getUserId();
        result.name = UserInfo.getName();
        result.email = UserInfo.getUserEmail();
        result.userType = UserInfo.getUserType();
        result.isAuthenticated = result.userType != 'Guest';        
        return result;
    }
}
  1. Choose Save.

Configure access to the Apex class

Guest users and JavaScript code running on your Salesforce Site can’t access your new Apex class by default, so we must explicitly allow public access.

  1. In Salesforce Setup, search for and choose Sites.

image.png

  1. Select the site label for your Salesforce Site.
  2. Choose Public Access Settings.
  3. Scroll down to the section named Enabled Apex Class Access, then choose Edit.

image.png

  1. Add your newly created Apex class, e.g. SiteUserInfo, to the Enabled Apex Classes list, then choose Save.