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
TC AdminTC Admin 

Help writing Test class code for SOQL and override save button

Hi, I'm struggling to write a test class that has over 75% code coverage. I currently only have 57% since adding an override to the save button.
Before the save button I have 82%.
I thought I just needed to add the save method to the class but obviously not. Any help would be hugly welcome.

APEX CLASS CONTROLLER
PUBLIC CLASS TimeSheetEditAllItemsList {
PUBLIC LIST <Time_sheet_items__c> GetItemsList {get;set;}
PUBLIC TimeSheetEditAllItemsList(ApexPages.StandardController ctrl){ 
 GetItemsList = [
        SELECT Id,Time_Sheet__c,D_Product_Manager__c,D_Type__c,D_Project__c,Day_Monday__c,Day_Tuesday__c,Day_Wednesday__c,
Day_Thursday__c,Day_Friday__c,Day_Weekend__c,Day_Monday_Comment__c,Day_Tuesday_Comment__c,Day_Wednesday_comment__c,Day_Thursday_Comment__c,Day_Friday_Comment__c,Day_Weekend_Comment__c 
        FROM Time_Sheet_Items__c 
        WHERE       Time_Sheet__c= : ctrl.getId() ORDER BY  id ASC
        ];
    }
    public pageReference save(){
        UPSERT GetItemsList ;
        return null;
}
}

 

TEST CLASS
@isTest

Private class TestClassTimeSheetEditAllItemsList {

    static testMethod void GetItemsList() {
        
        // create new TS record to test against
        Time_Sheet__C TS = new Time_Sheet__c();
        TS.Time_sheet_week_ending__C = date.Newinstance (2016,06,06);
        TS.Employee__C ='a0Gb000000Cm6OHEAZ';
        insert TS;

        // create a line item for the TS to test against
        Time_Sheet_Items__c TSi = new TIme_Sheet_Items__c();
        TSi.D_project__C ='011 GFX';
        TSi.D_Type__c = '05 Editorial';
        TSi.Time_Sheet__c =TS.Id;
        TSi.RecordTypeid ='012b00000009cosAAA';
        insert TSi;

        // Construct the standard controller
        ApexPages.StandardController con = new ApexPages.StandardController(TS);
        
         // create the controller
        TimeSheetEditAllItemsList ext = new TimeSheetEditAllItemsList(con);
        
        //Start the test
         Test.startTest();
         
        // Check that Time sheets created and can query data with items lines
        Time_Sheet_Items__c TSiL=[
                    SELECT Id,Time_Sheet__c,Day_Monday__c,Day_Tuesday__c,Day_Wednesday__c,Day_Thursday__c,Day_Friday__c,Day_Weekend__c,Day_Monday_Comment__c,Day_Tuesday_Comment__c,Day_Wednesday_comment__c,Day_Thursday_Comment__c,Day_Friday_Comment__c,Day_Weekend_Comment__c 
                    FROM Time_Sheet_Items__c 
                    ];
		// Check the save works
		con.save();
        // Check it ran
        System.assert(TSiL!=null);   
        
        // Stop test
        Test.stopTest();
    }

}

Best Answer chosen by TC Admin
Khan AnasKhan Anas (Salesforce Developers) 

Hi,

Greetings to you! It is a pleasure to be in touch with you again.

You need to use ext.save(); instead of con.save(); because ext is instance of class.

Try below code:

@isTest

Private class TestClassTimeSheetEditAllItemsList {

    static testMethod void GetItemsList() {
        
        // create new TS record to test against
        Time_Sheet__C TS = new Time_Sheet__c();
        TS.Time_sheet_week_ending__C = date.Newinstance (2016,06,06);
        TS.Employee__C ='a0Gb000000Cm6OHEAZ';
        insert TS;

        // create a line item for the TS to test against
        Time_Sheet_Items__c TSi = new TIme_Sheet_Items__c();
        TSi.D_project__C ='011 GFX';
        TSi.D_Type__c = '05 Editorial';
        TSi.Time_Sheet__c =TS.Id;
        TSi.RecordTypeid ='012b00000009cosAAA';
        insert TSi;

        // Construct the standard controller
        ApexPages.StandardController con = new ApexPages.StandardController(TS);
        
         // create the controller
        TimeSheetEditAllItemsList ext = new TimeSheetEditAllItemsList(con);
        
        //Start the test
         Test.startTest();
         
        // Check that Time sheets created and can query data with items lines
        Time_Sheet_Items__c TSiL=[
                    SELECT Id,Time_Sheet__c,Day_Monday__c,Day_Tuesday__c,Day_Wednesday__c,Day_Thursday__c,Day_Friday__c,Day_Weekend__c,Day_Monday_Comment__c,Day_Tuesday_Comment__c,Day_Wednesday_comment__c,Day_Thursday_Comment__c,Day_Friday_Comment__c,Day_Weekend_Comment__c 
                    FROM Time_Sheet_Items__c 
                    ];
		// Check the save works
		ext.save();
        // Check it ran
        System.assert(TSiL!=null);   
        
        // Stop test
        Test.stopTest();
    }

}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 

Hi,

Greetings to you! It is a pleasure to be in touch with you again.

You need to use ext.save(); instead of con.save(); because ext is instance of class.

Try below code:

@isTest

Private class TestClassTimeSheetEditAllItemsList {

    static testMethod void GetItemsList() {
        
        // create new TS record to test against
        Time_Sheet__C TS = new Time_Sheet__c();
        TS.Time_sheet_week_ending__C = date.Newinstance (2016,06,06);
        TS.Employee__C ='a0Gb000000Cm6OHEAZ';
        insert TS;

        // create a line item for the TS to test against
        Time_Sheet_Items__c TSi = new TIme_Sheet_Items__c();
        TSi.D_project__C ='011 GFX';
        TSi.D_Type__c = '05 Editorial';
        TSi.Time_Sheet__c =TS.Id;
        TSi.RecordTypeid ='012b00000009cosAAA';
        insert TSi;

        // Construct the standard controller
        ApexPages.StandardController con = new ApexPages.StandardController(TS);
        
         // create the controller
        TimeSheetEditAllItemsList ext = new TimeSheetEditAllItemsList(con);
        
        //Start the test
         Test.startTest();
         
        // Check that Time sheets created and can query data with items lines
        Time_Sheet_Items__c TSiL=[
                    SELECT Id,Time_Sheet__c,Day_Monday__c,Day_Tuesday__c,Day_Wednesday__c,Day_Thursday__c,Day_Friday__c,Day_Weekend__c,Day_Monday_Comment__c,Day_Tuesday_Comment__c,Day_Wednesday_comment__c,Day_Thursday_Comment__c,Day_Friday_Comment__c,Day_Weekend_Comment__c 
                    FROM Time_Sheet_Items__c 
                    ];
		// Check the save works
		ext.save();
        // Check it ran
        System.assert(TSiL!=null);   
        
        // Stop test
        Test.stopTest();
    }

}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
TC AdminTC Admin
One again Khan you come to my rescure. Thank you for explaining what my errowas as well. it really helps me understand for furture Apex endeavours.