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

Generic Trigger for all objects

Hi friends,


   Can u share u r knowledge with me,




      I want to write generic trigger for all objects,i.e i need to pass object name to the trigger then trigger will fired on that object(while insserting records). How i can acieve this.



Thanx In advance


Jon Mountjoy_Jon Mountjoy_

I don't believe you can write something that takes the object name and then creates a trigger on that object name.


The syntax for triggers demands an object name up front. ie. 



trigger X on ObjectName (before update) {


What are you trying to accomplish?







i am trying to write a trigger that can be applicable for all objects , we have to pass object1 name in runtime,then it will fires for that object1 while doing operations with that time if i pass object2 name then it will fires doing operations on that it will be works for each object  





ThanX in Advance

Cory CowgillCory Cowgill

1. You have to create Triggers for each Object, you cannot create just one Trigger. There is no way around that.


2. You CAN create a Trigger Handler which each Trigger would invoke and use Dynamic Apex / SObjects to perform generic logic at run-Time.


Review the Trigger Handler Pattern. There is a common blog post about this It is a common practice by most veteran salesforce developers to use Trigger Handler pattern.


Also, review the Dynamic Apex in the Apex Developer Guide. This will teach you how to dynamically cast SObject at run time.


Using those two approaches concepts, you may be able to achieve this goal..






In my opinion, it is not possible to implement because we can not call a trigger by trigger name and you cannot pass any parameter to the trigger. It automatically get called when it matches the DML conditions (insert, update and delete). This is Salesforce internal functionality so you can not override this.


A generic trigger handler is definitely the way to go.  I've created a set of trigger handler classes/utilities such that I can attach a trigger to any object/event combination and then in the body of the trigger simply pass the required context from Trigger into the handler in a type-independent manner.  Works great!  I do with I could do this without registering the trigger explicitly for each object that should have the behavior, but it's a small con overall.




I am new to salesforce,i am working on that but iam not getting ,can please provide code for me



ThanX in Advance