2

I'm developing a community in Winter'19 sandbox and have opted to use the configurable self-registration form. I get an ambiguous error message whenever someone tries to register with an existing email/username. How can I customise the error message?

I'm aware of this issue here: https://success.salesforce.com/issues_view?id=a1p3A0000019QlIQAU

But the error I get is: "Hmm. Something isn't right. Please try again." This error is literally not helpful.

I'd like to keep the configurable self-rego page, but just want to update the error message.

2
  • Did you try to change the standard exception page and get the proper error descriptio? {!site.errorDescription}. Following post will help you if not helaforce.wordpress.com/2017/11/13/… Commented Sep 21, 2018 at 3:40
  • That doesn't help me because I'm using the configurable self register in Winter '19 that creates its own apex class for registration.
    – DCole
    Commented Sep 24, 2018 at 5:53

1 Answer 1

4

I used a try and catch method and then if statements matching the exact error code to replace with more helpful errors.

Edited to contain code:

global Id createUser(Id accountId, Id profileId, Map<SObjectField, String> registrationAttributes, String password) {
        // Show all errors

This is where custom code starts as a try (but it's trying all the pre-built code when you create a Self-Reg class:

        try {
            User u = new User();
            u.ProfileId = profileId;
            for (SObjectField field : registrationAttributes.keySet()) {
                String value = registrationAttributes.get(field); 
                u.put(field, value);
            }

            u = handleUnsetRequiredFields(u);
            generateContact(u, accountId);
            if (String.isBlank(password)) {
                password = generateRandomPassword();
            }
            Site.validatePassword(u, password, password);

(above)Tries to create a user and add it to the specified Profile in the community settings.

(below) adds "prtl" to last name of user.

            // Add suffix _prtl to User - Last Name (10 Dec 2018)
            u.LastName += '_prtl';
            u.TSV_Portal__c = True;

            if (u.contactId == null) {
                return Site.createExternalUser(u, accountId, password);
            }

            insert u; 

            System.setPassword(u.Id, password);
            return u.id;


            //ERROR HANDLING

(below)This is where the actual error handling & custom message begins. We had to manually do registrations wrong, duplicates, etc, to get the exact errors.

        } catch (Exception e) {
            string message = e.getMessage();
            string LinkUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
            if(message.equals('[User already exists.]')){
                throw new Auth.DiscoveryCustomErrorException ('User Already Exists, please go to login page. ' + LinkUrl );
            }else if(message.equals('[There was an error in registering a user in site [SiteName]. The error message is: portal user already exists for contact]')) {

                throw new Auth.DiscoveryCustomErrorException('An error occurred, please try logging in or using a different email. You can login at ' + LinkUrl);
            }else if(message.equals('Your password must have a mix of letters and numbers')) {
                throw new Auth.DiscoveryCustomErrorException (e.getMessage());
            }else if(message.equals('Your password must be at least 8 characters long.')) {
                throw new Auth.DiscoveryCustomErrorException (e.getMessage());
            }else if(message.contains('DUPLICATES_DETECTED')) {
                throw new Auth.DiscoveryCustomErrorException ('Account already exists, please login or contact us for help.');
            }else{
                throw new Auth.DiscoveryCustomErrorException ('Please contact us and give us this error message: ' + e.getMessage());
            }

        }
    }

Some of the errors we were happy to retain (like password), but wanted it documented for later changes if needed.

And at the very end, if it was some weird error, we asked them to contact us to give us the error message so we could then make a new else if statement if needed.

3
  • Hi @DCole, can you please elaborate a bit more on your solution? It would be helpful to others if you could provide additional details regarding where you placed the try/catch block, or, even better, if you could provide a short code snippet with your solution. Thanks
    – smukov
    Commented Jul 12, 2019 at 8:29
  • 1
    Hi @smukov, I have updated my solution with code.
    – DCole
    Commented Jul 22, 2019 at 1:22
  • This is super helpful. Thanks!
    – kevdecent
    Commented Mar 17, 2022 at 21:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .