You need to sign in to do that
Don't have an account?
Bohao
Site.createPortalUser() always return null userId
I have a site for self-registration which calls Site.createPortalUser() to create a new portal user if it is a new user. It works perfectly on site registration page. The problem now is that I have to capture facebook login and create portal user for facebook users. Current code has callout function to get facebook user's email, userid and username, etc. And then, pass those info to the same function used for site registration page. All parameters are correct, just don't know why Site.createPortalUser() always give me null userId? Anyone has any idea about this?
Problem solved. In order to call createportaluser function, caller should be inside a form with forceSSL=true. otherwise, userid will return null when u call createportaluser function.
All Answers
Hi Bohao,
The function "createPortalUser" - needs the following parametrs:-
Creates a portal user for the given account and associates it with the site's portal.
The optional opt_password argument is the password of the portal user. If not specified, or if set to null or an empty string, this method sends a new password email to the portal user.
The optional opt_sendEmailConfirmation argument determines whether a new user email is sent to the portal user. Set it to true to send a new user email to the portal user. The default is false, that is, the new user email isn't sent.
The nickname field is required for the user sObject when using thecreatePortalUser
method.
Hi Ispita
Thank you for replying me. I am using same same code for my project. And I checked all parameters including nickname and accountId, and left password empty string. Basically, the parameters I am using are in the same format as site registration page. And there is no duplicate user that has been created before. The only difference is that registration page has 'Register' button to call createPortalUser() function and facebook login is calling createPortalUser() inside another apex function.
Problem solved. In order to call createportaluser function, caller should be inside a form with forceSSL=true. otherwise, userid will return null when u call createportaluser function.
Thanks, this helped.
i am creating from trigger and it is nor working.Is it the issue of forceSSL??
for my case, i am creating portal user in trigger, so i am not able to use the site.createportalUser, si i re-implemented the standard function.
In that case how to create user by javascript remoting? if we are using an html form instead of apex form
public with sharing class SiteRegisterController {
// PORTAL_ACCOUNT_ID is the account on which the contact will be created on and then enabled as a portal user.
// you need to add the account owner into the role hierarchy before this will work - please see Customer Portal Setup help for more information.
@TestVisible
private static Id PORTAL_ACCOUNT_ID = '001x000xxx35tPN';
public SiteRegisterController () {
}
public String username {get; set;}
public String email {get; set;}
public String password {get; set {password = value == null ? value : value.trim(); } }
public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
private boolean isValidPassword() {
return password == confirmPassword;
}
public PageReference registerUser() {
// it's okay if password is null - we'll send the user a random password in that case
if (!isValidPassword()) {
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
ApexPages.addMessage(msg);
return null;
}
User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = communityNickname;
String accountId = PORTAL_ACCOUNT_ID;
// lastName is a required field on user, but if it isn't specified, we'll default it to the username
String userId = Site.createPortalUser(u, accountId, password);
if (userId != null) {
if (password != null && password.length() > 1) {
return Site.login(username, password, null);
}
else {
PageReference page = System.Page.SiteRegisterConfirm;
page.setRedirect(true);
return page;
}
}
return null;
}
}
i know its late but for helping others i will try to answer this,
so what you can do is:
as site.createPortalUser will return null so you can apply a condition like:
String userId = Site.createPortalUser(u, accountId, password);
if(test.isRunningTest()){
userid='temp';
}
if (userId != null) {
if (password != null && password.length() > 1) {
return Site.login(username, password, null);
}
else {
PageReference page = System.Page.SiteRegisterConfirm;
page.setRedirect(true);
return page;
}
and it will help to increase the coverage