You need to sign in to do that
Don't have an account?
NVN Dev
Site.createExternalUser(u, accountId, password) returning null
Hello All,
While I am creating community through apex, Site.createExternalUser(u, accountId, password) is returning null
Can anyone help me out?
While I am creating community through apex, Site.createExternalUser(u, accountId, password) is returning null
Can anyone help me out?
I have used, forceSSL=true.
Is there anything else which i need to be looked at?
Thanks,
Naveen
try {
System.debug('--in try---');
System.debug('---user---'+u);
userId = Site.createExternalUser(u, accountId, password);
System.debug(Site.createExternalUser(u, accountId, password));
} catch(Site.ExternalUserCreateException ex) {
List<String> errors = ex.getDisplayMessages();
for (String error : errors) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));
}
System.debug(ex.getMessage());
}
You should debug the Id with:
That being said, it's likely you already created the user if you've been testing this awhile. Add some System.debugs to the catch bracket as well. Try debugging ex.getMessage() to see if that gives you anything.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_Site_exceptions.htm
public PageReference registerUser() {
System.debug('--method called---');
// it's okay if password is null - we'll send the user a random password in that case
String profileId = '00e28000000x7rk'; // To be filled in by customer.
String roleEnum = 'Customer User'; // To be filled in by customer.
String accountId = '001280000093OaC'; // To be filled in by customer.
String userName = email;
User u = new User();
u.Username = 'naveen@portal.com';
u.Email = 'naveen.sfdc21@gmail.com';
u.FirstName = 'Naveen';
u.LastName = 'Kakrla';
u.CommunityNickname = 'naveen.sf21';
u.ProfileId = profileId;
String userId;
try {
System.debug('--in try---');
System.debug('---user---'+u);
userId = Site.createExternalUser(u, accountId, password);
System.debug(userId);
} catch(Site.ExternalUserCreateException ex) {
System.debug('--catch---');
List<String> errors = ex.getDisplayMessages();
for (String error : errors) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));
}
System.debug(ex.getMessage());
}
if (userId != null) {
if (password != null && password.length() > 1) {
System.debug('--in if password---');
return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
}
else {
System.debug('--in else password---');
PageReference page = System.Page.CommunitiesSelfRegConfirm;
page.setRedirect(true);
return page;
}
}
return null;
}
The problem for me was entering a password that does not match the org password requirements.
createExternalUser will not throw an exception, but will just return null in this case.
Credit goes to: AvailableName (https://salesforce.stackexchange.com/a/228106/53505)
Are you write the test class the above code. I am writing test calss it covers only 76% , Can any one have to write the test class,
Please share the comment below.