You need to sign in to do that
Don't have an account?
Prav 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
}
}
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
}
}
Your createNewUserFromContact method from trigger but in handler class there is no method named createNewUserFromContact.
Please update the method name in handler class.
Trigger:
Handler Class:
Thanks,
Maharajan.C
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
Hi,
Please edit your code like the one below. Refresh your console and try again
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
@Pav
Try Refreshing Dev Console Window Try And Save Again