12

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?

  • Hard to believe there is no such idea even, just created one here . – Pasan Eeriyagama Feb 22 at 0:41
18

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;
    }
}
  • Prev I was looking for some API so before performing DML we could check USERNAME is duplicate or not, but seems there is no option for that.. Only way that Rebert mentioned.. – Ratan Paul Apr 13 '16 at 5:12
  • 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 '16 at 1:27
  • 1
    @AdrianLarson: That's awesome. I didn't know those StatusCodes exist. I changed my code snippet to use this. – Robert Sösemann Apr 22 '16 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))" – Tanmoy Bhattacharjee May 31 '16 at 10:23
  • @TanmoyBhattacharjee: Thanks for pointing out this bug. I changed the code so it works. – Robert Sösemann Jun 1 '16 at 7:30

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.