function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jos Vervoorn 2Jos Vervoorn 2 

ERROR: Static method cannot be referenced from a non static context

While performing validation on my Apex Trigger & helper class I run into the following error:

Static method cannot be referenced from a non static context: void OpportunityLineItemTriggerHandler.AutoScheduleLineItemsAfterUpdate(List<OpportunityLineItem>, Map<Id,OpportunityLineItem>)

Now in test sandbox ... I did not run into this error .. so I'm not sure why in production it fails ...
Trigger code:
OpportunityLineItemTriggerHandler updater = new OpportunityLineItemTriggerHandler();

Apex class code (first x lines ......)
Public void AutoScheduleLineItemsAfterInsert(List<OpportunityLineItem> NewOppLineItems)
  {
     System.debug('** * ** #20 AutoScheduleLineItemsAfterInsert');
     //** Get prepped by retrieving the base information needed for generating schedule
     Date currentDate;             
     Decimal NumberOfInstallments; //**Decimal numberPayments;
     Decimal paymentAmount;
     Decimal totalPaid;
     String ItemDescription;
      
     NumberOfInstallments = 1; //** Ensure default value for installments even when no value provided.
          
     //** Generate lists for bulkify on OpportunityItems etc. 
     set<id> oppid = new set<id>();
     List<Id> oppLineIdsList = new List<Id>();
     List<OpportunityLineItemSchedule> newScheduleObjects = new List<OpportunityLineItemSchedule>();
     
     //** Assign (new) added ID values to variables
     for (OpportunityLineItem ol : NewOppLineItems) {
          oppLineIdsList.add(ol.id);
          oppid.add(ol.opportunityid);
     }
     //** Create (parent) opportunity list so we have required into to retrieve account based product (pricebook) values.
     list<opportunity> opplist = [select id, AccountID,CurrencyIsoCode from opportunity where id in : oppid ];
     
    for (opportunity opp: opplist) {
         //** So we have all we need. Account, Opportunity currency and ProductID
         system.debug('** * ** #44 AccountID: '+opp.AccountID);
         system.debug('** * ** #45 Opportunity Currency: '+opp.CurrencyIsoCode);
           
         //** For every OpportunityLineItem record, add its associated pricebook entry
         //** to a set so there are no duplicates.
         Set<Id> pbeIds = new Set<Id>();             //** PriceBookEntryID
         for (OpportunityLineItem oli : NewOppLineItems) 
            //** pbeIds.add(oli.pricebookentryid);
            pbeIds.add(oli.id);
            //** For every OpportunityLineItem record, add its associated oppty
            //** to a set so there are no duplicates.
            Set<Id> opptyIds = new Set<Id>();
            for (OpportunityLineItem oli : NewOppLineItems) 
                 opptyIds.add(oli.OpportunityId);

Thanks in advance.
 
Best Answer chosen by Jos Vervoorn 2
Jos Vervoorn 2Jos Vervoorn 2
I found the error. It was caused by the fact that I changed the class but forgot to (re)submit the trigger. So in production, it failed while in test/dev it also failed after resubmitting. So it was caused invalid save state.

All Answers

Marcelo CostaMarcelo Costa
Hi Jos!!
This error states that at some point in your code, you are trying to call a Static method from an instantiated object.
is Any method in your trigger handler static? If so, how are you invoking it?
Static methods in salesforce a little different (and in my opinion more useful) than static methods in other programming languages, but conditional invocation of static methods from an instantiated object might give this error you are getting.
good luck!
Jos Vervoorn 2Jos Vervoorn 2
Thanks Marcelo,

The handler has 'only' two methods. 
Public static void AutoScheduleLineItemsAfterInsert(List<OpportunityLineItem> NewOppLineItems)
Public static void AutoScheduleLineItemsAfterUpdate(List<OpportunityLineItem> NewOppLineItems, Map<ID,OpportunityLineItem> OldOppLineItems)

Both are called from the trigger.

      else if (Trigger.isAfter){
              if(Trigger.isUpdate){
                 //Code to execute after update                  
                 OpportunityLineItemTriggerHandler updater = new OpportunityLineItemTriggerHandler();
                 updater.AutoScheduleLineItemsAfterUpdate(Trigger.new,Trigger.oldMap);
              }
              if(Trigger.isInsert){
                 //Code to execute after insert
                 OpportunityLineItemTriggerHandler.AutoScheduleLineItemsAfterInsert(Trigger.new);                  
              }
Jos Vervoorn 2Jos Vervoorn 2
I found the error. It was caused by the fact that I changed the class but forgot to (re)submit the trigger. So in production, it failed while in test/dev it also failed after resubmitting. So it was caused invalid save state.
This was selected as the best answer
Marcelo CostaMarcelo Costa
Jos, the code for Trigger.isUpdate is wrong...
you are calling the static method from an instantiated object :)