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
roni shoreroni shore 

Unable to insert community user

Hi guys, I am trying to insert community user through but its failing.
    public static void createCommunityUser(Set<Id> contactIdSet){
        List<Contact> contactList = [SELECT 
                                           FROM Contact 
                                           WHERE Id IN: contactIdSet];
        List<User> userList = new List<User>();
        List<Profile> profileList = [SELECT 
                                            FROM Profile 
                                            WHERE Name = 'Customer Community User' 
                                            LIMIT 1];
        List<UserRole> roleList = [SELECT 
                                        FROM UserRole 
                                        WHERE PortalType = 'None'
        for(Contact contactObj : contactList){
            User uObj = new User();
            uObj.LastName = contactObj.LastName;
            uObj.ContactId = contactObj.Id;
            uObj.Username = contactObj.Email;
            uObj.Email = contactObj.Email;
            String lstName = contactObj.LastName;
            uObj.ProfileId = profileList[0].Id;
            uObj.UserRoleId = roleList[0].Id;
            uObj.IsActive = true; 
            uObj.TimeZoneSidKey = 'GMT';
            uObj.LanguageLocaleKey = 'en_US';
            uObj.EmailEncodingKey = 'UTF-8';
            uObj.LocaleSidKey = 'en_US';
            uObj.CommunityNickname = contactObj.LastName;
            String identifier = contactObj.Email;
            //uObj.UserType = 'CustomerSuccess';               
            /* Insert the user record */
            Insert userList;  
        }Catch(Exception e){
            System.debug('Error Occured at Community User Creation : '+e.getMessage());
        System.debug('Community Users ->'+userList);
   please suggest
Waqar Hussain SFWaqar Hussain SF
What are you getting the debug logs?
roni shoreroni shore
role type must match user type