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
Aashi AditiAashi Aditi 

Field is not writeable: User.Name

public with sharing class UserName {
 public static void accountMethod(List<Contact> con )
    {
       List<User> newUsers = new List<User>();
    Profile p = [select id from Profile where name='Tester'];
    Map<String,Contact> contactsByNameKeys = new Map<String,Contact>();
           for (Contact c : con) 
    {
        if(c.firstname == null){
           //Show error for record not having any employees
           c.addError('Contact must have first name');
        }
        String userName = c.firstname + ' ' + c.lastname;
        contactsByNameKeys.put(userName,c);
        User a = new User(name=userName, profileid = p.id);
        newUsers.add(a);
    }
        insert newUsers;
    }
    }

Trigger to create New User on the basis of Contact’s FirstName , Contact’s Last Name.
For This :
Create new Profile (May be clone the System Admin Profile). And use this profile for tagging on User level.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Aashi,

>> https://developer.salesforce.com/forums/?id=906F000000092LKIAY

As stated in the above link can you try specifying the firstname and lastname rather than the name - a bit like contacts. While there is a name field available via the API, it's not mentioned in the fields reference guide:

http://login.salesforce.com/help/doc/en/salesforce_field_names_reference.pdf

I suspect you'll need to populate some more fields to add a user - profile for example. The fields I usually set up are:

alias
email
emailencodingkey
lastname
languagelocalekey
localesidkey
profileid
timezonesidkey
username

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Aashi AditiAashi Aditi

public class UserName {
  public static void userMethod(List<Contact> con )
    {
        Profile p = [Select Id, Name from Profile where Name ='Tester' LIMIT 1]; 
        List<User> newUsers = new List<User>();
        for (Contact c : con) 
        {
       User a = new User();
       a.UserName = c.Email+'-POC';
       a.ContactId = c.Id;
       a.ProfileId = p.id;
       a.Email = c.Email;
       a.EmailEncodingKey = 'UTF-8';        
       a.FirstName = c.FirstName;
       a.LastName = c.LastName;
       a.TimeZoneSidKey = 'America/Los_Angeles';
       a.LocaleSidKey = 'en_US';
       a.LanguageLocaleKey = 'en_US';
       if (c.LastName.length() >= 8){
            a.Alias = c.LastName.substring(0, 7);    
        }else{
            a.Alias = c.LastName;
        }   
       newUsers.add(a);
       }
    }
}
trigger insertaccount on Contact (after insert) 
{
    if(trigger.isafter && trigger.isinsert)
       {
        UserName.userMethod(Trigger.new);
       }
    
}

Still I'm unable to see user in particular profile 'tester'. I'm adding this in trigger class as well.
please help.