如何从扩展虚拟类的触发器处理程序中调用静态方法在Apex中?

huangapple go评论73阅读模式
英文:

how to call a static method from a trigger handler that extends a virtual class in Apex?

问题

I've got a trigger handler class which extends a virtual class from this framework.


public override void beforeInsert() {
    handleExWorksCalculations(Trigger.New, Trigger.oldMap, true);
}

public static void handleExWorksCalculations(List<OrderItem> orderItemsList, Map<Id,OrderItem> oldOrderItemsMap, Boolean isInsert) { }```

When I deploy the class to the org I get the following error:

> force-app\main\default\classes\OrderItemTriggerHandler.cls  Method
> does not exist or incorrect signature: void
> handleExWorksCalculations(List<SObject>, Map<Id,SObject>, Boolean)
> from the type OrderItemTriggerHandler (69:33)

I know I missing a fundamental concept as the parameters do match the method signature.

<details>
<summary>英文:</summary>

I&#39;ve got a trigger handler class which extends a virtual class from [this framework](https://github.com/kevinohara80/sfdc-trigger-framework).

public with sharing class OrderItemTriggerHandler extends TriggerHandler { }

public override void beforeInsert() {
    handleExWorksCalculations(Trigger.New, Trigger.oldMap, true);
}

public static void handleExWorksCalculations(List&lt;OrderItem&gt; orderItemsList, Map&lt;Id,OrderItem&gt; oldOrderItemsMap, Boolean isInsert) { }
When I deploy the class to the org I get the following error:

&gt; force-app\main\default\classes\OrderItemTriggerHandler.cls  Method
&gt; does not exist or incorrect signature: void
&gt; handleExWorksCalculations(List&lt;SObject&gt;, Map&lt;Id,SObject&gt;, Boolean)
&gt; from the type OrderItemTriggerHandler (69:33)

I know I missing a fundamental concept as the parameters do match the method signature.



</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

有两个问题在你的代码中:

1. 你需要在最后关闭类。

```apex
public with sharing class OrderItemTriggerHandler extends TriggerHandlerTest1 { //类的开始
    ...代码在这里
} //类的结束
  1. Trigger.oldMap 的映射将返回 <Id,object>,你需要将该映射强制转换为 <Id,OrderItem>。

以下是修复后的代码:

public with sharing class OrderItemTriggerHandler extends TriggerHandlerTest1 {

    public override void beforeInsert() {
        handleExWorksCalculations(Trigger.New, (Map<Id,OrderItem>) Trigger.oldMap, true);
    }

    public static void handleExWorksCalculations(List<OrderItem> orderItemsList, Map<Id,OrderItem> oldOrderItemsMap, Boolean isInsert) {
    }

}
英文:

There are 2 problems in your code

  1. You need to close class at the end.

> public with sharing class OrderItemTriggerHandler extends TriggerHandlerTest1 { //Start of Class
> ...Code will be Here
> } //End of Class

  1. Map of Trigger.oldMap will return <Id,object> and you need to cast that map in to <Id,OrderItem>

Here is the working code

public with sharing class OrderItemTriggerHandler extends TriggerHandlerTest1 { 

    public override void beforeInsert() {
        handleExWorksCalculations(Trigger.New,(Map&lt;Id,OrderItem&gt;) Trigger.oldMap, true);
    }

    public static void handleExWorksCalculations(List&lt;OrderItem&gt; orderItemsList, Map&lt;Id,OrderItem&gt; oldOrderItemsMap, Boolean isInsert) {
    }

}

huangapple
  • 本文由 发表于 2023年3月15日 17:43:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75742923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定