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
sultansultan 

How to invoke a class through Trigger?please send the code?

Best Answer chosen by sultan
Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Feroz,

Both Java & Apex syntax are same.
1. if Method are Static then we can invoke method: Class_Name.MethodName();
2. If method are no-Static then:
    2.1: Create instance of class
    2.2: invoke method using object: Oob.MethodName();

Example:
=========================================================================================================
public class MyFirstClass{

   public void doOperation1(){
     // Enter statement here
   }
 
  public static void doOperation2(){
      //Enter statement here
  }

}
=========================================================================================================
                                                                      Invoke Controller Method from Trigger
//To call doOperation1() method
MyFirstClass obj = new MyFirstClass();
obj.doOperation1();

//To call doOperation2() method - above i have mentioned, Static method can call directly using ClassName.Method_Name
MyFirstClass.doOperation2();

Please let me know if you find any issue &  need more help.

Thanks & Regards,
Ranjeet Singh.

All Answers

AshlekhAshlekh
Hi,

You can create instance of class and call methods from trigger and you also call static methods of class without creating a instance of class and you can create method in Trigger also,

Below links hepls you to study about handler patterns

https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
http://developer.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers

Below links is same question 
https://developer.salesforce.com/forums/ForumsMain?id=906F000000093cKIAQ

IF it helps you than please mark it as a solution and ENJOY APEX

Ranjeet Singh (SFDC Developer)Ranjeet Singh (SFDC Developer)
Hi Feroz,

Both Java & Apex syntax are same.
1. if Method are Static then we can invoke method: Class_Name.MethodName();
2. If method are no-Static then:
    2.1: Create instance of class
    2.2: invoke method using object: Oob.MethodName();

Example:
=========================================================================================================
public class MyFirstClass{

   public void doOperation1(){
     // Enter statement here
   }
 
  public static void doOperation2(){
      //Enter statement here
  }

}
=========================================================================================================
                                                                      Invoke Controller Method from Trigger
//To call doOperation1() method
MyFirstClass obj = new MyFirstClass();
obj.doOperation1();

//To call doOperation2() method - above i have mentioned, Static method can call directly using ClassName.Method_Name
MyFirstClass.doOperation2();

Please let me know if you find any issue &  need more help.

Thanks & Regards,
Ranjeet Singh.
This was selected as the best answer