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
Daniel BleasdaleDaniel Bleasdale 

Error When Preforming Validation On My Apex Class

User-added image
When I go to Validate to set live I get this problem, It dosnt show in the testing and It dosnt cause any issues in the sandbox. I was wondering if anyone could help me solve it?

Controller:-
public class PrintableView
{
   public final Apprentice__c App;
   public PrintableView(ApexPages.StandardController stdController) {
        this.App = (Apprentice__c)stdController.getRecord();
    }  
	public Apprentice__c ApprenticeObj{get;set;}
	public Programme_Area__c ProgramArea {get;set;}
	public Boolean bolPrintableView {get;set;}
           
    public void init() {
 	String strPrintableView = ApexPages.currentPage().getParameters().get('print');
 	bolPrintableView = (strPrintableView == '1');
        Programme_Area__c ProgramArea = new Programme_Area__c();
        ProgramArea.PA_Code__c = '112';
        insert ProgramArea;
        
	}
    public PrintableView(){
        ApprenticeObj = new Apprentice__c();
        ProgramArea = new Programme_Area__c();
        
    }
}
Test Class:-
@isTest

private class PrintViewTest {
    private Apprentice__c ApprenticeObj {get;set;}
    private Boolean bolPrintableView {get;set;}
    public PrintableView Pview {get;set;}
    public Programme_Area__c ProgramArea {get;set;}

    @istest Private Static void ApprenticeObjTest(){
       
		Programme_Area__c ProgramArea = new Programme_Area__c();
        ProgramArea.PA_Code__c = '112';
		insert ProgramArea;
	
		Apprentice__c ApprenticeObj = new Apprentice__c();
		ApprenticeObj.Date_of_Birth__c = date.parse('12/08/2000');
		ApprenticeObj.Programme_area__c = ProgramArea.Id;
		insert ApprenticeObj;
    }
    
    @istest Private Static void StandardTest(){
      Apprentice__c ApprenticeObj = new Apprentice__c();  
      Apexpages.StandardController sc = new Apexpages.standardController(ApprenticeObj);
      PrintableView PrintExt = new PrintableView(sc);
    }
     @istest Private Static void GetSetTest(){
        PrintableView Pview = new PrintableView();
        Pview.init();
        
        // check that an Apprentice__c was created
        List<Apprentice__c> ApprenticeObj = [SELECT Id FROM Apprentice__c];
        List<Programme_Area__c> ProgramArea = [SELECT Id FROM Programme_Area__c]; 
    }       
}

Thanks for any help

 
Best Answer chosen by Daniel Bleasdale
Raj VakatiRaj Vakati
Looks like you have an apex class "Tests" in production whihc refrered PrintableView  class .. update the class and deplooy it or include in the package after testing in sandbox

All Answers

Raj VakatiRaj Vakati
Looks like you have an apex class "Tests" in production whihc refrered PrintableView  class .. update the class and deplooy it or include in the package after testing in sandbox
This was selected as the best answer
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Daniel,  I can't say that I know why you're getting the error you're getting; but try these versions of the code and test classes and see if the error persists.
public class PrintableView
{
    public final Apprentice__c App;
    public PrintableView( ApexPages.StandardController stdController )
    {
        this.App = (Apprentice__c) stdController.getRecord();
    }  

    public Apprentice__c ApprenticeObj      { get; private set; }
    public Programme_Area__c ProgramArea    { get; private set; }

    // this "instance initialization code" runs before the constructor
    {
        ApprenticeObj = new Apprentice__c();
        ProgramArea = new Programme_Area__c();
    }

    public Boolean bolPrintableView
    {
        get { return ApexPages.currentPage().getParameters().get('print') == '1'; }
    }
           
    public void init()
    {
        insert new Programme_Area__c( PA_Code__c = '112' );
    }
}
You can learn more about "instance initialization code" here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_static.htm, search for "Using Initialization Code".
 
@isTest
private class PrintViewTest
{
    //  This doesn't use the controller class at all.  Is it necessary?
    @isTest
    private static void apprenticeObjTest()
    {
        Programme_Area__c ProgramArea = new Programme_Area__c( PA_Code__c = '112' );
        insert ProgramArea;
    
        insert new Apprentice__c
        (   Date_of_Birth__c = Date.newInstance( 2000, 12, 8 )
            Programme_area__c = ProgramArea.Id
        );
    }
    
    @isTest
    private static void standardTest()
    {
        PrintableView printExt = new PrintableView
        (   new Apexpages.standardController
            (   new Apprentice__c()
            )
        );
    }

    @isTest
    private static void getSetTest()
    {
        Test.startTest();
        PrintableView pview = new PrintableView();
        pview.init();
        Test.stopTest();
        
        List<Programme_Area__c> programArea = [SELECT Id FROM Programme_Area__c];
        System.assert( !programArea.isEmpty(), 'Programme_Area__c was not created.' );
    }       
}
I'm not sure why you need the test method "apprenticeObjTest()", as it doesn't cover any of the controller.  Does it test a trigger?