Apex - Invoking

Apex invoking refers to the process of executing the Apex class. Apex class can only be executed when it is invoked via one of the ways listed below −
  • Triggers and Anonymous block
  • A trigger invoked for specified events
  • Asynchronous Apex
  • Scheduling an Apex class to run at specified intervals, or running a batch job
  • Web Services class
  • Apex Email Service class
  • Apex Web Services, which allow exposing your methods via SOAP and REST Web services
  • Visualforce Controllers
  • Apex Email Service to process inbound email
  • Invoking Apex Using JavaScript
  • The Ajax toolkit to invoke Web service methods implemented in Apex
We will now understand a few common ways to invoke Apex.

From Execute Anonymous Block

You can invoke the Apex class via execute anonymous in the Developer Console as shown below −
Step 1 − Open the Developer Console.
Step 2 − Click on Debug.
Apex Invoking From Execute Anonymous Step1
Step 3 − Execute anonymous window will open as shown below. Now, click on the Execute button −
Apex Invoking From Execute Anonymous Step2
Step 4 − Open the Debug Log when it will appear in the Logs pane.
Apex Invoking From Execute Anonymous Step3

From Trigger

You can call an Apex class from Trigger as well. Triggers are called when a specified event occurs and triggers can call the Apex class when executing.
Following is the sample code that shows how a class gets executed when a Trigger is called.

Example

// Class which will gets called from trigger
public without sharing class MyClassWithSharingTrigger {

   public static Integer executeQuery (List<apex_customer__c> CustomerList) {
      // perform some logic and operations here
      Integer ListSize = CustomerList.size();
      return ListSize;
   }
}

// Trigger Code
trigger Customer_After_Insert_Example on APEX_Customer__c (after insert) {
   System.debug('Trigger is Called and it will call Apex Class');
   MyClassWithSharingTrigger.executeQuery(Trigger.new);  // Calling Apex class and 
                                                         // method of an Apex class
}

// This example is for reference, no need to execute and will have detail look on 
// triggers later chapters.

From Visualforce Page Controller Code

Apex class can be called from the Visualforce page as well. We can specify the controller or the controller extension and the specified Apex class gets called.

Example

VF Page Code
Apex Ivoking From VF Page Step1
Apex Class Code (Controller Extension)
Apex Ivoking From VF Page Step2

Contact

Talk to us

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores iusto fugit esse soluta quae debitis quibusdam harum voluptatem, maxime, aliquam sequi. Tempora ipsum magni unde velit corporis fuga, necessitatibus blanditiis.

Address:

৪৭/ দক্ষিণ বেগুনবাড়ি তেজগাঁও শিল্প এলাকা ঢাকা ১২০৮

Work Time:

সোমবার - শুক্রবার থেকে সকাল 9 টা থেকে বিকাল 5 টা

Phone:

+88 019 4145 2627