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
Mike ArthurMike Arthur 

Test Class Compile Error

Hi,

I'm writing a test class for a trigger and getting 'Non-void method might not return a value or might have statement after a return statement.'.

Should be a simple thing to fix but I can't see it I'm afraid.  Any advice?

The error is on the line
update taskToUpdate;

Here is the full class:
 
@isTest
                                
public class TestTaskBeyondContractEndDate {

    public static Task createTestTask() { 

        Account a = createAccount();

        Contract con = createContract(a);

        Vist_Detail__c conwk = createContractWork(a, con);

        Task t = createTask(conwk, '2015-07-31');  // Create task with date before Contract end date  

        Task taskToUpdate = [SELECT Id, ActivityDate FROM Task WHERE Id=:t.Id];
        taskToUpdate.ActivityDate = Date.valueOf('2015-12-08');
        update taskToUpdate;
        
   }            
    
// Helper methods

    private static Account createAccount() {
		RecordType rt = [select id,Name from RecordType where SobjectType='Account' and Name='Office' Limit 1];
        Account a = new Account(
            Name='Mike',
            RecordTypeId=rt.Id,
            Region__c='National',
            Status__c='Active',
        	Type='Prospect',
        	CurrencyIsoCode='GBP',
        	Phone='1',
        	Division__c='Water');
        insert a;
        return a;
    }
    
   private static Contract createContract(Account a) {        
		RecordType rt = [select id,Name from RecordType where SobjectType='Contract' and Name='Maintenance Contract' Limit 1];
        Contract con = new Contract(                
            RecordTypeId=rt.Id,
        	AccountId=a.Id,
        	Status='Active',
        	Current_State__c='New',
        	CurrencyIsoCode='GBP',
        	G_Division__c='Water',
            endDate=Date.valueOf('2015-12-31'), // yyyy-mm-dd
        	Contract_Type__c='Maintenance Contract');      
        insert con;
        return con;
    } 

    private static Vist_Detail__c createContractWork(Account a, Contract con) {        
        Vist_Detail__c conwk = new Vist_Detail__c(                
            RecordTypeId='01220000000UVOh',
        	Account__c=a.Id,
            Contract__c=con.ID,
            Service_Owner__c='005w0000003Y7Nl',
        	CurrencyIsoCode='GBP',
        	Division__c='Water',
            Start_Date__c=Date.valueOf('2015-05-08'),
        	Service_Summary__c='Chemistry',
        	Technician_Comments__c='abcd');      
        insert conwk;
        return conwk;
    } 

        private static Task createTask(Vist_Detail__c conwk, string Tdate) {
        Task t = new Task(
            RecordTypeId='01220000000UUkC',
            WhatId=conwk.Id,
            Subject='abcd',
            ActivityDate=Date.valueOf('2015-05-08'),
        	CurrencyIsoCode='GBP',
            Status='Open',
            Activity_Type__c='Service Visit',
            Description='abcd',
            Priority='Normal',
        	Division__c='Water');
        insert t;
        return t;
    }
    
//    private static void updateTask(Task theTask, string theDate){
//        Task taskToUpdate = [SELECT Id, ActivityDate FROM Task WHERE Id=:theTask.Id];
//        taskToUpdate.ActivityDate = Date.valueOf(theDate);
//        update taskToUpdate;
//    }

}



Many Thanks,
Mike.
Best Answer chosen by Mike Arthur
Vivek DeshmaneVivek Deshmane
Hi,
Try below code and let me know if it works.
@isTest                   
public class TestTaskBeyondContractEndDate {

    public static testMethod  void createTestTask() { 

        Account a = createAccount();

        Contract con = createContract(a);

        Vist_Detail__c conwk = createContractWork(a, con);

        Task t = createTask(conwk, '2015-07-31');  // Create task with date before Contract end date  

        Task taskToUpdate = [SELECT Id, ActivityDate FROM Task WHERE Id=:t.Id];
        taskToUpdate.ActivityDate = Date.valueOf('2015-12-08');
        update taskToUpdate;
        
   }            
    
// Helper methods

    private static Account createAccount() {
		RecordType rt = [select id,Name from RecordType where SobjectType='Account' and Name='Office' Limit 1];
        Account a = new Account(
            Name='Mike',
            RecordTypeId=rt.Id,
            Region__c='National',
            Status__c='Active',
        	Type='Prospect',
        	CurrencyIsoCode='GBP',
        	Phone='1',
        	Division__c='Water');
        insert a;
        return a;
    }
    
   private static Contract createContract(Account a) {        
		RecordType rt = [select id,Name from RecordType where SobjectType='Contract' and Name='Maintenance Contract' Limit 1];
        Contract con = new Contract(                
            RecordTypeId=rt.Id,
        	AccountId=a.Id,
        	Status='Active',
        	Current_State__c='New',
        	CurrencyIsoCode='GBP',
        	G_Division__c='Water',
            endDate=Date.valueOf('2015-12-31'), // yyyy-mm-dd
        	Contract_Type__c='Maintenance Contract');      
        insert con;
        return con;
    } 

    private static Vist_Detail__c createContractWork(Account a, Contract con) {        
        Vist_Detail__c conwk = new Vist_Detail__c(                
            RecordTypeId='01220000000UVOh',
        	Account__c=a.Id,
            Contract__c=con.ID,
            Service_Owner__c='005w0000003Y7Nl',
        	CurrencyIsoCode='GBP',
        	Division__c='Water',
            Start_Date__c=Date.valueOf('2015-05-08'),
        	Service_Summary__c='Chemistry',
        	Technician_Comments__c='abcd');      
        insert conwk;
        return conwk;
    } 

        private static Task createTask(Vist_Detail__c conwk, string Tdate) {
        Task t = new Task(
            RecordTypeId='01220000000UUkC',
            WhatId=conwk.Id,
            Subject='abcd',
            ActivityDate=Date.valueOf('2015-05-08'),
        	CurrencyIsoCode='GBP',
            Status='Open',
            Activity_Type__c='Service Visit',
            Description='abcd',
            Priority='Normal',
        	Division__c='Water');
        insert t;
        return t;
    }
    
//    private static void updateTask(Task theTask, string theDate){
//        Task taskToUpdate = [SELECT Id, ActivityDate FROM Task WHERE Id=:theTask.Id];
//        taskToUpdate.ActivityDate = Date.valueOf(theDate);
//        update taskToUpdate;
//    }

}

Best Regards,
-Vivek

All Answers

Vivek DeshmaneVivek Deshmane
Hi,
Try below code and let me know if it works.
@isTest                   
public class TestTaskBeyondContractEndDate {

    public static testMethod  void createTestTask() { 

        Account a = createAccount();

        Contract con = createContract(a);

        Vist_Detail__c conwk = createContractWork(a, con);

        Task t = createTask(conwk, '2015-07-31');  // Create task with date before Contract end date  

        Task taskToUpdate = [SELECT Id, ActivityDate FROM Task WHERE Id=:t.Id];
        taskToUpdate.ActivityDate = Date.valueOf('2015-12-08');
        update taskToUpdate;
        
   }            
    
// Helper methods

    private static Account createAccount() {
		RecordType rt = [select id,Name from RecordType where SobjectType='Account' and Name='Office' Limit 1];
        Account a = new Account(
            Name='Mike',
            RecordTypeId=rt.Id,
            Region__c='National',
            Status__c='Active',
        	Type='Prospect',
        	CurrencyIsoCode='GBP',
        	Phone='1',
        	Division__c='Water');
        insert a;
        return a;
    }
    
   private static Contract createContract(Account a) {        
		RecordType rt = [select id,Name from RecordType where SobjectType='Contract' and Name='Maintenance Contract' Limit 1];
        Contract con = new Contract(                
            RecordTypeId=rt.Id,
        	AccountId=a.Id,
        	Status='Active',
        	Current_State__c='New',
        	CurrencyIsoCode='GBP',
        	G_Division__c='Water',
            endDate=Date.valueOf('2015-12-31'), // yyyy-mm-dd
        	Contract_Type__c='Maintenance Contract');      
        insert con;
        return con;
    } 

    private static Vist_Detail__c createContractWork(Account a, Contract con) {        
        Vist_Detail__c conwk = new Vist_Detail__c(                
            RecordTypeId='01220000000UVOh',
        	Account__c=a.Id,
            Contract__c=con.ID,
            Service_Owner__c='005w0000003Y7Nl',
        	CurrencyIsoCode='GBP',
        	Division__c='Water',
            Start_Date__c=Date.valueOf('2015-05-08'),
        	Service_Summary__c='Chemistry',
        	Technician_Comments__c='abcd');      
        insert conwk;
        return conwk;
    } 

        private static Task createTask(Vist_Detail__c conwk, string Tdate) {
        Task t = new Task(
            RecordTypeId='01220000000UUkC',
            WhatId=conwk.Id,
            Subject='abcd',
            ActivityDate=Date.valueOf('2015-05-08'),
        	CurrencyIsoCode='GBP',
            Status='Open',
            Activity_Type__c='Service Visit',
            Description='abcd',
            Priority='Normal',
        	Division__c='Water');
        insert t;
        return t;
    }
    
//    private static void updateTask(Task theTask, string theDate){
//        Task taskToUpdate = [SELECT Id, ActivityDate FROM Task WHERE Id=:theTask.Id];
//        taskToUpdate.ActivityDate = Date.valueOf(theDate);
//        update taskToUpdate;
//    }

}

Best Regards,
-Vivek
This was selected as the best answer
Mike ArthurMike Arthur
Hi Vivek,

I just figured it out and I think your answer will resolve it too.  I found another post where the resolution was similar.

The error is not on the line where it is indicated.  It occurs because the method is defined to return a Task but no Task was being returned.  The error was indicated on the last executable line. 

If I either remove the return Task from the method definition or return a Task at the end of the method the error goes away.  I don't need to return a Task so I have removed it, as per your suggestion Vivek.

Many Thanks,
Mike.