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
travis.truetttravis.truett 

Specify "Assigned to" in Opportunity Event Trigger?

I have a trigger that, among other things, is creating some events on new opportunities. Here's that section of the trigger:
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime,subject='Account Management Follow-Up', EndDateTime=myDateTime, IsAllDayEvent=true, OwnerId = 'cwilli'));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(2),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(2), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(4),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(4), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(6),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(6), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(8),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(8), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Account Management Follow-Up', EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(10),subject='Renewal',EndDateTime=myDateTime.addMonths(10), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));
                        eventList.add(new Event(whatid=o.id,startdatetime=myDateTime.addMonths(9),subject='Sales Contract Follow-Up',EndDateTime=myDateTime.addMonths(9), IsAllDayEvent=true/*, OwnerId = 'cwilli'*/));

As you can see, I was trying to use the OwnerId field with a specific user's alias, but that was essentially destroying my trigger. I just need to know the correct syntax for specifying the assigned to field when I create these events. Thanks.
Vivek DeshmaneVivek Deshmane
Hi Travis,

You can assign event owner as specific user by getting user detail by appllying allias or user name criteria  filter  on user object/table.
you should write below query at start of trigger out side loop.
List<User> lsuserRecords=[Select Id FROM User WHERE Alias='cwilli'];
if(lsuserRecords!=null && !lsuserRecords.isEmpty())
{
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime,subject='Account Management Follow-Up', EndDateTime=myDateTime, IsAllDayEvent=true, OwnerId = lsuserRecords[0].id));
}

You can Assign opportunity owner as  event owner in after insert trigger
eventList.add(new Event(whatid=o.id,startdatetime=myDateTime,subject='Account Management Follow-Up', EndDateTime=myDateTime, IsAllDayEvent=true, OwnerId = o.OwnerId));

Best Regards,
-Vivek