15

We are creating our own ISV trial provisioning page and using the SignupRequest API to create trials. I can't seem to figure out a way to check for the availability of a username synchronously. Unfortunately, the only way to figure out if a username is taken is to signup and then the user gets a pretty generic failure email with a link back to the wrong trial page (the standard Salesforce trial signup page). I would rather check for the existence of a username while the user is filling out the form if possible. Is there any API that enables this?

1
  • Hard to believe there is no such idea even, just created one here . Feb 22, 2019 at 0:41

1 Answer 1

20

No need for an API. Just use synchronous Apex code instead.

Simply create and insert a User in a try and check for the right exception in the catch and always rollback the insert in the finally.

This class should do the job.

public class UserService {
    public static Boolean existsName(String fullUserName) {
        Boolean result = false;

        User user = new User();  
        user.FirstName = 'any';
        user.LastName = 'any';
        user.Alias = 'any';
        user.EMail = 'any@email.com';
        user.Username = fullUserName;
        user.CommunityNickname = fullUserName;
        user.LocaleSidKey = 'en_US';
        user.LanguageLocaleKey = 'en_US';
        user.EmailEncodingKey = 'UTF-8';
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.ProfileId = [SELECT Id, Name FROM Profile WHERE Name='Standard User' limit 1].Id;

        Savepoint sp = Database.setSavepoint();

        try {
            insert user;
        }
        catch(DmlException ex) {
            if(ex.getDmlStatusCode(0) == StatusCode.DUPLICATE_USERNAME.name()) {
                result = true; 
            }
        }
        finally {
            Database.rollback(sp);
        }

        return result;
    }
}
9
  • 2
    I think this answer would be somewhat more robust if you change your catch conditional to ex. getDmlStatusCode(0) == StatusCode.DUPLICATE_USERNAME.
    – Adrian Larson
    Apr 22, 2016 at 1:27
  • 1
    @AdrianLarson: That's awesome. I didn't know those StatusCodes exist. I changed my code snippet to use this. Apr 22, 2016 at 10:45
  • 2
    This conditional handling actually gives an error saying the following : "Comparison arguments must be compatible types: String, StatusCode" and You should do the following – " if(ex.getDmlStatusCode(0) == String.valueOf(StatusCode.DUPLICATE_USERNAME))" May 31, 2016 at 10:23
  • 1
    @Shane Feel free to provide a better solution. Otherwise this is a somewhat lame comment. Feb 7, 2019 at 16:33
  • 1
    Sorry, it isn't a lame comment nor a personal opinion. Using exceptions for flow-control is a well-known anti-pattern: stackoverflow.com/questions/729379/… . That being said, what is totally lame is SF doesn't (seem to) give you a way to determine whether a username is unique before entering an error state. Feb 7, 2019 at 19:47

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.