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
Austin JohnseyAustin Johnsey 

Trigger template for updating task field from field on related object

I have an identical custom field (Site_ID__c) on leads, contacts, opportunities and tasks. When a task is created and related to one of these objects, I would like a trigger that would update the field on the task from the field on the related object. Does anyone have a code template for listing the whatids, determining the object type and then specifying the custom field to update? Ideally, the code would repeat the process for all 3 possible objects in this scenario. 
Virendra Singh NarukaVirendra Singh Naruka
Austin,

Hope below code will help you .. 

String account_prefix = Schema.SObjectType.Account.getKeyPrefix();
String task_whatid = task.WhatId;
if(task_whatid.startsWith(account_prefix)){ // Means Id is of Account Type