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
Mani RenusMani Renus 

How a trigger calls the Apex class? please give any example

Best Answer chosen by Mani Renus
Satish_SFDCSatish_SFDC
You create methods on your Apex Class. Can be static or instance methods.
In your trigger you can then have code that calls these classes.
This is very similar to the way method calling works in other languages.

For eg:
Class
<pre>
public class MyClass{
   public static void TestMethod(){
             //Your code in the class
   }
}
</pre>

Trigger:
<pre>
Trigger testTrigger on Account(before insert){
     MyClass.TestMethod();
}
</pre>

Hope this helps

Regards,
Satish Kumar

All Answers

Satish_SFDCSatish_SFDC
You create methods on your Apex Class. Can be static or instance methods.
In your trigger you can then have code that calls these classes.
This is very similar to the way method calling works in other languages.

For eg:
Class
<pre>
public class MyClass{
   public static void TestMethod(){
             //Your code in the class
   }
}
</pre>

Trigger:
<pre>
Trigger testTrigger on Account(before insert){
     MyClass.TestMethod();
}
</pre>

Hope this helps

Regards,
Satish Kumar
This was selected as the best answer
Satish_SFDCSatish_SFDC
Think of these as events.
So if you want some code to be executed just before inserting a record you use the before insert. If you want code to be executed just after an account is created (may be sending an email) you write the code in the after insert trigger. 

There are only 7 such events
before insert
before update
before delete
after insert
after update
after delete
after undelete

You can use all of them on a single trigger or write seperate triggers for each event.

If you are using a combination like below:
<pre>
Trigger myTrigger on Account(before insert, before update, after insert, after update){
      if(Trigger.isBefore){
              if(Trigger.isUpdate){
                           //Code to execute before update
              }
              if(Trigger.isInsert){
                         //Code to execute before insert
              }
      }
      else if (Trigger.isAfter){
              if(Trigger.isUpdate){
                        //Code to execute after update
              }
              if(Trigger.isInsert){
                      //Code to execute after insert
              }
       }
}
</pre>

Regards,
Satish Kumar
Satish_SFDCSatish_SFDC
In this case you can use the before insert and  before update trigger.

After insert will not work here because this trigger runs after the records are inserted into the database. Once the records are inserted you may not be able to change the values. You may have to query them again and then change the values.
But this way the after update trigger will fire again.

That is a lot of work, So before update is a good place.

Regards,
Satish Kumar