You need to sign in to do that
Don't have an account?
Tristan P
Automatically create Community user when Contact is created
Hi all,
I'm trying to find out how I can automatically create a Community user when a Contact is created. I've had a look around and I think this could be done using an Apex class (?) as a part of a workflow but this is something I'm completely new to, so I haven't got any idea where to start. Is anyone able to give me any guidance here?
Thanks,
Tristan
Best Regards,
Sandhya
All Answers
Below is the sample code for the same.
Also refer below links.
https://developer.salesforce.com/blogs/developer-relations/2014/06/how-to-provision-salesforce-communities-users.html
https://stackoverflow.com/questions/20787497/automate-creating-communities-user
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Sandhya
Best Regards,
Sandhya
I have Error
Review the errors on this page.
CreateCommunityUser: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): User, original object: Contact: [] Trigger.CreateCommunityUser: line 16, column 1
trigger CreateCommunityUser on Contact (After insert) {
if(Trigger.isInsert){
for(Contact co : trigger.new){
Contact con = [select id,email,firstName,lastname,accountId from Contact where Id =:co.Id];
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = false;
dmo.EmailHeader.triggerOtherEmail = false;
dmo.EmailHeader.triggerAutoResponseEmail = false;
dmo.optAllOrNone = false;
// create portal user
string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):'';
nick += Datetime.now().getTime();
User newUser1 = new User(alias=con.firstName, email = con.email, emailencodingkey = 'UTF-8', firstname = con.firstName, lastname = con.lastname, languagelocalekey = 'en_US',localesidkey = 'en_US',contactId = con.Id,timezonesidkey = 'Asia/Dubai',username = con.email,CommunityNickname = nick,ProfileId ='00e0w000000InE5AAK', IsActive = true , UserRoleId ='00E0w000000gA8YEAU');
newUser1.setOptions(dmo);
insert newUser1;
}
}
}
I am facing the same issue ,did you find any solution?