+ Start a Discussion
Pavan Kumar 1141Pavan Kumar 1141 

how to create a user when a contact is created using trigger

Hi Team, Please help me on how to create a user when a contact is created using trigger.
Raj VakatiRaj Vakati
Try like this
 
trigger createUserfromContact on Contact (after update) {     
    List <User> newUsers = new List<User>();
	Profile pf= [Select Id from profile where Name='System Administrator'];

    for(Contact c : Trigger.NEW){
                
			    User userInsert = new User() ;
                userInsert.FirstName =c.FirstName!=null?c.FirstName:'';
                userInsert.LastName =c.LastName;
                String aliesKey ='';
                if(c.FirstName!=null && c.FirstName!=''){
                    aliesKey = aliesKey+c.FirstName.trim().substring(0,1);
                    if(c.LastName.length()>7){
                        aliesKey =aliesKey+c.LastName.trim().substring(0,6) ; 
                    }else{
                        aliesKey = aliesKey+c.LastName.trim() ; 
                    } 
                }else{
                    if(c.LastName.length()>7){
                        aliesKey = aliesKey+c.LastName.trim().substring(0,7);
                    }else{
                        aliesKey =  c.LastName.trim();
                    }
                }
                userInsert.Alias = aliesKey;
                userInsert.Email = c.Email;
                if(org.IsSandbox){
                    userInsert.Username = c.Email+'.'+'sandbox';
                    
                }else{
                    userInsert.Username = c.Email;
                }
                userInsert.ProfileId = pf.Id;
                userInsert.CommunityNickname =(c.FirstName!=null ? c.FirstName : '')+c.LastName+userUniqueNumber ;
                userInsert.TimeZoneSidKey = strTimeZoneKey;
                userInsert.Phone =c.Phone;
                userInsert.LanguageLocaleKey = strLanguageLocaleKey;
                userInsert.EmailEncodingKey =strEmailEncodingKey;
                userInsert.LocaleSidKey = strLocaleSidKey;
                userInsert.contactId=c.Id;   
				newUsers.add(userInsert);
            }  
            
			
            
        
    }   
    insert newUsers;
}

 
Raj VakatiRaj Vakati
Please use this code
 
trigger createUserfromContact on Contact (after update) {     
    List <User> newUsers = new List<User>();
	Profile pf= [Select Id from profile where Name='System Administrator'];
	Organization org = [Select Id , IsSandbox from Organization Limit 1]

    for(Contact c : Trigger.NEW){
                
			    User userInsert = new User() ;
                userInsert.FirstName =c.FirstName!=null?c.FirstName:'';
                userInsert.LastName =c.LastName;
                String aliesKey ='';
                if(c.FirstName!=null && c.FirstName!=''){
                    aliesKey = aliesKey+c.FirstName.trim().substring(0,1);
                    if(c.LastName.length()>7){
                        aliesKey =aliesKey+c.LastName.trim().substring(0,6) ; 
                    }else{
                        aliesKey = aliesKey+c.LastName.trim() ; 
                    } 
                }else{
                    if(c.LastName.length()>7){
                        aliesKey = aliesKey+c.LastName.trim().substring(0,7);
                    }else{
                        aliesKey =  c.LastName.trim();
                    }
                }
                userInsert.Alias = aliesKey;
                userInsert.Email = c.Email;
                if(org.IsSandbox){
                    userInsert.Username = c.Email+'.'+'sandbox';
                    
                }else{
                    userInsert.Username = c.Email;
                }
                userInsert.ProfileId = pf.Id;
                userInsert.CommunityNickname =(c.FirstName!=null ? c.FirstName : '')+c.LastName ;
                userInsert.TimeZoneSidKey = 'GMT';
                userInsert.Phone =c.Phone;
                userInsert.LanguageLocaleKey = 'en_US';
                userInsert.EmailEncodingKey ='ISO-8859-1';
                userInsert.LocaleSidKey = 'en_US';
                userInsert.contactId=c.Id;   
				newUsers.add(userInsert);
            }  
            
			
            
        
    }   
	InsertUser.updateUser(newUsers)
}
 
public class InsertUser{
    @future
    public static void updateUser(List<User> useridsTobeActivated){
		insert useridsTobeActivated ;
	}
	
}

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Pavan
 
trigger createUserfromContact on Contact (after insert) {    
 List <User> newUsers = new List<User>();
 Profile pf= [Select Id from profile where Name='System Administrator'];
 for(Contact c : Trigger.NEW){
                
	 User userInsert= new User() ;
            userInsert.FirstName =c.FirstName!=null?c.FirstName:'';
            userInsert.LastName =c.LastName;
            userInsert.Email=c.Email;
            userInsert.Username = c.Email;
            userInsert.ProfileId = pf.Id;
              
	newUsers.add(userInsert);
            } 
if(newUsers.size()>0) 
Insert newUsers;
}

The above solution might work for you .Please try and let us know.

Thanks
Bhargavi.