📜  Apex-调用

📅  最后修改于: 2020-11-05 03:17:30             🧑  作者: Mango


Apex调用是指执行Apex类的过程。 Apex类仅在通过以下方式之一调用时可以执行:

  • 触发器和匿名块

  • 为指定事件调用的触发器

  • 异步Apex

  • 安排Apex类以指定的时间间隔运行,或运行批处理作业

  • Web服务类

  • Apex电子邮件服务课程

  • Apex Web服务,允许通过SOAP和REST Web服务公开您的方法

  • Visualforce控制器

  • Apex电子邮件服务可处理入站电子邮件

  • 使用JavaScript调用Apex

  • 调用Apex中实现的Web服务方法的Ajax工具包

现在,我们将了解一些调用Apex的常用方法。

从执行匿名块

您可以通过在开发人员控制台中执行匿名调用Apex类,如下所示-

步骤1-打开开发者控制台。

步骤2-单击调试。

Apex从执行匿名Step1调用

步骤3-执行匿名窗口将打开,如下所示。现在,单击执行按钮-

Apex从执行匿名Step2调用

步骤4-打开调试日志,当它出现在“日志”窗格中时。

Apex从执行匿名Step3调用

从触发器

您也可以从Trigger调用Apex类。在发生指定事件时调用触发器,并且触发器在执行时可以调用Apex类。

以下是示例代码,显示了调用触发器时如何执行类。

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

   public static Integer executeQuery (List 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.

从Visualforce页面控制器代码

也可以从Visualforce页面调用Apex类。我们可以指定控制器或控制器扩展,然后调用指定的Apex类。

VF页面代码

Apex从VF页面调用Step1

Apex类代码(控制器扩展)

Apex从VF页面调用Step2