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
sugandhasugandha 

How to add data entered into visual force page into custom object

Hi everyone ,

I am new to force.com. I am creating this risk calculating application. I want to save the value of username and role taken from user into a custom object .

My custom object has two fields :-

loginuser and 

 

 The controller is :_

 

 

**
* An apex class that creates a portal user
*/
public with sharing class SiteRegisterController1 {
// 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.
private static Id PORTAL_ACCOUNT_ID = '0019000000PH5yx';
String[] Roles = new String[]{};

public SiteRegisterController1 () {
}
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 fullName{get;set{fullName = value == null ? value : value.trim(); } }
public String Rolees{get;set;}


public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Administrator','Administrator'));
options.add(new SelectOption('Supervisor','Supervisor'));
options.add(new SelectOption('Portfolio Manager','Portfolio Manager'));
return options;
}
public String[] getRoles() {
return roles;
}

public void setroles(String[] roles) {
this.roles = roles;
}

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 = fullName;

role_redirect_table t=new role_redirect_table();

t. loginuser=username;

t.role_specified_in_login=rolees;



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;
}

// Test method to bring this class's test coverage over the required 75%
@IsTest(SeeAllData=true) static void testRegistration() {
SiteRegisterController1 controller = new SiteRegisterController1();
controller.username = 'test@force.com';
controller.email = 'test@force.com';
controller.fullName = 'test';
// registerUser will always return null when the page isn't accessed as a guest user
System.assert(controller.registerUser() == null);

controller.password = 'abcd1234';
controller.confirmPassword = 'abcd123';
System.assert(controller.registerUser() == null);
}
}

 

 

I want to save the roles and username into the custom object that i have created.. i have highlighted the code. Kindly help.

AshlekhAshlekh

Hi

I think you have to provide all the required field on user object and than pass it to Site.createPortalUser(u,Accid, password);

 

On user object you have to provide alias and email and some other fields

 

Thanks

Ashlekh

sugandhasugandha

can you explain me more in this... i am not able to understand..

AshlekhAshlekh

Hi,

 

My mean that when you are creating user instance and providing value to user instance than you have to provide all the value to required field on user Object than you pass that user instance into site.createuser method.

 

If you facing problem than provide me your apexpage and controller codee.

 

Thanks

Ashlekh

Avidev9Avidev9

If I understand the requirement properly.

You want to insert the records in custom object by this piece of highlighted code , the object api name seems to be incorrect "role_redirect_table"

 

 

role_redirect_table t=new role_redirect_table();
t. loginuser=username;
t.role_specified_in_login=rolees;

 Custom object api name ends with "__c" always

 

so lets assume my object name is "userlog__c" and I have two fields "UserName__c" and "Email__c"

 

so the code will be

 

UserLog__c log = new UserLog__c();
log.username__c = username;
log.email__c = email;
log.Name = fullName; insert log; //this line inserts a new records

 

Modify the code for your use