2

I need to create a trigger that will create a user(from contact) when the checkbox = true. This code saves, is active but doesnt create user.

trigger createUserfromContact on Contact (after update) {
List <Contact> newCon = new List<Contact>(); 
List <Contact> newRegCon = new List<Contact>(); 
List <User> newUser1 = new List<User>();

    for(Contact con : Trigger.new){
        newCon.add(con);     
    }
    for(Contact con : [Select Id,ESS_User__c from Contact WHERE Id =: newcon ]){

       if (con.ESS_User__c == TRUE){
           for(User userqry1 : [Select Id, ContactId from User WHERE ContactId =:newCon ]){
            newUser1.add(userqry1);

                if(newUser1.isEmpty()){
                    User newUser = new User( 
                    CommunityNickname = con.FirstName + con.LastName.substring(0,3),
                    ContactId = con.id,
                    Department = con.Department,
                    Email = con.Email,
                    EmailEncodingKey = 'ISO-8859-1',
                    EmployeeNumber= con.Employee_No__c,
                    FirstName = con.FirstName,
                    IsActive = TRUE,     
                    LastName = con.LastName,
                    PortalRole = '00e28000000NWUUAA4', 
                    ReceivesInfoEmails= TRUE,
                    Username = con.Email,
                    UserPermissionsChatterAnswersUser= TRUE,
                    UserRoleId = '  00E28000000JZDUEA4'
                    );
                    insert newUser;   

                }
            }
        }
    }   
}
4
  • if(newUser1 == NULL){ instead if(newUser1.isEmpty()){
    – Ratan Paul
    Apr 14, 2016 at 6:59
  • still nothing... Apr 14, 2016 at 7:01
  • are you sure.. can put debug and check user is created or not.. after insert DML
    – Ratan Paul
    Apr 14, 2016 at 7:03
  • And you wants to create user only if it not exists? Apr 14, 2016 at 7:20

3 Answers 3

0

Try this ;)

trigger createUserfromContact on Contact (after update) {     
    List <User> newUsers = new List<User>();
    List <User> existingUsers = new List<User>();

    for(Contact con : Trigger.NEW){
       if (con.ESS_User__c == TRUE){
            existingUsers = [Select Id, ContactId from User WHERE ContactId = :con.ID];
            if(existingUsers.isEmpty()){
                newUsers.add(new User( 
                CommunityNickname = con.FirstName + con.LastName.substring(0,3),
                ContactId = con.id,
                Department = con.Department,
                Email = con.Email,
                EmailEncodingKey = 'ISO-8859-1',
                EmployeeNumber= con.Employee_No__c,
                FirstName = con.FirstName,
                IsActive = TRUE,     
                LastName = con.LastName,
                PortalRole = '00e28000000NWUUAA4', 
                ReceivesInfoEmails= TRUE,
                Username = con.Email,
                UserPermissionsChatterAnswersUser= TRUE,
                UserRoleId = '00E28000000JZDUEA4'));
            }
        }
    }   
    insert newUsers;
}
1
  • 1
    SOQL inside a for loop. No No No !!! Apr 14, 2016 at 7:06
2

Issue is here your if condition will be outside

if (con.ESS_User__c == TRUE){
           for(User userqry1 : [Select Id, ContactId from User WHERE ContactId =:newCon ]){
            newUser1.add(userqry1);
            } //close this line here
            if(newUser1.isEmpty()){

              //create user
            }

I know you are not going to update bulk contact records because that will create no of user records. Still I want to make your code bulkify

trigger createUserfromContact on Contact (after update) {

List <User> newUser1 = new List<User>();//list to perfrom DML outside of loop
    //set to contain only ESS_User__c = true contacts
    set<Id> setContactIds = new set<Id>();
    for(Contact con : Trigger.new)
    {
        if (con.ESS_User__c == TRUE){
            setContactIds.add(con.Id);
        }
    }
    //set to store contactId to user record
    set<Id> setExistingUserContactId = new set<Id>();
    for(User objUser: [Select Id, ContactId from User WHERE ContactId IN:setContactIds ])
    {
        setExistingUserContactId.add(objUser.ContactId);
    }
    for(Contact con : Trigger.new)
    {
        //check contact Id already exist in set or not
        if(!setExistingUserContactId.contains(con.Id))
        {
            User newUser = new User( 
                    CommunityNickname = con.FirstName + con.LastName.substring(0,3),
                    ContactId = con.id,
                    Department = con.Department,
                    Email = con.Email,
                    EmailEncodingKey = 'ISO-8859-1',
                    EmployeeNumber= con.Employee_No__c,
                    FirstName = con.FirstName,
                    IsActive = TRUE,     
                    LastName = con.LastName,
                    PortalRole = '00e28000000NWUUAA4', 
                    ReceivesInfoEmails= TRUE,
                    Username = con.Email,
                    UserPermissionsChatterAnswersUser= TRUE,
                    UserRoleId = '  00E28000000JZDUEA4'
            );
            newUser1.add(newUser);  
        }
    }
    insert newUser1;
}
4
  • 1
    You beat me to it. Lightning fast :) Apr 14, 2016 at 7:07
  • @VigneshwaranG haha.. prev didn't see this mistake suddenly I notice that closing bracket of for loop
    – Ratan Paul
    Apr 14, 2016 at 7:08
  • I would use a Set of Id to store Contact Id than using mapContactIdToUser which is Id to User which is of no use in this context. Apr 14, 2016 at 7:19
  • @VigneshwaranG ahh thank for your suggestion.. updated above..
    – Ratan Paul
    Apr 14, 2016 at 7:24
0

why are you querying for the contact during the trigger.. you don't have to do this.

trigger createUserfromContact on Contact (after update) {     
List <User> newUsers = new List<User>();
map<Id, User> existingUsers = new map<Id, User>();
for( User existingUser :  [Select Id, ContactId from User WHERE ContactId = :triggeer.newMap.keySet() ] )
{
existingUser.add( existingUser.contactId, existingUser);
}
for(Contact con : Trigger.new)
{
   if (con.ESS_User__c == TRUE)
   {
   if(!existingUsers.containsKey( con.Id )){
            newUsers.add(new User( 
            CommunityNickname = con.FirstName + con.LastName.substring(0,3),
            ContactId = con.id,
            Department = con.Department,
            Email = con.Email,
            EmailEncodingKey = 'ISO-8859-1',
            EmployeeNumber= con.Employee_No__c,
            FirstName = con.FirstName,
            IsActive = TRUE,     
            LastName = con.LastName,
            PortalRole = '00e28000000NWUUAA4', 
            ReceivesInfoEmails= TRUE,
            Username = con.Email,
            UserPermissionsChatterAnswersUser= TRUE,
            UserRoleId = '00E28000000JZDUEA4'));
        }
    }
}   
if ( !newUsers.isEmpty() )
insert newUsers;

}

1
  • Add these things in comments; Apr 14, 2016 at 7:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .