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
James DearmerJames Dearmer 

Testing VisualForce Apex Class Custom Controller

I have a VisualForce page that is using the ServiceAppointment object (from FSL) to create multiple records from one page.

Everything works fine functionally. All that is left is finishing off my Apex Testing Class to make it Valid. I think it is failing because the fields inside the VF page are not being populated but I can't seem to work out how!

VisualForce page:
<apex:page controller="AddmultipleServiceAppointmentController" showHeader="true" setUp="true" showChat="false" sidebar="false">
  <apex:form >
    <apex:pageBlock >
      <apex:variable var="rowNum" value="{!0}"  />        
        <apex:pageBlockTable value="{!ListServiceAppointment}" var="a">
               
            <apex:column headerValue="Job">
                <apex:inputField value="{!a.ParentRecordId}"/>
            </apex:column>
            <apex:column headerValue="Service Crew">
                <apex:inputField value="{!a.Service_Crew__c}"/>
            </apex:column>
            <apex:column headerValue="Scheduled Start Time">
                <apex:inputField value="{!a.SchedStartTime}"/>
            </apex:column>
            <apex:column headerValue="Scheduled End Time">
                <apex:inputField value="{!a.SchedEndTime}"/>
            </apex:column>
            
             
            
        </apex:pageBlockTable>
        <apex:pageBlockButtons >
            <apex:commandButton value="Add Service Appointment Row" action="{!addServiceAppointment}"/>
            <apex:commandButton value="Save Service Appointments" action="{!saveServiceAppointment}"/>
            
        </apex:pageBlockButtons>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Apex Class:
 
public class AddmultipleServiceAppointmentController {
    public List<ServiceAppointment> ListServiceAppointment{
        get; 
        set;
    }
    //public Integer rowNum{get;set;}
    
    //public Id oppid{get;set;}
    string said = apexpages.currentpage().getparameters().get('saia');
    
    public AddmultipleServiceAppointmentController(){
        ListServiceAppointment = new List<ServiceAppointment>();
        ServiceAppointment sa= new ServiceAppointment();
        sa.ParentRecordId = said;
        ListServiceAppointment.add(sa);
    }
    
    public void addServiceAppointment(){
        ServiceAppointment sa = new ServiceAppointment();

        if(Test.isRunningTest())
        {
            // add fields here
            sa.ParentRecordId='0WO6E000000KHWlWAO';
            sa.Status = 'None';
            sa.EarliestStartTime = datetime.now();
            sa.DueDate = datetime.now() + 30;
            sa.Job_Type__c = 'Fire';        
            sa.SchedStartTime = datetime.now();
            sa.SchedEndTime = datetime.now();
        }
        ListServiceAppointment.add(sa);
    }

    public PageReference saveServiceAppointment(){
        insert ListServiceAppointment;
        return Page.AllServiceAppointmentssaved;
       
    }
    
  
}

Apex Testing Class:
 
@isTest 
public class testServiceAppointmentsTriggers 
{
    static testMethod void AddmultipleServiceAppointmentController() 
    {
        ServiceAppointment testServiceAppointment = new ServiceAppointment();
        testServiceAppointment.ParentRecordId='0WO6E000000KHWlWAO';
        testServiceAppointment.Status = 'None';
        testServiceAppointment.EarliestStartTime = datetime.now();
        testServiceAppointment.DueDate = datetime.now() + 30;
        testServiceAppointment.Job_Type__c = 'Fire';
        
        testServiceAppointment.SchedStartTime = datetime.now();
        testServiceAppointment.SchedEndTime = datetime.now();
            
        insert testServiceAppointment;
        
        Test.StartTest(); 
            ApexPages.currentPage();
            AddmultipleServiceAppointmentController testAccPlan = new AddmultipleServiceAppointmentController();
			
        	testAccPlan.addServiceAppointment();
            testAccPlan.saveServiceAppointment();
                        
        Test.StopTest();
    }
}

​​​​​​​I think i know what the issue is - i am not populating the fields inside the visualforce page before it gets submitted but I'm unsure how to do this. My code coverage using this apex class is near 95% so it's clearly running everything as it should.

A lot of different examples that I've seen are using query string parameters in a slightly different situation. If anyone could give me a helping hand, it would be much appreciated!
Best Answer chosen by James Dearmer
James DearmerJames Dearmer
For anyone else who gets stuck on this, I was missing out the Test.isRunningTest() command.
 
if(Test.isRunningTest()) {
            // add fields here
            sa.ParentRecordId='0WO1r000006nAlLGAU';
            sa.Status = 'None';
            sa.EarliestStartTime = datetime.now();
            sa.DueDate = datetime.now() + 30;
            sa.Job_Type__c = 'Fire';        
            sa.SchedStartTime = datetime.now();
            sa.SchedEndTime = datetime.now();
}

Hope this helps anyone needing it later on down the line!

James Dearmer
Web Design Kent (https://rushax.com)