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 

List has more than 1 row for assignment. Need to retrieve all names and Id's from User to Event Trigger

Greetings community, you see, i need your help with this trigger, let me context first:

What i exactly need for the trigger, is that from a multipicklist, associate its values to a user id to send a email for the associated user id from User, because of a helperclass called "emailHelper", that after a few modifications it requires a user id that lets it send a template email to the users that the user id has referes to.

Trigger is here below:
trigger EventosAOportunidadv3 on Event (after insert) {

    List<Opportunity> op = [SELECT Owner.id, Owner.Name, Equipo__c 
              				From Opportunity];
    
    for(Event eve:Trigger.new)//Recorre el evento
    {      
        System.debug('Tamaño Trigger: '+Trigger.new.size());
		system.debug('Cantidad de oportunidades: '+op.size());
        If((eve.Subject == 'Reunión'))//Verifies the subject is reunion
        {
            emailHelper e = new emailHelper();
            
            String[] tmpString = op.get(0).Equipo__c.split(';');
			For(String s : tmpString)//Iterates along the custom object (multipicklist)
   			system.debug(s);
            
           //Here i need to bring the users from User and retrieve its id's i did that as well, but....
    User user1 = [Select name,id from user];

            if((op.get(0).equipo__c).contains(user.name)&&(op.get(0).equipo__c!=null))
            {
            
            String usua = user1.ownerid;//For testing got the owner's id related to the event, but  list has more than 1 row for assignment, if limited the user up to one the code DOES Work and ran out of options
            String a = usua;
            Id usuarioid = Id.valueOf(a);//and turn its string value onto a ID
            System.debug ('Por Felipe');
            e.sendEmail(usuarioid);
            	else
                {
                    System.debug('User not valid');
                    //e.sendmail(usuarioid);
                }
            }    
           }
         }
}
emailHelper class, if needed:
// In a separate class so that it can be used elsewhere
Global class emailHelper {
    
public void sendEmail(ID usuarioid) {

  //New instance of a single email message

 Messaging.SingleEmailMessage mail =

            new Messaging.SingleEmailMessage();

// Who you are sending the email to
    mail.setTargetObjectId(usuarioid); 
	//System.debug('Mail de user:'+user);
    // The email template ID used for the email

   mail.setTemplateId('00X36000000NQvEEAW');   

   mail.setBccSender(false);

   mail.setUseSignature(false);

   mail.setSenderDisplayName('HR Recruiting');

   mail.setSaveAsActivity(false); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    } 

}


E.g: If from the multipicklist i pick the values "Felipe" and "Andres", need to associate those picklist values to a actual user ID from two people stored on the User object,in this case called "Felipe" and "Andres.

I know its not very bulky safe, but any help would be appreciated.
Thanks in advance!
Phillip.
Amit Chaudhary 8Amit Chaudhary 8
Please update your class like below
trigger EventosAOportunidadv3 on Event (after insert) 
{

    List<Opportunity> op = [SELECT Owner.id, Owner.Name, Equipo__c From Opportunity ];
    
    for(Event eve:Trigger.new)
    {
        System.debug('Tamaño Trigger: '+Trigger.new.size());
		system.debug('Cantidad de oportunidades: '+op.size());
		
        If((eve.Subject == 'Reunión'))//Verifies the subject is reunion
        {
            emailHelper e = new emailHelper();
            String[] tmpString = op.get(0).Equipo__c.split(';');
			For(String s : tmpString)
				system.debug(s);
            
            //Here i need to bring the users from User and retrieve its id's i did that as well, but....
			List<User> Listuser1 = [Select name,id from user limit 1];

            if((op.get(0).equipo__c).contains(user.name)&&(op.get(0).equipo__c!=null))
            {
            
				String usua;
				if( Listuser1.size() > 0 )	
				{
					usua = Listuser1[0].id;
				}
			
				if(usua != null)
				{
					e.sendEmail(usua);
				}
            	else
                {
                    System.debug('User not valid');
                }
            }
        }
    }
	
}

Let us know if this will help you
Felipe SenlerFelipe Senler
Sadly, the code didint worked because:

on line 21: Method does not exist or incorrect signature: [String].contains(Schema.SObjectField)

Where "if((op.get(0).equipo__c).contains(user.name)&&(op.get(0).equipo__c!=null))"

Apparently, a contains cant have a Schema.SObjectField.
Thanks!
Phillip
Felipe SenlerFelipe Senler
I may have come with a better solution than before, ,but now problem is:
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]; //Created a user since i require the users id to associate them against the names that are contained on Custom field "Equipo__c" from Opportunity
    
    for(Event eve:Trigger.new)
    {	        
        If((eve.Subject == 'Reunión'))
        {
            emailHelper e = new emailHelper();
            
            for(User user2:user1)Iterate all the list from user1 to another called user 2
            {   
                for (Integer i = 0; i<user1.size();i++)
//Iterate the whole user2 through the size of user1 as a limiter
                {
                    String usua = user2.name;
                	System.debug('Nombres de usuarios:'+usua);
                    String usuaid = user2.id;
                    String a = usuaid;
            		Id usuarioid = Id.valueOf(a);
                    System.debug('Id usuario: '+usuarioid);
//Recieves in fact the five users, but does the iteration five times, causing the later contains to not send the mail as needed, sadly....
                    
            if((op.get(0).equipo__c).contains(user1.get(0).name)&&(op.get(0).equipo__c!=null))
            {
                System.debug('Se fue correo!');
                e.sendEmail(usuarioid); 
            	//System.debug('Al id: '+usuarioid+' De nombre(s): '+usua);
            } 
            }          
            }
            }
            }    
     }
Like said before, it does the itearation for all users though. But it does it five times for each user. However, now it looks bulky safe though so its progress!
Any help its appreciated
Phillip