0

I am using a trigger framework that works quite well. However, I don't know how to call a method on a trigger handler class from another trigger handler class. I am trying to avoid a separate helper class. I have tried a wide variety of things to no avail. The example below (not all the code), raises the following error "Error: execution of BeforeUpdate caused by: System.TypeException: Invalid conversion from runtime type List<Calc__c> to List<Inventory__c>:". It appears that I may have to cast somehow. Any help will be much appreciated.

//Class 1
    
    public class calcTriggerHandler extends TriggerHandler {
    
       public calcTriggerHandler() {
       }
    
       public override void beforeupdate(){
         updateInventory();
       }
    
       public Id inventoryId;
    
        /*...other code....*/
    
       private void updateInventory() { 
         invTriggerHandler inventory = new invTriggerHandler();
         Boolean isQtyUpdated = inventory.rollUpQuantity(inventoryId);
       }
    }
    
    //----------------
    
    //Class 2
    
    public class invTriggerHandler extends TriggerHandler {
    
        //Constructor
        public invTriggerHandler() {
        }
     
        public override void beforeupdate(){
            rollUpQuantity(invId);  
        }
        
        public Id invId;
        public Boolean qtyUpdated;
    
        public Boolean rollUpQuantity(Id invId){
      
            for(Inventory__c inv: (List<Inventory__c>)Trigger.New){
               if(inv.Qty_Error__c == false){
                  qtyUpdated = true;    
                }            
            } 
    
            return qtyUpdated;                      
        }
    }
1
  • this is why the Lightning Platform Enterprise Architecture was invented (see Andrew Fawcett) - domain, service, selector, unit of work layers
    – cropredy
    Aug 21, 2020 at 22:29

1 Answer 1

0

You won't be able to call this method without fixing it, first:

public Boolean rollUpQuantity(Id invId){
    for(sObject inv: Trigger.New){
       if(inv.get('Qty_Error__c') == false){
          qtyUpdated = true;    
        }
    } 
    return qtyUpdated;
}

If the field is different for each object, you'd also need to provide that as well:

public Boolean rollUpQuantity(Id invId, sobjectField field){
    for(sObject inv: Trigger.New){
       if(inv.get(field) == false){
          qtyUpdated = true;    
        }
    } 
    return qtyUpdated;
}

At some point, you may find it beneficial to actually start using helper methods/classes. There's nothing inherently wrong with using them, and you'll gain a lot of utility out of them if done well.

1
  • Thank you. I will take your suggestion and move forward with a helper class.
    – Snights
    Aug 21, 2020 at 22:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .