This is what worked for me Marty. There is a bit of additional logic from our business use case. But you should be able to pull that out fairly easily.
I didn't add any code for the page, but you don't seem to have any confusion about getting data back and forth from your controller and page.
public void modifyUserConfirm() {
createUser();
Database.DMLOptions dlo = new Database.DMLOptions();
dlo.EmailHeader.triggerUserEmail = true;
try {
Database.SaveResult saveResults;
if (isNewUser && activeUsers.size() >= maximumNumberOfUsers) {
throw new DmlException('You have exceeded the amount of active users your organization is allotted.');
} else {
saveResults = Database.update(user, dlo);
}
for (Database.Error result : saveResults.getErrors()) {
throw new DmlException(result.getMessage());
}
if (isNewUser) activeUsers.add(user);
} catch (DmlException e) {
System.debug(e);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
if (isNewUser && !Test.isRunningTest()) delete contact;//remove contact record if user is not created.
} catch (Exception e) {
System.debug(e);
}
}
private Contact createContactForUser() {
contact = new Contact(
AccountId = userAccountId,
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
RecordTypeId = aRecordType
);
insert contact;
return contact;
}
private void createUser() {
if (isNewUser) {
contact = createContactForUser();
User newUser = new User(
FirstName = user.FirstName,
LastName = user.LastName,
UserName = user.UserName,
Email = user.Email,
Phone = user.Phone,
Title = user.Title,
ProfileId = selectedProfileId,
ContactId = contact.Id
);
user = newUser;
}
user.LanguageLocaleKey = 'en_US';
user.LocaleSidKey = 'en_US';
user.TimeZoneSidKey = 'America/Chicago';
user.EmailEncodingKey = 'UTF-8';
user.Phone = FormatPhone(user.Phone);
user.Alias = user.FirstName.substring(0, 1) + user.LastName.substring(0, 1);
user.CommunityNickname = user.FirstName.substring(0, 1) + user.LastName;
}
@future
method?@future
annotation.saveContact()
andsaveUser()
. I tried two approaches based on @JesseMilburn's suggestion: 1) callsaveContact()
as the first operation insaveUser()
, and 2) use a third action methodsavePortalUser()
which calls the previous two methods in sequence. Same problem still exists.