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
Prav PPrav P 

Create user from Contact Trigger

Hi,

Im trying to create user from contact object by using trigger and method. However, Im getting the below error , could you please help

Variable does not exist: CreateNewUserHandler
Missing  at 'public'


trigger NewUser on Contact (after insert) {

    List<User> userList = new List<User>();
    Set<Id> contactIds = new Set<Id>();
    for(Contact c: Trigger.New){
       if(c.Agent_type__c == 'Member'){
            contactIds.add(c.id);
        }
    }

    if(contactIds.size() > 0){
        CreateNewUserHandler.createNewUserFromContact(contactIds);
    }
}
public with sharing class CreateNewUserHandler {


public static void CreateNewUserHandler(Set<Id> contactIds)
{
    List<User> userList = new List<User>();
    List<Profile> profileList = [Select Id from Profile where Name=: 'Customer Community User' limit 1];
    //List<UserRole> roleList = [Select Id from UserRole where Name=: 'CEO' limit 1];
    for(Contact contactObj : [Select Id, Name, Email,Username__c from Contact where Id IN: contactIds limit 50000]){
        User uObj = new User();
        uObj.Username = contactObj.Username__c;
        uObj.Email = contactObj.Email;
        uObj.Alias = contactObj.Name;
        uObj.UserRoleId = roleList[0].Id;
        uObj.ProfileId = profileList[0].Id;
        uObj.IsActive = true; 
        uObj.TimeZoneSidKey = 'GMT';
        uObj.LanguageLocaleKey = 'en_US';
        uObj.EmailEncodingKey = 'UTF-8';
        uObj.LocaleSidKey = 'en_US';
        uObj.ContactId = contactObj.Id;
        userList.add(uObj);
    }
    try{
           insert userList;  // insert the user record
    }catch(Exception e){
          // Catch Exception
     }
}
Maharajan CMaharajan C
Hi,

Your createNewUserFromContact method from trigger but in handler class there is no method named createNewUserFromContact.

Please update the method name in handler class.

Trigger:
 
trigger NewUser on Contact (after insert) {
    List<User> userList = new List<User>();
    Set<Id> contactIds = new Set<Id>();
    for(Contact c: Trigger.New){
        if(c.Agent_type__c == 'Member'){
            contactIds.add(c.id);
        }
    }
    if(contactIds.size() > 0){
        CreateNewUserHandler.createNewUserFromContact(contactIds);
    }
}

Handler Class:
 
public with sharing class CreateNewUserHandler {
    
    public static void createNewUserFromContact(Set<Id> contactIds)
    {
        List<User> userList = new List<User>();
        List<Profile> profileList = [Select Id from Profile where Name=: 'Customer Community User' limit 1];
        List<UserRole> roleList = [Select Id from UserRole where Name=: 'CEO' limit 1];
        for(Contact contactObj : [Select Id, Name, Email,Username__c from Contact where Id IN: contactIds limit 50000]){
            User uObj = new User();
            uObj.Username = contactObj.Username__c;
            uObj.Email = contactObj.Email;
            uObj.Alias = contactObj.Name;
            uObj.UserRoleId = roleList[0].Id;
            uObj.ProfileId = profileList[0].Id;
            uObj.IsActive = true; 
            uObj.TimeZoneSidKey = 'GMT';
            uObj.LanguageLocaleKey = 'en_US';
            uObj.EmailEncodingKey = 'UTF-8';
            uObj.LocaleSidKey = 'en_US';
            uObj.ContactId = contactObj.Id;
            userList.add(uObj);
        }
        try{
            insert userList;  // insert the user record
        }catch(Exception e){
            system.debug(' Exception --> ' + e.getLineNumber());
            system.debug(' Exception --> ' + e.getMessage());
        }
    }
    
}

Thanks,
Maharajan.C
CharuDuttCharuDutt
Hii Pav
Try Below Code
trigger UserFromContact on Contact (after insert) {
  
    Set<Id> conId= new Set<Id>();
    for(Contact Con: Trigger.New){

        if(Con.Agent_type__c == 'Member'){
            conId.add(Con.id);

        }
    }
    if(conId.size() > 0){
        CreateNewUserHandler.createNewUserFromContact(conId);
    }
}

public with sharing class CreateNewUserHandler {
    
    public static void createNewUserFromContact(Set<Id> conId)
    {
        List<User> userList = new List<User>();
        List<Profile> profileList = [Select Id from Profile where Name=: 'Customer Community User' limit 1];
        List<UserRole> roleList = [Select Id from UserRole where Name=: 'CEO' limit 1];
        for(Contact contactObj : [Select Id,Name,FirstName,LastName,Email,Username__c from Contact where Id IN: conIdlimit 50000]){
           User usr = new User(LastName = Con.LastName,
                            FirstName= Con.FirstName,
                            Alias = Con.Name,
                            Email = Con.Email,
                            Username = Con.Username__c,
                            IsActive = true, 
                            ProfileId = p.id,
                            TimeZoneSidKey = 'GMT',
                            LanguageLocaleKey = 'en_US',
                            EmailEncodingKey = 'UTF-8',
                            LocaleSidKey = 'en_US'
                           );
            userList.Add(usr);
        }
                    insert userList;  
    } 
}


TEST CLASS
@isTest
public class CreateNewUserHandlerTest {
@isTest
    Public Static Void UnitTest(){
        Account Acc = new Account();
        Acc.Name='Tesst Acc';
        insert Acc;
        
        Contact Con = new Contact();
        Con.FirstName = 'Test';
        Con.LastName = 'Con';
        Con.Email = 'Test123@Test.Com';
        Con.Username__c = 'Test123@Test.Com';
        Con.AccountId = Acc.Id;
        Insert Con;
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 

 
Prav PPrav P
Thanks CharuDutt. I tired the above code and still getting the same error
Suraj Tripathi 47Suraj Tripathi 47

Hi,

Please edit your code like the one below. Refresh your console and try again

if(contactIds.size() > 0){
        CreateNewUserHandler.CreateNewUserHandler(contactIds);
    }

if still, you are getting errors then try the below

create a new class(not with or without sharing) and new methods and then call them in the trigger

If your solution is solved please mark it as the Best Answer

Thank You

 

CharuDuttCharuDutt

@Pav 

Try Refreshing Dev Console Window Try And Save Again