调用( Invoking)
优质
小牛编辑
145浏览
2023-12-01
Apex调用是指执行Apex类的过程。 Apex类只有在通过下面列出的方法之一调用时才能执行 -
触发器和匿名块
为指定事件调用的触发器
异步Apex
调度Apex类以指定的时间间隔运行,或运行批处理作业
Web服务类
Apex电子邮件服务类
Apex Web Services,允许通过SOAP和REST Web服务公开您的方法
Visualforce控制器
Apex电子邮件服务处理入站电子邮件
使用JavaScript调用Apex
用于调用Apex中实现的Web服务方法的Ajax工具包
我们现在将了解一些调用Apex的常用方法。
从执行匿名阻止
您可以通过在Developer Console中执行匿名来调用Apex类,如下所示 -
Step 1 - 打开开发者控制台。
Step 2 - 单击Debug。

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

Step 4 - 打开调试日志,它将显示在“日志”窗格中。

来自触发器
您也可以从Trigger调用Apex类。 当指定事件发生时调用触发器,触发器在执行时可以调用Apex类。
以下示例代码显示了在调用Trigger时如何执行类。
例子 (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.
来自Visualforce页面控制器代码
也可以从Visualforce页面调用Apex类。 我们可以指定控制器或控制器扩展,并调用指定的Apex类。
例子 (Example)
VF Page Code

Apex Class Code (Controller Extension)
