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
❤Code❤Code 

constructorcallsoveriablemethod

Hi All,

I am getting following vioaltion in an extension class. Could you please help me in understanding the vioaltion and how to avoid..

Regards
Best Answer chosen by ❤Code
pradeep naredlapradeep naredla
HI,
 there is nothing wrong with ur code once see the count of flowerbrackets are getting odd no 
public with sharing class CustomForecastPageControllerClass{
   Map<Integer, String> mapOfMonthNumberAndMonthName = New Map<Integer, String>();

   
    public CustomForecastPageControllerClass(ApexPages.StandardController controller) {
       
       mapOfMonthNumberAndMonthName = serviceClass.constructMappingForMonthNumberAndMonth();       
       
        if(mapOfMonthNumberAndMonthName.get(getRangeStart()) != null){ ///////////////////////////error here
            forecastHeader.Period__c = mapOfMonthNumberAndMonthName.get(getRangeStart());     
        }else{
            forecastHeader.Period__c = mapOfMonthNumberAndMonthName.get(system.today().month());     
        }
       
    }
  
    public integer getRangeStart()
    {
        List<Period> periodList = serviceClass.getPeriodList();
        map<Integer, List<Period>> fiscalMapForYear = new map<Integer, List<Period>>();
        Integer year,closeDateyear;
        for(Period period : PeriodList) {
          year = period.StartDate.year();
          if(fiscalMapForYear.get(year) != null) {
              fiscalMapForYear.get(year).add(period);
          } else {
                fiscalMapForYear.put(year, new List<Period> {period});
             }
        }
        closeDateyear= system.today().year();
        if(fiscalMapForYear != null && fiscalMapForYear.get(closeDateyear)!= null){
            for(Period period : fiscalMapForYear.get(closeDateYear)) {
                if(system.today()>= period.StartDate && system.today()<= period.EndDate){
                  return period.number;
                }
            }
        }
        return null;
    }
   
}
Thanks,
pradeep

All Answers

pradeep naredlapradeep naredla
Hi,

There are a few more restrictions that a class must obey to allow inheritance. Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will be invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.

Thanks,
pradeep
❤Code❤Code
Hi Pradeep,

Thanks for the help.. Can u plz give a samll example in Apex...

Regards
pradeep naredlapradeep naredla
Hi,
   post ur code if it is ok with u...

Thanks,
pradeep

❤Code❤Code
Hi Please find below the code....

public with sharing class CustomForecastPageControllerClass{
   Map<Integer, String> mapOfMonthNumberAndMonthName = New Map<Integer, String>();
    }
   
    public CustomForecastPageControllerClass(ApexPages.StandardController controller) {
       
       mapOfMonthNumberAndMonthName = serviceClass.constructMappingForMonthNumberAndMonth();       
       
        if(mapOfMonthNumberAndMonthName.get(getRangeStart()) != null){ ///////////////////////////error here
            forecastHeader.Period__c = mapOfMonthNumberAndMonthName.get(getRangeStart());     
        }else{
            forecastHeader.Period__c = mapOfMonthNumberAndMonthName.get(system.today().month());     
        }
       
    }
  
    public integer getRangeStart()
    {
        List<Period> periodList = serviceClass.getPeriodList();
        map<Integer, List<Period>> fiscalMapForYear = new map<Integer, List<Period>>();
        Integer year,closeDateyear;
        for(Period period : PeriodList) {
          year = period.StartDate.year();
          if(fiscalMapForYear.get(year) != null) {
              fiscalMapForYear.get(year).add(period);
          } else {
                fiscalMapForYear.put(year, new List<Period> {period});
             }
        }
        closeDateyear= system.today().year();
        if(fiscalMapForYear != null && fiscalMapForYear.get(closeDateyear)!= null){
            for(Period period : fiscalMapForYear.get(closeDateYear)) {
                if(system.today()>= period.StartDate && system.today()<= period.EndDate){
                  return period.number;
                }
            }
        }
        return null;
    }
   
}
pradeep naredlapradeep naredla
HI,
 there is nothing wrong with ur code once see the count of flowerbrackets are getting odd no 
public with sharing class CustomForecastPageControllerClass{
   Map<Integer, String> mapOfMonthNumberAndMonthName = New Map<Integer, String>();

   
    public CustomForecastPageControllerClass(ApexPages.StandardController controller) {
       
       mapOfMonthNumberAndMonthName = serviceClass.constructMappingForMonthNumberAndMonth();       
       
        if(mapOfMonthNumberAndMonthName.get(getRangeStart()) != null){ ///////////////////////////error here
            forecastHeader.Period__c = mapOfMonthNumberAndMonthName.get(getRangeStart());     
        }else{
            forecastHeader.Period__c = mapOfMonthNumberAndMonthName.get(system.today().month());     
        }
       
    }
  
    public integer getRangeStart()
    {
        List<Period> periodList = serviceClass.getPeriodList();
        map<Integer, List<Period>> fiscalMapForYear = new map<Integer, List<Period>>();
        Integer year,closeDateyear;
        for(Period period : PeriodList) {
          year = period.StartDate.year();
          if(fiscalMapForYear.get(year) != null) {
              fiscalMapForYear.get(year).add(period);
          } else {
                fiscalMapForYear.put(year, new List<Period> {period});
             }
        }
        closeDateyear= system.today().year();
        if(fiscalMapForYear != null && fiscalMapForYear.get(closeDateyear)!= null){
            for(Period period : fiscalMapForYear.get(closeDateYear)) {
                if(system.today()>= period.StartDate && system.today()<= period.EndDate){
                  return period.number;
                }
            }
        }
        return null;
    }
   
}
Thanks,
pradeep

This was selected as the best answer
pradeep naredlapradeep naredla
HAPPY TO HELP U

thanks,
pradeep