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
Thomas Gordon 20Thomas Gordon 20 

need a loop

I know I need a loop in here somewhere to loop through this list, but I can't get it right. I get" initial term of field expression must be a concrete SObject : list. Can somebody save me please? I'm looking to create a contact from anew user. I know the trigger wont work.

public class createContactFromUser {
    
    public static void ContactCreate(){ 
        List<Contact> ct = new List<Contact>();
        List<User> usr = new List<User>();
        usr=[select id, Name, AccountId, Firstname, Lastname, Email from User                        
                        where profileId ='00et0000000HfNg'
                    and isActive = true];  
        
            System.debug(usr.size());
                 
        if(usr != null && usr.size() > 0) {    
            Contact uc = new Contact();                
                uc.Name = usr.Name;            
              uc.Email = usr.Email;
            uc.AccountId = usr.AccountId;
             ct.add(uc);
        }    
    insert ct;
    }
}
Best Answer chosen by Thomas Gordon 20
Patcs_1Patcs_1
Hi

Use for loop here

   if(usr != null && usr.size() > 0) {    
    for(user u: usr)
    {
            Contact uc = new Contact();                
                uc.Name = u.Name;            
              uc.Email = u.Email;
            uc.AccountId = u.AccountId;
             ct.add(uc);
   }
        }    
Insert ct;

Thanks

All Answers

Patcs_1Patcs_1
Hi

Use for loop here

   if(usr != null && usr.size() > 0) {    
    for(user u: usr)
    {
            Contact uc = new Contact();                
                uc.Name = u.Name;            
              uc.Email = u.Email;
            uc.AccountId = u.AccountId;
             ct.add(uc);
   }
        }    
Insert ct;

Thanks
This was selected as the best answer
Thomas Gordon 20Thomas Gordon 20
thanks,...may have helped but error moved to line:  uc.Name = u.Name; . Will prolly move thru the loop until I get the variables right or something??
Thomas Gordon 20Thomas Gordon 20
I'm sorry, let me be clearer...I changed the 'Name' to 'First Name', as 'Name was not writeable. So now, 'invalid type: uc.first..' I tried 'String' in front, but no work...
Patcs_1Patcs_1
ok it should be,

              Contact uc = new Contact();                
                uc.FirstName = u.FirstName;  
               uc.LastName = u.LastName;
              uc.Email = u.Email;
            uc.AccountId = u.AccountId;

Try this let me know if this is fixed.

Thanks
Thomas Gordon 20Thomas Gordon 20
Thanks, FIXED the errors, all but creating a user didnt create a contact. Wrong logic I guess; back to square 1
Patcs_1Patcs_1
Hi Thomas

Have you called this class in Trigger (user Object)?

Thanks
Thomas Gordon 20Thomas Gordon 20
Naw, Just tested by adding new User, thinking it would insert new contact. Don't see y not? Tried building trigger to create contact but lessons learned is DML on these two objects wont work in same class, so need future class. Just as well build this, but dysfunctional so far...