×
Apex 编程教程Apex 环境Apex 示例Apex 数据类型Apex 变量Apex 字符串Apex 数组Apex 常量Apex 决策Apex 循环Apex 集合Apex 类Apex 类方法Apex 对象Apex 接口Apex DMLApex 数据库方法Apex SOSLApex SOQLApex 安全性Apex 调用Apex 触发器Apex 触发设计模式Governer Limits调节器限制Apex 批量处理Apex 调试Apex 测试Apex 部署Apex 字符串Apex 数组

Apex 调用


Apex调用是指执行Apex类的过程。 Apex类只能在通过以下方法之一调用时执行:

  • 触发器和匿名块
  • 为指定事件调用触发器。
  • 异步Apex
  • 调度Apex类以按指定的时间间隔运行,或运行批处理作业。
  • Web服务类
  • Apex电子邮件服务类
  • Apex Web服务,它允许通过SOAP和REST Web服务公开您的方法。
  • Visualforce控制器
  • Apex电子邮件服务来处理入站电子邮件。
  • 使用JavaScript调用Apex
  • Ajax工具包,用于调用在Apex中实现的Web服务方法。

我们将看看调用的Apex一些常见的方式。


执行匿名块

您可以通过开发者控制台中的execute anonymous调用Apex类,如下所示:


步骤1:打开开发者控制台


步骤2:单击调试。


调试


第3步:执行匿名窗口将如下所示打开,然后点击执行按钮:


执行匿名窗口

第4步:打开调试日志,它将出现在日志窗格中。


调试日志


触发器

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


下面是一个示例代码,显示当调用Trigger时类如何被执行。


例如:

//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则页面控制代码

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


例如:


VF页面代码:


VF页面代码


Apex类代码(控制器扩展)


Apex类代码


分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)