0

We are using angular to display UI and Salesforce is being used to maintain database. We have a REST API class created which has the url Mapping. This class is used to find the records.

We are using 'Customer Community Login' Custom profile and need to check the data by logging as Community User.

Im experiencing an error when trying to login through SSO: REGISTRATIONERROR: SObject row was retrieved via SOQL without querying the requested field: User.ProfileId

Below is my piece of code:

global class Registration_Handler implements Auth.RegistrationHandler {

global boolean canCreateUser(Auth.UserData data) {
    return true;
}

global User createUser(Id portalId, Auth.UserData data) {       


    String ssoGUID = data.identifier;

    // Check if the User already exists
    List<User> existingUsers = [SELECT Id, profileId, contactId, isActive FROM User WHERE SSO_Identifier__c = :ssoGUID];
    if(existingUsers != null && existingUsers.size() > 0) {
        return validateCommunityUser(existingUsers.get(0));
    }

    // Create a new user 
    else {
        // Block the user if he can't create new users
        if(!canCreateUser(data)) {

            return null;
        }

        // Parse user's info, checking if the full name is defined or not
        String firstName, lastName, dataFirstName, dataLastName, dataFullName;
        dataFirstName = '' + data.firstName;
        dataLastName = '' + data.lastName;
        dataFullName = '' + data.fullName;

        if(dataFirstName == null || dataFirstName.length() == 0 || dataFirstName.equalsIgnoreCase('null')
            || dataLastName == null || dataLastName.length() == 0 || dataLastName.equalsIgnoreCase('null')) {
            // Use the full name
            if(dataFullName != null && dataFullName.length() > 0 && !dataFullName.equalsIgnoreCase('null')) {
                firstName = dataFullName.substringBefore(' ');
                lastName = dataFullName.substringAfter(' ');
            } else {                   
                return null;
            }
        } else {
            firstName = dataFirstName;
            lastName = dataLastName;
        }

        // Get user's email
        String email = data.email;
        if(email == null || email.length() == 0) {                
            return null;
        }

        // Query the person account to see if an existing record is available
        List<Account> existingPersonAccounts = [Select Id, PersonContactId From Account 
            Where PersonEmail LIKE :email AND Name = :dataFullName LIMIT 1];

        // Check if the person account actually exists or if it needs to be created
        Account customerPA = null;
        if(existingPersonAccounts == null || existingPersonAccounts.size() == 0) {
            // Create the Account
            Map<String, String> accountRecordTypeMap = getRecordTypeMap('Account');
            customerPA = new Account(
                RecordTypeId = accountRecordTypeMap.get(ACCOUNT_RECORD_TYPE_CUSTOMER),
                FirstName = firstName,
                LastName = lastName, 
                PersonEmail = email
            );
            insert customerPA;

            List<Account> personAccounts = [Select PersonContactId From Account Where Id = :customerPA.Id];
            customerPA = personAccounts.get(0);
        } else {
            // Get the existing one
            customerPA = existingPersonAccounts.get(0);
        }

        // Check the contact side of the PA has been created
        if(customerPA == null || customerPA.PersonContactId == null) {               
            return null;
        }

        // Check if an existing user is already been associated to the found / created PersonAccount
        List<User> existingCommunityUser = [SELECT Id FROM User WHERE contactId = :customerPA.PersonContactId];
        User communityUser = null;
        if(existingCommunityUser != null && existingCommunityUser.size() > 0) {
            communityUser = validateCommunityUser(existingCommunityUser.get(0));
        } else {
            //Alias must be 8 characters or less
            String alias = email;
            if(alias.length() > 8) {
                alias = alias.substring(0, 8);
            }

            // Create the user
            communityUser = new User(
                username = getUsernamePrefix() + email + getUsernameSuffix(),
                email = email,
                lastName = lastName,
                firstName = firstName,

                profileId = getProfile().Id,
                contactId = customerPA.PersonContactId,
                SSO_Identifier__c = ssoGUID,

            );
        }

        return communityUser;
    }
}
/**
* Return the profile that must be used
*/
public static Profile getProfile() {
    return [SELECT Id FROM profile WHERE name=:getProfileName()];
}

/**
* Validate the community user record
*/
private User validateCommunityUser(User communityUser) {
    if(communityUser == null) {
      return null;
    } else {
        // Verify if the user has the right profile
        if(checkCommunityUserProfile(communityUser)) {
            return validateCommunityUserActive(communityUser);
        } else {             
            return null;
        }
    }
}

/**
* Check if the community user profile is the expected one
*/
private Boolean checkCommunityUserProfile(User communityUser) {
    // Check if the profile is the expected one
    return communityUser != null && (communityUser.ProfileId == getProfile().Id || communityUser.ContactId != null);
}

/**
* Validate if the community user is active. If not, activate the record
*/
private User validateCommunityUserActive(User communityUser) {
    if(communityUser != null) {
        // Switch the active flag
        if(!communityUser.isActive) {
            communityUser.isActive = true;
            try {
                update communityUser;
            } catch(Exception e) {
                return null;
            }
        }

        return communityUser;
    } else {           
        return null;
    }
}

}

1

In one of the method, User.ProfileId is not queried. The checkCommunityUserProfile(communityUser) method tries to access the ProfileId field incommunityUser which is not in the SOQL query.

Updated code:

global class TQSsoRegistrationHandler implements Auth.RegistrationHandler {

global boolean canCreateUser(Auth.UserData data) {
    return true;
}

global User createUser(Id portalId, Auth.UserData data) {       


    String ssoGUID = data.identifier;

    // Check if the User already exists
    List<User> existingUsers = [SELECT Id, profileId, contactId, isActive FROM User WHERE SSO_Identifier__c = :ssoGUID];
    if(existingUsers != null && existingUsers.size() > 0) {
        return validateCommunityUser(existingUsers.get(0));
    }

    // Create a new user 
    else {
        // Block the user if he can't create new users
        if(!canCreateUser(data)) {

            return null;
        }

        // Parse user's info, checking if the full name is defined or not
        String firstName, lastName, dataFirstName, dataLastName, dataFullName;
        dataFirstName = '' + data.firstName;
        dataLastName = '' + data.lastName;
        dataFullName = '' + data.fullName;

        if(dataFirstName == null || dataFirstName.length() == 0 || dataFirstName.equalsIgnoreCase('null')
            || dataLastName == null || dataLastName.length() == 0 || dataLastName.equalsIgnoreCase('null')) {
            // Use the full name
            if(dataFullName != null && dataFullName.length() > 0 && !dataFullName.equalsIgnoreCase('null')) {
                firstName = dataFullName.substringBefore(' ');
                lastName = dataFullName.substringAfter(' ');
            } else {                   
                return null;
            }
        } else {
            firstName = dataFirstName;
            lastName = dataLastName;
        }

        // Get user's email
        String email = data.email;
        if(email == null || email.length() == 0) {                
            return null;
        }

        // Query the person account to see if an existing record is available
        List<Account> existingPersonAccounts = [Select Id, PersonContactId From Account 
            Where PersonEmail LIKE :email AND Name = :dataFullName LIMIT 1];

        // Check if the person account actually exists or if it needs to be created
        Account customerPA = null;
        if(existingPersonAccounts == null || existingPersonAccounts.size() == 0) {
            // Create the Account
            Map<String, String> accountRecordTypeMap = getRecordTypeMap('Account');
            customerPA = new Account(
                RecordTypeId = accountRecordTypeMap.get(ACCOUNT_RECORD_TYPE_CUSTOMER),
                FirstName = firstName,
                LastName = lastName, 
                PersonEmail = email
            );
            insert customerPA;

            List<Account> personAccounts = [Select PersonContactId From Account Where Id = :customerPA.Id];
            customerPA = personAccounts.get(0);
        } else {
            // Get the existing one
            customerPA = existingPersonAccounts.get(0);
        }

        // Check the contact side of the PA has been created
        if(customerPA == null || customerPA.PersonContactId == null) {               
            return null;
        }

        // Check if an existing user is already been associated to the found / created PersonAccount
        /* Added Profile Id field in the SOQL */
        List<User> existingCommunityUser = [SELECT Id, ProfileId FROM User WHERE contactId = :customerPA.PersonContactId];
        /* Added Profile Id field in the SOQL */
        User communityUser = null;
        if(existingCommunityUser != null && existingCommunityUser.size() > 0) {
            communityUser = validateCommunityUser(existingCommunityUser.get(0));
        } else {
            //Alias must be 8 characters or less
            String alias = email;
            if(alias.length() > 8) {
                alias = alias.substring(0, 8);
            }

            // Create the user
            communityUser = new User(
                username = getUsernamePrefix() + email + getUsernameSuffix(),
                email = email,
                lastName = lastName,
                firstName = firstName,

                profileId = getProfile().Id,
                contactId = customerPA.PersonContactId,
                SSO_Identifier__c = ssoGUID,

            );
        }

        return communityUser;
    }
}
/**
* Return the profile that must be used
*/
public static Profile getProfile() {
    return [SELECT Id FROM profile WHERE name=:getProfileName()];
}

/**
* Validate the community user record
*/
private User validateCommunityUser(User communityUser) {
    if(communityUser == null) {
      return null;
    } else {
        // Verify if the user has the right profile
        if(checkCommunityUserProfile(communityUser)) {
            return validateCommunityUserActive(communityUser);
        } else {             
            return null;
        }
    }
}

/**
* Check if the community user profile is the expected one
*/
private Boolean checkCommunityUserProfile(User communityUser) {
    // Check if the profile is the expected one
    return communityUser != null && (communityUser.ProfileId == getProfile().Id || communityUser.ContactId != null);
}

/**
* Validate if the community user is active. If not, activate the record
*/
private User validateCommunityUserActive(User communityUser) {
    if(communityUser != null) {
        // Switch the active flag
        if(!communityUser.isActive) {
            communityUser.isActive = true;
            try {
                update communityUser;
            } catch(Exception e) {
                return null;
            }
        }

        return communityUser;
    } else {           
        return null;
    }
}

}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.