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
Tristan PTristan 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 Answer chosen by Tristan P
SandhyaSandhya (Salesforce Developers) 
You need to create trigger for this on contact object and use below code
trigger NewUser 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 ='', IsActive = true);
newUser1.setOptions(dmo);
insert newUser1;
         }
     }
}

Best Regards,
Sandhya

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample code for the same.
 
Contact con = [select id,email,firstName,lastname,accountId from Contact where Id =:contactId];         

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 newUser = new User(
                    alias = createAlias(con.firstName, con.lastName), 
                    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 = .......,
                    IsActive = true);

newUser.setOptions(dmo);
insert newUser;

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
 
 
SandhyaSandhya (Salesforce Developers) 
You need to create trigger for this on contact object and use below code
trigger NewUser 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 ='', IsActive = true);
newUser1.setOptions(dmo);
insert newUser1;
         }
     }
}

Best Regards,
Sandhya
This was selected as the best answer
Bibhishan PawarBibhishan Pawar

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

Shikha ChaurasiaShikha Chaurasia
Hi Bibhishan Pawar.
I am facing the same issue ,did you find any solution?