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
NikithaNikitha 

Hi, I m new to salesforce. I need a code. create a user whenever lead is converted into contact. Please help me I'm trying but it is not working

ShirishaShirisha (Salesforce Developers) 
Hi Swathi,

Greetings!

You need to write the after update trigger on Lead to create the user and please find the sample code to create the user as below:
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator' limit 1];
list<User> usr = new list<User>();
User u = new User(Alias = 'st', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='ABC', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id , CustomUserName__c = 'Test name',
TimeZoneSidKey='America/Los_Angeles', Username='systemad@testorg.com',
CommunityNickname = 'Test');
usr.add(u);
insert u;
You need to either query the user or hard code the profile to create the user.

Reference:https://developer.salesforce.com/forums/?id=906F000000092p2IAA

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
Siva Poreddy 11Siva Poreddy 11
@Swathi A 8

You can achieve this with Apex triggr. You can create static users with the help of underneath code, play with this code and think how to create dynamic users!!

*************** Apex class *************

public class createUserWhenLeadConvertInoContact {
    public static void createUser(){
        Profile objProfile = [SELECT Id FROM Profile WHERE Name = 'Standard Platform User' LIMIT 1];
        //Add User Information
        User objUser = new User();
        objUser.LastName = 'Swathi';
        objUser.Alias = 'Swathi';
        objUser.Email = 'Swathi@gmail.com';
        objUser.Username = 'SwathiSwathiSwathi@gmail.com';
        objUser.ProfileId = objProfile.id;
        objUser.TimeZoneSidKey = 'GMT';
        objUser.LanguageLocaleKey = 'en_US';
        objUser.EmailEncodingKey = 'UTF-8';
        objUser.LocaleSidKey = 'en_US';
        //Insert User
        Insert objUser;
        system.debug('@@@@@@@@@objUser' +objUser);
    }
}

************ APEX Trigger *************

trigger createUserWhenLeadConvertInoContact on Lead (after insert, after update) {
    
    List<Lead> ldObj = new List<Lead>();
    if((trigger.isInsert || trigger.isUpdate) && trigger.isAfter){
        for(Lead leadObj: trigger.new){
            if(leadObj.status == 'Closed - Converted'){
                ldObj.add(leadObj);
                system.debug('@@@@@@@@@@oldLead' + ldObj);
            }
        }
    }
    if(ldObj.size()>0){
        createUserWhenLeadConvertInoContact.createUser(); 
    }    
}


Regards,
 
NikithaNikitha
Hii I tried the code but I m getting err like this
TriggeronContact: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, only portal users can be associated to a contact: []

my code is


trigger TriggeronContact on Contact (After insert) {
    Set<ID> contactids = new Set<ID>();
    for(Contact c : Trigger.new){
        if(c.LastName != null ){
            Contactids.add(c.Id);
        }
    }
    List<Contact> conlist =[Select id, email,FirstName,LastName  from Contact where Id IN : contactids];
    List<User> userList = new List<User>();
    for(Contact con : trigger.new){
        string nick = con.email!=null?con.email.substring(0, con.email.indexOf('@')):''; 
        nick += Datetime.now().getTime();
        List<Profile> profileList = [Select Id from Profile where Name=: 'System Administrator' limit 1];
        
        User newuser = new User(Alias = con.FirstName,
                                email = con.Email,
                                emailencodingkey = 'UTF-8',
                                firstname = con.FirstName,
                                lastname = con.LastName,
                                localesidkey = 'en_US',
                                contactId = con.Id,
                                timezonesidkey='America/Los_Angeles',
                                IsActive = true,
                                username = con.Email,
                                CommunityNickname = nick,
                                ProfileId =  profileList[0].Id,
                                LanguageLocaleKey = 'en_US');
                          userList.add(newuser);
}
    insert userList;

}

what I want to do to clear this error message!
Siva Poreddy 11Siva Poreddy 11
hi Swathi,

Please refer https://developer.salesforce.com/forums/?id=9060G0000005kOoQAI


Regards,