+ Start a Discussion
McFlyMcFly 

Register a user with creating a new account

With our site we would like to provide a registration process to new users in such way that a new account and an according contact is created each time a new user registers. As a result after the successful registration there should be a new contact that belongs to a new account and this contact is linked to a newly created user.

 

Is that possible and how can we achieve that?

The documentation we found http://wiki.developerforce.com/index.php/Authenticating_Users_on_Force.com_Sites just provides an example where all newly created users and contacts are tied to one account.

Best Answer chosen by Admin (Salesforce Developers) 
HydaspisChaosHydaspisChaos

 


McFly wrote:

With our site we would like to provide a registration process to new users in such way that a new account and an according contact is created each time a new user registers. As a result after the successful registration there should be a new contact that belongs to a new account and this contact is linked to a newly created user.

 

 

First you have to create a new account. Please be aware, that the account-owner has to belong to a role. Otherwise you will not be able to associate the new registered user to this account. So you can do something like the following (code snippet from the SiteRegisterController):

 

 

User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = communityNickname;
        
String accountOwnerId = [select id from User where $CRITERIA][0].id;

Account a = new Account(name = username, OwnerId = accountOwnerId);
insert a;
        
String userId = Site.createPortalUser(u, a.id, password);

 

 

HTH

All Answers

DevilSheetuDevilSheetu

If you have enabled the login for the sites, you just need to follow the steps as given below:

 

1. Create a page that will collect information required for your Account, contact and User e.g: Communitynickname,Account Name, contact name and Info, password etc

2. If user has filled out all the information, call the Site.CreatePortaluser method as:

 

String userId = Site.createPortalUser(user, Account.Id, Password);

 

3. Method will automatically create a new user,account and a contact associated with the Account and User

4. Now, you can get the contact info collected from pages you have created and update contact created in step 2 with same.

 

if(userId!=null)

{
                 Contact contact = getContact(user.contactId); // Retrieve the contact created by the CreatePortalUser method.

                 //Update your contact info in this contact

                contact.email = DummyContact.email; //this will be a dummy contact where you have collected your data from pages

                   //Then update the contact

                  database.update(contact);

 

 

Hope this helps you a bit.

HydaspisChaosHydaspisChaos

 


McFly wrote:

With our site we would like to provide a registration process to new users in such way that a new account and an according contact is created each time a new user registers. As a result after the successful registration there should be a new contact that belongs to a new account and this contact is linked to a newly created user.

 

 

First you have to create a new account. Please be aware, that the account-owner has to belong to a role. Otherwise you will not be able to associate the new registered user to this account. So you can do something like the following (code snippet from the SiteRegisterController):

 

 

User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = communityNickname;
        
String accountOwnerId = [select id from User where $CRITERIA][0].id;

Account a = new Account(name = username, OwnerId = accountOwnerId);
insert a;
        
String userId = Site.createPortalUser(u, a.id, password);

 

 

HTH

This was selected as the best answer
McFlyMcFly

Thank you for the quick response , it brought us to the right track! :smileyhappy: