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
Felipe SenlerFelipe Senler 

For loop that gets all users from User, to get certain users from User

Hello community:

You see, i have this issue from a previous problem on another post though, since ive upgraded my solution a bit more, but heres my issue:
I need that from a for loop on two users, user1 (Original object) and User2 object to dinamically itarate against, to get all users, this actually does manage to grant me all users from the Object user, but now i need to get only a few names out of it, heres the code ive figured so far:
for(User user2:user1)//Iterates to get all users available
            {   
                	String usua = user2.name;
                	System.debug('Nombres de usuarios:'+usua);
                    String usuaid = user2.id;//gets the id for each user retrieved
                    String a = usuaid;
                   //Assigns the Id of the users into 'a' as variable (Orginal,right?)
            		Id usuarioid = Id.valueOf(a);//turns the id (String) as Id
                    System.debug('Id usuario: '+usuarioid);
            }
To sum up, i need to basically, from this loop that gets me all users and id's from user to be able to retrieve certain users out of it, been trying to figure this out, but im kinda stuck for the time being.
Any help is appreciated.
Anthony Williams 34Anthony Williams 34
Based on how you have listed the code and my understanding of  what you are looking, I would add an if condition inside of the for loop to check for the User you are looking for.  

I do have a question about the approach of this code, from my understanding the user1 object is gathering all users in your org via an for loop.  A second for loop was created to search for a particular set of users.  My question is this, just create a Map against the User object and add a SOQL where clause to limit the search for the records you are looking for?  With this approach, you will have your users stored in a variable that you can manipulate at your lesuire within the code.  Here is an example of this function: https://developer.salesforce.com/page/Apex_Code_Best_Practices
Felipe SenlerFelipe Senler
trigger EventosAOportunidadv2 on Event (after insert) 
{
    List<Opportunity> op = [SELECT Owner.id, Owner.Name, Equipo__c 
              				From Opportunity];
    List <User> user1 = [Select name,id from user];
    List <User> useraux = new List <User>();

    for(Event eve:Trigger.new)//Iterates the event
    {	 

        If((eve.Subject == 'Reunión'))//Verifies if the event has 'Meeting' as a subject
        {
         emailHelper e = new emailHelper();
            List <String> picklist = op.get(0).Equipo__c.split(';');
             		System.debug('Equipo picklist: '+picklist);
            
         for(Integer i = 0; i<user1.size();i++)  //Heres the previous code, but this time using the size of Users as a limiter for lists elements
         {
             		String usua = user1.get(i).name;
                	System.debug('Nombres de usuarios:'+usua);
                    String usuaid = user1.get(i).id;
            		System.debug('Usuario id: '+usuaid);
                    String a = usuaid;
            		Id usuarioid = Id.valueOf(a);
                    System.debug('Id usuario: '+usuarioid);
 /*And here code goes to a mess, since need values from the multipicklist (Equipo__c) to actually compare and associate those elements selected with User's id and name, and kinda stuck here too.*/

            if((op.get(0).equipo__c).contains(user1.get(0).name)&&(op.get(0).equipo__c!=null)) 
//Then its just a matter of having the user id's from the Users
            {
                System.debug('Mail's sent!!');
                e.sendEmail(usuarioid); 
            	System.debug('To id: '+usuarioid+' name's: '+usua);
            }
         }
          }
        }
Thats the full code, @Anthony, to sum up, this trigger needs to send emails, from elements selected from a picklist and associate the selected elements to a ID to use the helper method sendEmail.

Thanks a lot for your fast reply.