英文:
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'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<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>
# 答案1
**得分**: 1
以下是翻译好的部分:
有两个问题在你的代码中:
1. 你需要在最后关闭类。
```apex
public with sharing class OrderItemTriggerHandler extends TriggerHandlerTest1 { //类的开始
...代码在这里
} //类的结束
- 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
- 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
- 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<Id,OrderItem>) Trigger.oldMap, true);
}
public static void handleExWorksCalculations(List<OrderItem> orderItemsList, Map<Id,OrderItem> oldOrderItemsMap, Boolean isInsert) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论