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
Praveen rajPraveen raj 

triggers on multiple objects

Hi i'm new to salesforce can any one help me. Can  we write triggers on multiple objects if yes could you send me some sample code 

Best Answer chosen by Admin (Salesforce Developers) 
DeepeshDeepesh

If your question was that if one trigger would work for multiple objects, the answer is no.

Apex triggers are object specific.

If you meant if you can have DML or code on multiple objects within a same trigger, yes you can do that.

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Yes we can write a trigger on multiple objects. Triggers are written in Apex, and execute before or after an insert, update, delete or undelete event occurs on a sObject. The syntax that introduces a trigger definition will look a little familiar, and begins with the trigger keyword:

trigger myAccountTrigger on Account (before insert, before update)

{

   if (Trigger.isInsert)

   {

    //

   }

   if (Trigger.isUpdate) {

     for(Account a: Trigger.new)

       if (a.name == 'bad')

         a.name.addError('Bad name'); // prevent update

      }

 }

 

This example fires before any Account sObject is inserted or updated. The Trigger.new context variable provides access to the list of accounts being inserted or updated. Update and delete triggers can use Trigger.old to refer to the old versions of the objects being updated or deleted.

Triggers make full use of Apex, allowing you to continue using a familiar language for data manipulation. There are a few restrictions - for example it doesn't make sense to allow call outs to web services from within a trigger as that would unduly lengthen the transaction.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

DeepeshDeepesh

If your question was that if one trigger would work for multiple objects, the answer is no.

Apex triggers are object specific.

If you meant if you can have DML or code on multiple objects within a same trigger, yes you can do that.

This was selected as the best answer
Praveen rajPraveen raj

HI jain i'm sorry i asked for multiple objects you have mention only for single object i.e Account