I am attempting to use my portal to check a contact's field to specify which profile I would like them to posses. Right now a User can only be created if you have entered a value that matches a particular field on the contact object. If the value passed in exists on a contact record in the system - you must supply the same e-mail address attached to that contact. If all these hold true a user is created using the aforementioned contact.
Here is the code attempting to assign profile Ids before creating the user. (I will clean up the code to not use hardcoded IDs after I know this will work).
User u = new User();
u.Username = username;
u.Email = email;
u.CommunityNickname = username;
u.ContactId = c.Id;
u.LastName = c.LastName;
u.FirstName = c.FirstName;
system.debug('Preparing to assign Profile');
if(c.Value_To_Check__c!= null){
if(c.Value_To_Check__c== 'Value 1'){
system.debug('Value 2 found. Assigning profile ID');
u.ProfileId = '00ei00000017KeH';
}
else if(c.Value_To_Check__c== 'Value 2'){
system.debug('Value 2 found. Assigning profile ID');
u.ProfileId = '00ei00000014qTw';
}
else if(c.Value_To_Check__c== 'Value 3'){
system.debug('Value 3 found. Assigning profile ID');
u.ProfileId = '00ei00000017Jrq';
}
else if(c.Value_To_Check__c== 'Value 4'){
system.debug('Value 4 found. Assigning profile ID');
u.ProfileId = '00ei0000001DZOg';
}
}
String accountId;
if(c.AccountId == null){
accountId = PORTAL_ACCOUNT_ID;
system.debug('Using default portal account ID');
}
else{
accountId = c.AccountId;
system.debug('Using contacts account Id: ' + c.AccountId);
}
String userId;
try{
system.debug('Creating portal user');
userId = Site.createPortalUser(u, accountId, password);
system.debug('Portal user created: ' + userId);
}
Currently it keeps creating users with the default profile type instead of the ones specified by u.ProfileId. I am seeing that the debug log is printing out the "Value X found. Assigning profile Id."
Anyone come across something like this? Is it even possible to do what I am doing. I cannot take off the default profile setting or else self-registration is not possible.
Thanks in advance.