function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RockersRockers 

How to create a custom portal user through apex?

Hi,

 

      I need to create a custom portal user through apex. Actually, this user will create through contact object. But, i need to customize it. so, is there any chance to create a user through apex and that user should be the custom portal user.

 

      Give me suggestions about this, how to create a user throught apex.

 

     Thanks in advance..........

 

Regards,

Phanikumar

Navatar_DbSupNavatar_DbSup

Hi,
If you are creating the customer portal user through internally then you can below sample code to create a customer portal user.
--------------------- Customer Portal User created internally-----------------


Profile pf = [Select id,name from Profile where name ='Volunteer' limit 1];


account acc = [select name from accout where name="individual"];


Contact con = [select id,name,email,lastname,firstname,accoundid from contact where accountid=:accid limit 1];


User u2 = new User(contactId=con.Id, username=con.Email, firstname=con.FirstName,
lastname=con.LastName, email=con.Email,communityNickname = con.LastName + '_' + Rnd,
alias = string.valueof(con.FirstName.substring(0,1) + con.LastName.substring(0,1)), profileid = pf.Id, emailencodingkey='UTF-8',
languagelocalekey='en_US', localesidkey='en_US', timezonesidkey='America/Los_Angeles');
Database.DMLOptions dlo = new Database.DMLOptions();
dlo.EmailHeader.triggerUserEmail= true;
Database.saveresult sr = Database.insert(u2,dlo);
--------------------- Customer Portal User through sites-----------------
Public string Email{get;set;}
Public string FirstName{get;set;}
Public string LastName{get;set;}
Public string CommunityNickname{get;set;}
Public string Passwd{get;set;}
account acc = [select name from accout where name="individual"];
User u = new User();
u.Username = Email;
u.FirstName = FirstName;
u.LastName = LastName;
u.Email = Email;
// u.IsPortalSelfRegistered = true;
u.CommunityNickname = communityNickname;
String pwdd = Passwd;
system.debug('<< before create user >>');
String userId = Site.createPortalUser(u, acc.id, pwdd); // This method create a customer portal user

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

RockersRockers

Hi Jain,

 

      Thank you very much for your valuable response. Let me check and i will let you know, if there are any issues.

 

Regards,

Phanikumar