Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created Aug 7, 2022
Embed
What would you like to do?
Task Trigger
trigger TaskTrigger on Task (after insert) {
//List of platform event
List<Activity_Event__e> activityEventList = new List<Activity_Event__e>();
if(Trigger.isAfter && Trigger.isInsert){
system.debug('URL => ' + String.valueOf(URL.getCurrentRequestUrl()).toLowerCase());
Boolean recordCreateFromAPI = String.valueOf(URL.getCurrentRequestUrl()).toLowerCase().contains('services/soap');
system.debug('recordCreateFromAPI => ' + recordCreateFromAPI);
if(!recordCreateFromAPI){ //check if record is creating from API or not (below code will not execute if record inserted from data loader [API])
for(Task tsk: Trigger.new){
//Instantiating platform event
Activity_Event__e ae = new Activity_Event__e();
ae.Activity_Record_ID__c = tsk.Id; //put task record id
ae.What_ID__c = tsk.WhatId; // put parent record id
ae.Who_ID__c = tsk.whoId; // put who id (contact,Lead ID)
activityEventList.add(ae); //add event in the list
}
}
}
try{
if(activityEventList.size()>0)
EventBus.publish(activityEventList); //publish the event
}catch(Exception e){
system.debug('Exception has occurred! ' + e.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment