[SalesForce] Apex Trigger Creating Event on Account- Won’t show in Open Activities

I've created an Apex trigger on a custom object that creates an event for an account. The trigger works, and I have the data, but I can't get it to show up under Accounts=> Choose Account=> Open Activities.

In an attempt to figure it out, I manually created a new event with the same start and end date/times in the Open Activities section under the account by clicking the "New Event" button. It shows up, so I ran a SOQL query- basically Select all fields from event, and compared the data between events that do show as open, and the events I created that don't show up. I don't see any differences, unless I am just missing something.

Here's the code from my trigger:

    trigger CreateSFEventFromOperationsEvent on Operations_Events__c (after insert) {
    //When a new OperationsEvent is created, create an actual Salesforce Event under Account Activities.

    for (Operations_Events__c opEvent : Trigger.new) {
        Event event = new Event(
            OwnerId = opEvent.CreatedById,
            WhatId = opEvent.Event_Account__c,
            StartDateTime = opEvent.StartDate__c,
            EndDateTime = opEvent.End_Date__c,
            Subject = 'Go Live',
            Department_s_del__c = opEvent.Department__c,
            Product_s__c = opEvent.Product_s__c

        );
        insert event ;             
    }
}

Any help is appreciated.

Best Answer

The reason the event is not coming up in the Open Activities is because the activityDateTime field in the Event will be set to the createdDate, unless you explicitly set it. This will cause the Event to appear in the Activity history Related List. To resolve this issue you have to set the ActivityDateTime field to the event start date while creating the event. So your code should look something like this:

Event event = new Event(
        OwnerId = opEvent.CreatedById,
        WhatId = opEvent.Event_Account__c,
        StartDateTime = opEvent.StartDate__c,
        EndDateTime = opEvent.End_Date__c,
        ActivityDateTime = opEvent.StartDate__c, //add this line
        Subject = 'Go Live',
        Department_s_del__c = opEvent.Department__c,
        Product_s__c = opEvent.Product_s__c

    );

Also please make sure that you have bulkified your trigger.

Related Topic