0

I am able to create a new community user internally but when i try to create a user from filling the custom registration page from incognito its throwing the error

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Profile]: [Profile]

How to resolve this issue.

Registration controller apex class:-

public class Sign_Up_Controller {
@AuraEnabled
public static String registerUser(String lname, String fname,String email, String uname, String alias, String password){
    system.debug('lname'+lname);
    system.debug('fname'+fname);
    system.debug('email'+email);
    system.debug('uname'+uname);
    system.debug('alias'+alias);

    Account acc = new Account(Name = fname +' '+lname);
    insert acc;
    Account parentAcc = [select id from Account where Name =: acc.Name limit 1];

    Contact con = new Contact(FirstName = fname, LastName = lname, AccountId = parentAcc.id );
    insert con;

    // UserRole usrRole = [select id from UserRole where Name = 'COO'];

    Contact childCon = [select id from contact where FirstName =:fname AND LastName = :lname limit 1];
    Profile profileId = [SELECT Id FROM Profile WHERE Name = 'CustomerCommunityLoginClone' LIMIT 1];
    String nickname = ((fname != null && fname.length() > 0) ? fname.substring(0,1) : '' ) + lname.substring(0,1);
    nickname += String.valueOf(Crypto.getRandomInteger()).substring(1,7);
    User usr = new User(LastName = lname,
                        FirstName=fname,
                        Email = email,
                        Username = uname,
                        ProfileId = profileId.id,
                        TimeZoneSidKey = 'GMT',
                        LanguageLocaleKey = 'en_US',
                        EmailEncodingKey = 'UTF-8',
                        LocaleSidKey = 'en_US',
                        ContactId = childCon.id,
                        Alias = alias,
                        //PortalRole = 'COO',
                        CommunityNickname = nickname);
    insert usr;        
    String userId;
    try {
        userId = Site.createExternalUser(usr, parentAcc.id, password);
    } catch(Site.ExternalUserCreateException ex) {
        system.debug('Exceptions: '+ex.getDisplayMessages()); 

        // This message is used for debugging. Do not display this in the UI to the end user.
        // It has the information around why the user creation failed.
        System.debug(ex.getMessage());

    }

    if (userId != null) { 
        if (password != null && password.length() > 1) {
            system.debug('Get current url');
            return null;
        }
        else {
            system.debug('Get current url'+System.Page.CommunitiesSelfRegConfirm);
            return String.valueOf(System.Page.CommunitiesSelfRegConfirm);
        }
    }
    else{
        return null;
    }
}

}

  • Are you using developer edition? – gs650x Sep 9 at 12:54
  • Yes, i am doing it in developer org. – alok jha Sep 9 at 13:04
  • While creating a user, you need to mention the profile id. – rahul gawale Sep 9 at 13:17
  • I have already assigned the profile id. – alok jha Sep 9 at 13:19
  • have you configured members setting for the community? Also please add the custom page code here. – rahul gawale Sep 9 at 13:59

Your Answer

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

Browse other questions tagged or ask your own question.