Search code examples
salesforceapex

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 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, 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.


Solution

  • 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<Id,OrderItem>) Trigger.oldMap, true);
        }
    
        public static void handleExWorksCalculations(List<OrderItem> orderItemsList, Map<Id,OrderItem> oldOrderItemsMap, Boolean isInsert) {
        }
    
    }