I am using a self registration page for guest user sign up.It is showing this error Your request cannot be processed at this time. The site administrator has been alerted.As far as i think it is because of create portal user as it is not returning userid
global class SelfRegisterController {
@TestVisible
private static boolean isValidPassword(String password, String confirmPassword) {
return password == confirmPassword;
}
@TestVisible
private static boolean siteAsContainerEnabled(String communityUrl) {
Auth.AuthConfiguration authConfig = new Auth.AuthConfiguration(communityUrl,'');
return authConfig.isCommunityUsingSiteAsContainer();
}
@TestVisible
private static void validatePassword(User u, String password, String confirmPassword) {
if(!Test.isRunningTest()) {
Site.validatePassword(u, password, confirmPassword);
}
return;
}
@AuraEnabled
public static String selfRegister(String firstname ,String lastname, String email, String password, String confirmPassword, String accountId, String regConfirmUrl, String extraFields, String startUrl, Boolean includePassword) {
Savepoint sp = null;
try {
System.debug('Start of comp :: ');
sp = Database.setSavepoint();
if (lastname == null || String.isEmpty(lastname)) {
return Label.Site.lastname_is_required;
}
if (email == null || String.isEmpty(email)) {
return Label.Site.email_is_required;
}
UserRole usr =[SELECT Id, Name, PortalRole, PortalType, PortalAccountId, ParentRoleId FROM UserRole limit 1];
User u = new User();
u.Username = email;
u.put('Email',email);
u.UserRoleId = usr.id;
u.ContactId='0030r00000Jmc0TAAR';
u.FirstName = firstname;
u.LastName = lastname;
//u.ProfileId='00e6g000000ZEtV';
String networkId = Network.getNetworkId();
System.debug('networkId:: ' + networkId);
// If using site to host the community the user should not hit s1 after logging in from mobile.
if(networkId != null && siteAsContainerEnabled(Network.getLoginUrl(networkId))) {
u.put('UserPreferencesHideS1BrowserUI',true);
}
String nickname = ((firstname != null && firstname.length() > 0) ? firstname.substring(0,1) : '' ) + lastname.substring(0,1);
nickname += String.valueOf(Crypto.getRandomInteger()).substring(1,7);
u.put('CommunityNickname', nickname);
if (extraFields != null) {
List<Object> extraFieldsList = (List<Object>) JSON.deserializeUntyped(extraFields);
for (Object thisFieldObject : extraFieldsList) {
Map<String,Object> thisField = (Map<String,Object>) thisFieldObject;
Schema.SObjectField sof = Schema.SObjectType.User.fields.getMap().get((String) thisField.get('fieldPath'));
u.put(sof, thisField.get('value'));
}
}
System.debug('includePassword:: ' + includePassword);
if (includePassword) {
if (!isValidPassword(password, confirmPassword)) {
return Label.site.passwords_dont_match;
}
validatePassword(u, password, confirmPassword);
}
else {
password = null;
}
// lastName is a required field on user, but if it isn't specified, we'll default it to the username
System.debug('userId:: ' );
system.debug('accountId'+accountId);
Contact con = [select id,accountid,name from contact where accountid ='0010r00000ISTRdAAP'];
system.debug('con'+con.AccountId);
system.debug('userDetails'+u);
String userId = Site.createPortalUser(u, con.AccountId, password);
system.debug('userId'+userId);
// create a fake userId for test.
if (Test.isRunningTest()) {
userId = 'fakeUserId';
}
if (userId != null) {
if (password != null && password.length() > 1) {
ApexPages.PageReference lgn = Site.login(email, password, startUrl);
system.debug('if blocks');
if(!Test.isRunningTest()) {
aura.redirect(lgn);
}
}
else {
ApexPages.PageReference confirmRef = new PageReference(regConfirmUrl);
if(!Test.isRunningTest()) {
aura.redirect(confirmRef);
}
}
}
return null;
}
catch (Exception ex) {
Database.rollback(sp);
return ex.getMessage();
}
}
@AuraEnabled
public static List<Map<String,Object>> getExtraFields(String extraFieldsFieldSet) {
List<Map<String,Object>> extraFields = new List<Map<String,Object>>();
Schema.FieldSet fieldSet = Schema.SObjectType.User.fieldSets.getMap().get(extraFieldsFieldSet);
if(!Test.isRunningTest()) {
if (fieldSet != null) {
for (Schema.FieldSetMember f : fieldSet.getFields()) {
Map<String, Object> fieldDetail = new Map<String, Object>();
fieldDetail.put('dbRequired', f.getDBRequired());
fieldDetail.put('fieldPath', f.getFieldPath());
fieldDetail.put('label', f.getLabel());
fieldDetail.put('required', f.getRequired());
fieldDetail.put('type', f.getType());
fieldDetail.put('value', ''); // client will populate
extraFields.add(fieldDetail);
}}}
return extraFields;
}
@AuraEnabled
global static String setExperienceId(String expId) {
// Return null if there is no error, else it will return the error message
try {
if (expId != null) {
Site.setExperienceId(expId);
}
return null;
} catch (Exception ex) {
return ex.getMessage();
}
}
}
Any help would be very helpful.