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 

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;                      

                        }

                    }

                }                      

    }

}

Shailesh DeshpandeShailesh Deshpande
I think you should first store the email address in a set. Then make a query to fetch the contacts whose Email is present in the set. Then continue with your regular process and check if set contains email present at elist[j]. This way you dont have to make a query in the for loop. Also when you find a matching email create a list of event relation, add them to the list and then insert the list outside the for loop.
DIEHARDDIEHARD

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. 

Vinit_KumarVinit_Kumar

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;    

 

}

}

DIEHARDDIEHARD

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?

Vinit_KumarVinit_Kumar

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]);

Shailesh DeshpandeShailesh Deshpande
In that case you simply need to specify "isInvitee = false" for the EventRelation object. Please refer the below link:

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_eventattendee.htm
DIEHARDDIEHARD

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.

DIEHARDDIEHARD

I got it to work but I had to include "isParent = true" in addition to "isInvitee = false"

Shailesh DeshpandeShailesh Deshpande
Cool.. Thanks for sharing.