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

createportaluser and custom contact details

I need to create a ready-to-login community user with prespecified password and corresponding contact and account. Contact and account have custom fields that need to be populated as part of this.

None of the available CreatePortalUser methods offer the ability to specify a contact, but I found some code (see link below) to manually create the user and link to a contact.

The problem I have with this code is it's not possible to set the password as part of the user insert, and adding a subsequent system.setPassword() call in the same transaction doesn't work. Weirdly, if I execute setPassword again immediately after in developer console then it works.

It seems that if the password isn't set during user insert then SFDC sends out the first time login link and won't accept a setPassword during the same transaction?

If so I can't get around it by putting the setPassword in a @future call because this code already has an @future call and you can't nest them.

Any insights or suggestions to get around this?
Best Answer chosen by davcondev
My solution was to go back to using CreatePortalUser which seems to be exempt from Mixed DML Operation rules.

Insert Account
Call CreatePortalUser specifying the Account and password
In an @future call, find the Contact that was created and update its details