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
DIEHARDDIEHARD 

Trigger to assign Event WhoId field not working

I am unable to assign the WhoId field to a contact ID in the trigger below. I do actually get the ID from the query statement in trigger.  I have verified that already, but when I assigned it to WhoId for the event that's firing the trigger, nothing seems to happen.  Any clues?

 

trigger ContactList on Event (before insert) {
    List<string> elist;
    LIST<Contact> contacts;   
    EventRelation er;
    string qemail;
    String input;
    String[] splitInput;

    for(Event e: Trigger.new) {
    
            // Create a list of emails by using ';' as delimeter       
            input = e.Description;
            if(input != null){
                input = input.replace('\r',' ');
                input = input.replace('\n',' ');
                input = input.replace('\t',' ');
                splitInput = input.split(' ');
            }          
            elist = new List<String>( splitInput );
            
            // Iterate through the list if not empty
            if (!elist.isEmpty())
                for(Integer j = 0; j < elist.size(); j++){
                    
                    if(elist[j].contains('@') && elist[j].contains('.')){
                        qemail = elist[j].trim();
                        contacts = null;
            // Query for contact name and  contact ID, associated with recipient email address
                        contacts = new List<Contact>([Select ID,Email,Name from Contact where Email = :qemail]);
                        if(!contacts.isEmpty())
                            e.WhoId = contacts[0].ID;                    
                        }
                    }
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Diehard,

 

First and foremost ,you should be running query inside your For loop,that is not recommended at all.I ahve modified the code.Please find below the uodated one:-

 

trigger ContactList on Event (before insert) {
    List<string> elist;
    LIST<Contact> contacts;   
    EventRelation er;
    string qemail;
    String input;
    String[] splitInput;

    for(Event e: Trigger.new) {
    
            // Create a list of emails by using ';' as delimeter       
            input = e.Description;
            if(input != null){
                input = input.replace('\r',' ');
                input = input.replace('\n',' ');
                input = input.replace('\t',' ');
                splitInput = input.split(' ');
            }      
}			
            elist = new List<String>( splitInput );
            
            // Iterate through the list if not empty
            if (!elist.isEmpty())
                for(Integer j = 0; j < elist.size(); j++){
                    
                    if(elist[j].contains('@') && elist[j].contains('.')){
                        qemail = elist[j].trim();
                        contacts = null;
						}
						}
            // Query for contact name and  contact ID, associated with recipient email address
                        contacts = new List<Contact>([Select ID,Email,Name from Contact where Email = :qemail]);
						for(Event ev : Trigger.new){
                        if(!contacts.isEmpty())
                            ev.WhoId = contacts[0].ID;                    
                        }
                    
    
}

 

 

Another thing to note is whether you have shared activities enabled in your org.Please check that.That might be the root cause.