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
devnullldevnulll 

Event Trigger - phone, email

I am trying to create a trigger that will add the phone, email and maybe one more contact field to the Event.Description field before insert.

 

The reason is that the email notification for new event is very vague and not helpful - missing the phone adn email. As a benefit the contact's phone and email will also be visible on cell phones and Outlook

 

So I created a trigger and it works, but I can't seem to retrieve the phone and email from the Event. These fields do not seem to exist, but are seemed linked to the contact's phone and email.

 

Here is my trigger:

trigger UpdateTaskDescription on Event (before insert) {
   for(Event e: trigger.new){
      if(e.description == null){e.description = '';}
      e.description += '\nPhone:'+ e.phone;
    }
}

 It generates an error on the last line - that e.phone doesn't exist.

 

Any ideas how to retrieve the corresponding phone and email fields from the related contact to which this event is linked?




joshbirkjoshbirk

The WhoID on Event will point back to the Contact.

devnullldevnulll

I used WhoID as suggested and it worked (not sure if this is the best way of doing it, but this is my first trigger).

 

Here is what I did:

 

trigger UpdateTaskDescription on Event (before insert) {
   for(Event e: trigger.new){
      Contact vCon = [Select Id, Name, Email, Phone, Description
                      From Contact    
                      Where id = :e.WhoID
                      Limit 1];

      if(e.description == null){e.description = '';}
       e.description += '\nName: ' + vCon.Name + '\nPhone: ' + vCon.Phone + '\nEmail: ' + vCon.Email;

    }
}

 

This trigger however will work only if the event is for a Contact. How do I properly detect if e.WhoID points to Lead or Contact?

Evan DonovanEvan Donovan

You should do that query in bulk; otherwise you will run into query limits. See http://wiki.developerforce.com/page/Best_Practice:_Bulkify_Your_Code. I'm going to be writing a similar trigger in a minute, so I will share my code with you.

 

I'm not sure if there is a way to check if WhoId is a lead or a contact; I think that it might just be that you have to query for both, and then assign based on which gets a match.

Evan DonovanEvan Donovan

Here's the code that I wrote for handling a similar problem.

 

The use of a Map to hold the Contact values was described in http://blog.jeffdouglas.com/2011/01/06/fun-with-salesforce-collections/ & in more detail in http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/.

 

In your case, if you want for Leads as well, you can create a second map for leads.

 

As for the assignments, I would suggest that first you get local variables for Email, Phone, etc., and then concatenate them together into the Event/Task description field. Note that the Task & Event are both Activity objects so they have the same fields.

 

trigger TaskSetContactEmailPhone on Task (before insert, before update) {
  // Create a set of all unique WhoIds.
  Set<Id> whoids = new Set<Id>();
  for(Task t : trigger.new) {
  	whoids.add(t.WhoId);
  }
  // Query for Contacts based on WhoIds.
  Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT Id, Name, Email, Phone, Description FROM Contact WHERE Id IN :whoids]);
  // Iterate over the list of records in the trigger and set the values before insert or update.
  for(Task t : trigger.new) {
  	// Confirm that the WhoId is in the Map (i.e., don't look at leads)
  	if(contacts.containsKey(t.WhoId)) {
  	  t.Contact_Email__c = contacts.get(t.WhoId).Email;
  	  t.Contact_Phone__c = contacts.get(t.WhoId).Phone;
  	}
  }
}