You need to sign in to do that
Don't have an account?
Modify Event trigger to include a list of contacts instead of invitees
The trigger below parses the Description field to obtain an email address. It then associates the email address with a contact using a query, and then finally inserts the contact into the invitee list of the event. It then loops back to parse new addresses and continue the process. The trigger works but I need to modify it to create a list of contacts instead of invitees when shared activities is enabled.. Does anyone have an idea? I know it's not a good idea to include a query inside a for loop but since we are dealing with multiple invitees then I don't know a way around it.
Trigger ParseAndAddInvites on Event(after 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]);
// Create and insert new attendee using queried contact and event info
if(!contacts.isEmpty()){
er = new EventRelation(EventId = e.ID, RelationId = contacts[0].ID);
insert er;
}
}
}
}
}
Hi Shailesh,
I have one question. Do you know how I can modify the event relation so that I can add a list of contacts instead of invitees.
I used: "new EventRelation(EventId = e.ID, RelationId = contacts[0].ID)" to add invitees but what should I do to add contacts?
Thank you Shailesh and I appreciate your quick response.
Do you mean that you want to add more than one contact,if that is so then try below :--
for(Integer i =0;i<=contacts.size();i++){
if(!contacts.isEmpty()){
er = new EventRelation(EventId = e.ID, RelationId = contacts[i].ID);
insert er;
}
}
No. The code above will add more than one invitee to the event. But, I don't want to add them as invitees. I need to add them as contacts. Any ideas?
What you mean by adding them as contacts because they are already Contacts from the below code :-
contacts = new List<Contact>([Select ID,Email,Name from Contact where Email = :qemail]);
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_eventattendee.htm
What I want to do is insert them as invitees who has already accepted the event or as a contact that has the event in his calendar. The invitee needs to have the event in his Open Activities related list. By adding "isInvitee = false" doesn't seem to work.
I got it to work but I had to include "isParent = true" in addition to "isInvitee = false"