Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created August 7, 2022 16:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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