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
RagsehRagseh 

How to create user dynamically

Hi All,

I have a requirement to create partner user dynamically using information from Account and contact.
I am able to create user but not able to recieve the mail(usually salesforce send out a mail to the newly created user with some link to activate the user).
Does any one have any idea to generate this mail dynamically.

Following is the sample code I used to create user.

        User newUser=new User();
        newUser.LastName=objAccount.Name;
        newUser.Email=objAccount.Dispatch_Email__c;
        newUser.Username=objAccount.Dispatch_Email__c+'.dev2';
        newUser.Alias = 'xydx'+'dev2';
        newUser.TimeZoneSidKey='GMT';
        newUser.LocaleSidKey = 'en_GB';
        newUser.EmailEncodingKey = 'ISO-8859-1';
        newUser.LanguageLocaleKey = 'en_US';
        newUser.ProfileId = '00e11000000Lwab';
        newUser.IsActive = true;
        newUser.communitynickname = objAccount.Name+'dev2';
        newUser.contactid = '0031100000K7CHY';
        insert newUser;

Thanks
Swati GSwati G
Hi,

You can use dml options while creating new user record to receive mail for newly created user.

Database.DMLOptions dlo = new Database.DMLOptions();

dlo.EmailHeader.triggerUserEmail= true;

database.insert(newUser, dlo);

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_dml_database_dmloptions.htm
Ragesh PRagesh P
Thanks Swati,
Its working.Slite change required as below

        Database.DMLOptions dml = new Database.DMLOptions();
        dml.EmailHeader.triggerUserEmail=true;
        newUser.setOptions(dml);
        insert newUser