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
aiyaz_SFDCaiyaz_SFDC 

Trigger (Best practice question)

Just hoping to get some advise:

 

- i'm trying to set up a trigger on task insert and update and since tasks can be associated to various objects Accts, Opps, Contacts, Cases, Leads, etc) I will need to create a trigger that can handle every scenario!  Has anyone done this?

 

- what should I consider (eg. the values of WhoID, whatID etc??)

 

- should I make several triggers for the Task object, or can it be done just as effectively in one trigger?

 

Any suggestions would be great!

 

Thanks in advance!

ShikibuShikibu

You should be able to do this in a single trigger. WhoID and WhatId are polymorphic -- they can reference a variety of different SObjects.

 

Here's an entity relationship diagram that shows this.


Does your trigger need to treat leads/contacts (whoid) and Accts, Opps, Cases, ... (whatid) differently? Then you'll need logic for what ever is different, but not additional triggers.

 

If your logic is more than a couple of lines, it might be a good idea to put it into a separate apex class.

 

 

aiyaz_SFDCaiyaz_SFDC

Thanks for the info!

 

Actually the trigger is only meant to update a field on the account record, so I would like it to also update the same field on the account record if the task is associated to JUST a Contact (a lot of times when creating a task for a contact, the account is not selected on the Related To field so I need to figure that part out).  I guess I will have to associate the contact to the account via the accountID.  Any suggestions on this?