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>
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>
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>
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.
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
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
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
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