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
BillPowell__cBillPowell__c 

VF Custom Cont. Test Class for Custom Object Help

I can't write code for the life of me but can usually plug in values and follow a pattern to have it do what I need it to. Had some help with a VF page, controller, and test class, but cant seem to get the test class to save. Can someone take a look at let me know what to change to make it pass? Thanks

VF Page

<apex:page controller="SampleController">
    <apex:form >
        <apex:sectionHeader title="Mass Create Sample Order Line Items"></apex:sectionHeader>
        <apex:pageBlock title="Sample Order Edit" mode="edit">

<!--SAMPLE ORDER RECORD SECTION & FIELDS-->  
            
			<apex:pageBlockSection title="Sample Order Information" collapsible="false" columns="2">              
                	<apex:inputField label="Project Name" value="{!sampleOrder.Project_Name__c}" required="true"/>
                	<apex:inputfield label="Order Status" value="{!sampleOrder.Status__c}" required="false"/>
                	<apex:inputField label="Sales Rep" value="{!sampleOrder.McGrory_Sales_Rep__c}" required="true" />
                	<apex:outputfield label="Order Date" value="{!sampleOrder.Request_Date__c}"/>
                	<apex:inputField label="Ship To" value="{!sampleOrder.Ship_To__c}" required="true"/>
                	<apex:inputfield label="Ship Date" value="{!sampleOrder.Ship_Date__c}" required="false"/>
                	<apex:inputField label="Notes" value="{!sampleOrder.Notes__c}" required="false"/> 
            </apex:pageBlockSection>
                        
            <apex:pageBlockSection title="Customer Information" collapsible="false" columns="2">
                	<apex:inputfield label="Company" value="{!sampleOrder.Company__c}" required="True"/>
                	<apex:outputfield label="Address" value="{!sampleOrder.Address__c}"/>
                	<apex:inputfield label="Contact" value="{!sampleOrder.Contact__c}" required="True"/>
               		<apex:inputfield label="Contact Phone" value="{!sampleOrder.Contact_Phone__c}"/>
                	<apex:inputField label="Contact Email" value="{!sampleOrder.Contact_Email__c}"/>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Alternate Contact Information - If Not Shipping To Sales Rep Or Customer" collapsible="false" columns="2">
                	<apex:inputfield label="Alternate Company" value="{!sampleOrder.Alternate_Company__c}" required="False"/>
                	<apex:inputfield label="Street Address" value="{!sampleOrder.Street_address__c}" required="False"/>
                	<apex:inputfield label="Alternate Contact" value="{!sampleOrder.Alternate_Contact__c}" required="False"/>
                	<apex:inputfield label="City" value="{!sampleOrder.City__c}" required="False"/>
                	<apex:inputfield label="Alternate Phone" value="{!sampleOrder.Alternate_Phone__c}" required="False"/>
                	<apex:inputfield label="State" value="{!sampleOrder.State__c}" required="False"/>
                	<apex:inputfield label="Alternate Email" value="{!sampleOrder.Alternate_Email__c}" required="False"/>
                	<apex:inputfield label="Zip/Postal Code" value="{!sampleOrder.Zip_Postal_Code__c}" required="False"/>               	              
            </apex:pageBlockSection>
            
<!--SAMPLE ORDER LINE ITEMS TABLE-->
            <apex:pageBlockSection title="Sample Order Line Items">
                <apex:pageBlockTable id="tblLineItems" value="{!sampleOrderLineItems}" var="soli">
                    <apex:column >
                        <apex:inputField value="{!soli.Quantity__c}"></apex:inputField>
                        <apex:facet name="header">
                            Quantity
                        </apex:facet>
                    </apex:column>
                    <apex:column >
                        <apex:inputField value="{!soli.Description__c}"></apex:inputField>
                        <apex:facet name="header">
                            Description
                        </apex:facet>
                    </apex:column>
                    <apex:column >
                        <apex:inputField value="{!soli.Width__c}"></apex:inputField>
                        <apex:facet name="header">
                            Width
                        </apex:facet>
                    </apex:column>
                    <apex:column >
                        <apex:inputField value="{!soli.Height__c}"></apex:inputField>
                        <apex:facet name="header">
                            Height
                        </apex:facet>
                    </apex:column>
                    <apex:column >
                        <apex:inputField value="{!soli.Product_Code__c}"></apex:inputField>
                        <apex:facet name="header">
                            Product Code
                        </apex:facet>
                    </apex:column>
                    <apex:facet name="footer">
                        <apex:commandButton value="Add Line" immediate="true" action="{!addLineItem}" rerender="tblLineItems"></apex:commandButton>
                    </apex:facet>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"></apex:commandButton>
                <apex:commandButton value="Cancel" action="{!cancel}"></apex:commandButton>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Custom Controller
public class SampleController {
    public Sample_Order__c sampleOrder  { get; set; }
    public List<Sample_Order_Line_Items__c> sampleOrderLineItems     { get; set; }
    
    public SampleController() {
        sampleOrder             = new Sample_Order__c();
        sampleOrderLineItems    = new List<Sample_Order_Line_Items__c>{ new Sample_Order_Line_Items__c() };
    }
    
    public void addLineItem() {
        sampleOrderLineItems.add( new Sample_Order_Line_Items__c() );
    }
    
    public PageReference save() {
        INSERT sampleOrder;
        
        for( Sample_Order_Line_Items__c soli : sampleOrderLineItems ) {
            soli.Sample_Order__c = sampleOrder.Id;
        }
        
        INSERT sampleOrderLineItems;
        
        return new PageReference( '/' + sampleOrder.Id );
    }
    
    public PageReference cancel() {
        return new PageReference( '/home/home.jsp' );
    }
}

Test Class (not working, can't figure out what to add/remove/edit) 
@isTest
private class SampleController_UT
{
	private static testmethod void testMethod1()
	{
		
		SampleController  soObj = new SampleController();
		soObj.addLineItem();
		soObj.cancel();
		soObj.Sample_Order__c = 'Test';
        soObj.Quantity__c = 1;
        soObj.Project_name__c = 'Quote Testing Project';
        soObj.McGrory_Sales_Rep__c = 'Bill Powell';
        soObj.Ship_To__c = 'Customer';

		
		try
		{
			soObj.save();
		}
		catch(Exception ee)
		{}
		
	}
	
}

 
Hemant_SoniHemant_Soni
Hi BillPowell,
I think this may help you.
@isTest 
public class SampleController_UT 
{
 static testMethod void testMethod1() 
 {
 Sample_Order_c  oSampleOrder = new Sample_Order_c ();
 oSampleOrder .Name='Test SampleOrder' ;
 //Add your required field

 list<Sample_Order_Line_Items_c> lstSampleOrderLineItems = new list<Sample_Order_Line_Items_c>();
 Sample_Order_Line_Items_c oSampleOrderLineItems = new Sample_Order_Line_Items_c();
 oSampleOrderLineItems.Name = 'Test';
 //Add your required field
 lstSampleOrderLineItems.add(oSampleOrderLineItems);
 
 
 Test.StartTest(); 

  PageReference pageRef = Page.testpage; // Add your VF page Name here
  Test.setCurrentPage(pageRef);

  SampleController  oSampleController  = new SampleController ();
  oSampleController.sampleOrder  = oSampleOrder;
  oSampleController.sampleOrderLineItems = lstSampleOrderLineItems; 
  oSampleController.save();
  oSampleController.cancel();
  oSampleController.addLineItem();  
 Test.StopTest();
 }
}
Thanks
Hemant
 
BillPowell__cBillPowell__c
Thanks all, that helped. I had to create records for 2 other objects the "sample order" looked up to, but I thankfully figured it out based on the framing you gave me. Much appreciated. Do have a question though, I need to create a "New" list view button so I can use it on related lists to override the standard "new" button. How would I accomplish this with the VF page and custom controller I have?
BillPowell__cBillPowell__c
Thanks Hemant, I actually got it to save. Forgot that I had to create the records that the lookups were related to. Thanks anyway