Activity Forums Salesforce® Discussions API - Is there any API that allows me to check if a Username is in use?

  • Gourav

    Member
    May 30, 2016 at 11:34 am

    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 = '[email protected]';
    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) {
    result = true;
    }
    }
    finally {
    Database.rollback(sp);
    }

    return result;
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos