You are here:
Create Custom Activity Records by Using Apex Code
Automatically generate records for your parent custom object. Each such record corresponds to a custom child object that represents an activity to track on the timeline.
Required Editions
| Available in: Lightning Experience |
| Available in: Enterprise and Unlimited Editions with Life Sciences Cloud, Life Sciences Cloud for Customer Engagement Add-on license, and the Life Sciences Customer Engagement managed package. |
| User Permissions Needed | |
|---|---|
| To configure activity timeline visibility settings: | Life Sciences Commercial Admin permission set |
Use the following steps as an example to create records for the parent custom object on insert, update, and delete actions, as applicable. Wherever needed, replace Custom_Activity__c with the API name of your custom child object. For example, if you create a custom object to represent in-person meetings, use the In_Person_Meetings object name to replace Custom_Activity__c, wherever applicable.
-
Create a trigger service apex class to generate records for the parent custom object that
each corresponds to the child custom objects.
- From Setup, select Developer Console.
- Click File | New | Apex Class.
- Enter CustomActivityTriggerService as the name.
-
In the CustomActivityTriggerService.aptx file, enter the following sample code. Customize
the code as needed.
public class CustomActivityTriggerService { public static void writeAccountActivityRecord(List<Custom_Activity__c> incomeRecords) { List<Account_Activity__c> eventRecords = new List<Account_Activity__c>(); for (Custom_Activity__c objRecord : incomeRecords) { eventRecords.add(new Account_Activity__c( Account__c = objRecord.Account__c, Event_Date_Time__c = Datetime.now(), // value should be in GMT Title__c = 'any text you want to use as a Title', Subtitle__c = 'another text you want to use as Subtitle', Entity_Id__c = objRecord.Id, Entity_Type__c = SObjectType.Custom_Activity__c.name, Entity_Subtype__c = 'any value that is used to classify ObjectA records, e.g. this can be a record type name', Event_Data__c = JSON.serialize(new ObjectEventData(objRecord)) )); } insert eventRecords; } private class ObjectEventData { public List<FieldDTO> fields; public ObjectEventData(Custom_Activity__c record) { fields = new List<FieldDTO>(); fields.add(new FieldDTO(SObjectType.Custom_Activity__c.fields.Status__c.name, String.valueOf(record.Status__c))); // fields.add(new FieldDTO(SObjectType.Custom_Activity__c.fields.FieldB__c.name, String.valueOf(record.FieldB__c))); // fields.add(new FieldDTO(SObjectType.Custom_Activity__c.fields.FieldC__c.name, String.valueOf(record.FieldC__c), record.FieldC__r.Name)); //example for date time field fields.add(new FieldDTO(SObjectType.Custom_Activity__c.fields.Start_Time__c.name, record.Start_Time__c.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''))); } } private class FieldDTO { public String fieldAPIName; public String fieldValue; public String relatedRecordName; public FieldDTO(String fieldAPIName, String fieldValue) { this.fieldAPIName = fieldAPIName; this.fieldValue = fieldValue; } public FieldDTO(String fieldAPIName, String fieldValue, String relatedRecordName) { this(fieldAPIName, fieldValue); this.relatedRecordName = relatedRecordName; } } }
-
Create a trigger for the apex class.
This configuration ensures that after you create a record for a custom activity, such as an in-person meeting, a related parent custom activity record is created.
- From Setup, select Developer Console.
- Click File | New | Apex Trigger | .
- Enter CustomActivityTrigger as the name.
- Select Custom_Activity__c as the sObject.
-
In the CustomActivityTrigger.aptx file, enter the following sample code. Customize the
code as needed.
trigger CustomActivityTrigger on Custom_Activity__c (after insert, after update) { //this check can be skipped if trigger is subscribed only on after insert //and after update events and service class method can be called immediately if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) { CustomActivityTriggerService.writeAccountActivityRecord(Trigger.new); } }
Did this article solve your issue?
Let us know so we can improve!

