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
Chaim Rosenfeld 8Chaim Rosenfeld 8 

need help in this visualforce and controller's test c;lass

Hello , i need help in test class of this visualforce page and controller, 

<apex:page controller="addJobCntrl"  sidebar="false" showHeader="false" >
<style>
.picklist{
 border:none;
 background-color : #ECECEC;
  width: 100%;
  padding: 12px 20px;

}
.picklist2{
 border:none;
 background-color : #ECECEC;
  width: 100%;
  padding: 12px 20px;

}

.lbl{
text-align:left;
font-family:Arial;
font-size:15px;
font-weight:bold;


}
.inp1 {
     border:none;
     background-color : #ECECEC;
     padding: 12px  30px;
     font-family:Arial;


  
}
.btn{

background-color: #4CAF50; /* Green */
    border: none;
    color: white;
    padding: 8px 20px;
    text-align: center;
    text-decoration: none ;
    display: inline-block;
    font-size: 16px;



}

</style>
<apex:form >
<div align="center">
<apex:outputLabel value="Please choose Customer from below list and Click Create Job Button" styleClass="lbl" ></apex:outputLabel>
<br/>
<br/>
<apex:inputField value="{!jobIns.Customer__c}" label="" id="Customer"/>
<br/>
<apex:commandButton action="{!creatContract}"  value="Create Job" styleClass="btn"/>  
</div>
</apex:form>  
  
  
</apex:page>

This is its Controller 

public class addJobCntrl {

Public string leadID{get;set;}
Public Lead leadIns{get;set;}
Public Job__c jobIns{get;set;}


public addJobCntrl (){

leadID = apexpages.currentpage().getparameters().get('id');
leadIns= [select id, Company, State__c, Street__c , Zip__c , City__c , Type_of_Service_Proposal__c , Name  , Sales_Person__c, Insurance_Amount__c , Email, Credit_Application_received__c, Credit_Limit_Amount__c,  Phone from Lead where ID =: leadID ];
jobIns = new Job__c(Lead__c  = leadID );


}


Public PageReference creatContract(){


jobIns.Name = leadIns.Company;
jobIns.Sales_Rep__c = leadIns.Sales_Person__c;
jobIns.Billing_Contact__c = leadIns.Name  ;
jobIns.Billing_Email__c = leadIns.Email;
jobIns.Billing_Phone_Number__c =  leadIns.Phone;
jobIns.Street__c = leadIns.Street__c ;
jobIns.State__c = leadIns.State__c ;
jobIns.Zipcode__c = leadIns.Zip__c ;
jobIns.City__c = leadIns.City__c ;
jobIns.Service_Type__c = leadIns.Type_of_Service_Proposal__c ;
jobIns.Credit_Application_received__c = leadIns.Credit_Application_received__c;
jobIns.Credit_Amount_Limit__c = leadIns.Credit_Limit_Amount__c;
jobIns.Insurance_Amount__c = leadIns.Insurance_Amount__c;

upsert jobIns;



PageReference p = new PageReference('/' + this.jobIns.Id );
return p;
}
}


and this is its test class 

@isTest 
public class addJobCntrlTest{

static testMethod void testMethod1() {
       Account custIns = new Account();
       custIns.Name = 'test';
       custIns.QB_Company__c = 'qfs';
       custIns.QB_Account__c = 'test'; 
       custIns.Service_Type__c  = 'Mid-Construction';
       insert custIns;
       
       Job__c jobIns = new Job__c();
       jobIns.Name = 'test';
       jobIns.Sales_Rep__c = 'Aharon Kaplan';
       jobIns.Customer__c = custIns.ID;
       jobIns.Billing_Contact__c = 'test';  
       jobIns.Billing_Email__c= 'test@test.com'; 
       jobIns.Billing_Phone_Number__c= '123'; 
       jobIns.QB_Job_Name__c = 'test';   
       jobIns.QB_Company__c = 'QFS';
       
       insert jobIns;
       
       
        
       Lead testRecord= new Lead();
       testRecord.LastName = 'test name ';
       testRecord.Company = 'test company';
       testRecord.Status = 'new';
       testRecord.Credit_Limit_Amount__c = 12344;
       insert testRecord;
     

        Test.setCurrentPage(Page.addJob);
        System.currentPageReference().getParameters().put('id', testRecord.Id);

        addJobCntrl addjobCntrlTestIns = new addJobCntrl();
        
        addjobCntrlTestIns.creatContract();
    }
 }

this test class is giving this error. 

System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Customer__c]: [Customer__c]

I am not sure why i keep getting this error, i am getting customer__c value from visualforce page's input field from user, visualforce page is giving / creating correct records, but in test class it says customer__c value is required , 

can any one pleaes help me in this at your earliest possible ???? 
Guru Vemuru 11Guru Vemuru 11
Hi Chaim Rosenfeld,
First thing you need to undustand you are writing test class for controller not Vf.
You are geeting this error because in your test class Customer__c is missing which is mandatory.
so add this in you test class and run.


Thanks
Guru Vemuru
Raj VakatiRaj Vakati
Customer__c  is required on lead as well ?? can you check that and try this code 
 
@isTest 
public class addJobCntrlTest{

static testMethod void testMethod1() {
	
       Account custIns = new Account();
       custIns.Name = 'test';
       custIns.QB_Company__c = 'qfs';
       custIns.QB_Account__c = 'test'; 
       custIns.Service_Type__c  = 'Mid-Construction';
       insert custIns;
       
       Job__c jobIns = new Job__c();
       jobIns.Name = 'test';
       jobIns.Sales_Rep__c = 'Aharon Kaplan';
       jobIns.Customer__c = custIns.ID;
       jobIns.Billing_Contact__c = 'test';  
       jobIns.Billing_Email__c= 'test@test.com'; 
       jobIns.Billing_Phone_Number__c= '123'; 
       jobIns.QB_Job_Name__c = 'test';   
       jobIns.QB_Company__c = 'QFS';
       insert jobIns;
       Lead testRecord= new Lead();
       testRecord.LastName = 'test name ';
       testRecord.Company = 'test company';
       testRecord.Status = 'new';
	   testRecord.Customer__c  =  custIns.ID;
       testRecord.Credit_Limit_Amount__c = 12344;
       insert testRecord;
     
		Test.setCurrentPage(Page.addJob);
        System.currentPageReference().getParameters().put('id', testRecord.Id);

        addJobCntrl addjobCntrlTestIns = new addJobCntrl();
        
        addjobCntrlTestIns.creatContract();
    }
 }