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
ThylaksoftThylaksoft 

Is it possible to create multiple events in trigger?

                 In our scenario,i need to create set of random events for contacts ,we tried with trigger  ,

that create only one event   for an instance.

                 how do we create bulk events?  

             Anybody have solution or valuable suggestion for this query would greatly appreciated  !

 

            Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
m_roarkm_roark

Santhanam,

 

This is easy to do.  An example of this is below.

 

trigger addMultipleEvents on Contact (after insert)

{

// Create a list to store the new events.

List<Event> newEvents = new List<Event> ();

// loop through new Contact records being inserted

for (Contact currentContact : Trigger.new)

{

// Create a loop to generate your Events

for (Integer i = 0; i < 10; i ++)

{

// Instanciate a new Event

Event newEvent = new Event(Subject = 'New Event '+i, ActivityStartDateTime = System.Now().addDays(i), WhoId=currentContact.Id);

// and add it to the list to be inserted.

newEvents.add(newEvent);

}

}

// After looping through all the Contacts in the trigger, if any new Events were created by the code, insert them into SalesForce.

if (newEvents.size() > 0)

insert newEvents;

}

All Answers

m_roarkm_roark

Santhanam,

 

This is easy to do.  An example of this is below.

 

trigger addMultipleEvents on Contact (after insert)

{

// Create a list to store the new events.

List<Event> newEvents = new List<Event> ();

// loop through new Contact records being inserted

for (Contact currentContact : Trigger.new)

{

// Create a loop to generate your Events

for (Integer i = 0; i < 10; i ++)

{

// Instanciate a new Event

Event newEvent = new Event(Subject = 'New Event '+i, ActivityStartDateTime = System.Now().addDays(i), WhoId=currentContact.Id);

// and add it to the list to be inserted.

newEvents.add(newEvent);

}

}

// After looping through all the Contacts in the trigger, if any new Events were created by the code, insert them into SalesForce.

if (newEvents.size() > 0)

insert newEvents;

}

This was selected as the best answer
ThylaksoftThylaksoft

 

 

m_roark