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
SFDC-NOOBSFDC-NOOB 

Test Code Coverage

Greetings,

 

I am at 68% and I can't seem to get up to 75% code coverage for my test class.  The code in red is NOT covered.  My code is as follows.

 

Note: I get the following error when testing:::: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]      //(LINE 17)

 

 

 

**********************************************Visualforce page**************************************

<apex:page controller="multirecords" tabstyle="Account">

<div style="font-size:12px">
Account Edit
</div>
<div style="font-size: 24px">
<B>New Account</B><br></br>
</div>

<apex:form id="TheForm">

<apex:pageblock >
<head>
<style>
div.pbSubheader{
font-size:13px;
}
</style>
</head>

<apex:pageMessages ></apex:pageMessages>
<apex:pageblocksection title="Account Information">
<apex:inputField Value="{!A.name}" required="true" taborderhint="1"/>
<apex:inputfield Value="{!A.type}" taborderhint="7"/>
<apex:inputfield value="{!A.phone}" required="true" taborderhint="2"/>
<apex:inputField value="{!A.Macro_Industry__c}" required="true" taborderhint="8"/>
<apex:inputfield value="{!A.billingstreet}" taborderhint="3"/>
<apex:inputfield value="{!A.Micro_Industry__c}" required="true" taborderhint="9"/>
<apex:inputfield value="{!A.billingcity}" taborderhint="4"/>
<apex:inputfield value="{!A.Custom_Account_Source__c}" required="true" taborderhint="10"/>
<apex:inputfield value="{!A.billingstate}" taborderhint="5"/>
<apex:inputfield value="{!A.website}" taborderhint="11"/>
<apex:inputfield value="{!A.Preferred_Mode_s__c}" required="true" taborderhint="6"/>
<apex:inputfield value="{!A.Customer_Type__c}" taborderhint="12"/>
<apex:inputfield value="{!A.description}" taborderhint="13"/>
</apex:pageblocksection>


<apex:pageblocksection title="Activity Information">
<apex:inputfield value="{!T.subject}" required="true" taborderhint="14"/>
<apex:inputfield value="{!T.Activitydate}" required="true" taborderhint="16"/>
<apex:inputfield value="{!T.status}" taborderhint="15"/>
<apex:inputfield value="{!T.description}" taborderhint="17"/>
</apex:pageblocksection>


<apex:pageblocksection title="Opportunity Information">
<apex:inputfield value="{!O.name}" taborderhint="18"/>
<apex:inputfield value="{!O.stagename}" taborderhint="20"/>
<apex:inputfield value="{!O.closedate}" taborderhint="19"/>
<apex:inputfield value="{!O.amount}" taborderhint="21"/>
</apex:pageblocksection>
<div align="center">
<apex:commandbutton value="Save Record" action="{!save}" rerender="TheForm"/>
</div>
</apex:pageblock>
</apex:form>


</apex:page>

 

***********************************************************************************CONTROLLER*******************************************************************************************

public class multirecords {

public Account A {get; set;}
Public Task T {get; set;}
Public Opportunity O {get; set;}

public multirecords()
{
A = new account();
T = new task();
O = new opportunity();
}

public PageReference save()
{

insert A;       //Line 17 -- The aforementioned error occurs here.

insert T;
T.whatid = A.id;

insert O;
O.Accountid = A.id;

return new PageReference('/'+A.id);
}

public PageReference Cancel()
{
return new PageReference('/001/o');
}

}

 

 

*****************************************************************************************TEST CLASS*******************************************************************************

@istest

public class TestMultiRecords{
public static testMethod void TestMultiRecords(){

PageReference pageRef = Page.multirecords;
Test.setCurrentPage(pageRef);

multirecords Controller = new multirecords();


Account a = new account(Name='Testmultirecord',phone='4109011267',macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
insert a;

task t = new task(subject='testmultirecord');
insert t;

controller.cancel();
controller.save();

}
}

 

 

 

souvik9086souvik9086

Assign all the reqired fields before inserting records. Then the error will be gone.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

SFDC-NOOBSFDC-NOOB

Thank you for your reply.  The "Name" field is included in the test class I posted.  I am not sure why it's throwing that error.

souvik9086souvik9086

@istest

public class TestMultiRecords{
public static testMethod void TestMultiRecords(){

PageReference pageRef = Page.multirecords;
Test.setCurrentPage(pageRef);

multirecords Controller = new multirecords();


Account a = new account(Name='Testmultirecord',phone='4109011267',macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
insert a;

task t = new task(subject='testmultirecord',whatid=a.id,priority='normal',Status='Not started');
insert t;

controller.cancel();
controller.save();

}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

SFDC-NOOBSFDC-NOOB

Thank you for your reply.  I made the corrections to include the additional fields when creating a new task; however, I still receive the same error:

 

Error Message::: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name]: [Name]

Stack Trace::: Class.multirecords.save: line 17, column 1 Class.TestMultiRecords.TestMultiRecords: line 19, column 1

souvik9086souvik9086

Is it telling that account name is missing? Strange.

 

Thanks

asish1989asish1989
@istest
public class TestMultiRecords{
	public static testMethod void TestMultiRecords(){
		Account a = new account(Name='Testmultirecord',phone='4109011267',macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
		insert a;

		task t = new task(subject='testmultirecord',whatid=a.id,priority='normal',Status='Not started');
		insert t;
		
		PageReference pageRef = Page.multirecords;
		Test.setCurrentPage(pageRef);
		multirecords Controller = new multirecords();
		controller.cancel();
		controller.save();

	}
}

 

SFDC-NOOBSFDC-NOOB

Yes, the error persists and states that "Name" is missing.  I'm assuming it's referring to account name.

 

Asish, thank you for your reply.  The test code you provided kept me at 68% covered.  The code that is not covered is as follows::

 

public class multirecords {

public Account A {get; set;}

Public Task T {get; set;}

Public Opportunity O {get; set;}

    public multirecords()    

{        

A = new account();        

T = new task();        

O = new opportunity();    

}               

 public PageReference save()    

{

insert A;       

 

insert T;        

T.whatid = A.id;                

 

insert O;        

O.Accountid = A.id;                

 

return new PageReference('/'+A.id);    

}

 public PageReference Cancel()
    {
        return new PageReference('/001/o');
    }
   
}

SFDC-NOOBSFDC-NOOB

Just a quick update. . .

 

When I remove the call to the save function, the error is gone; however, my code coverage goes from 68% to 56%.

 

@istest
public class TestMultiRecords{
   
    public static testMethod void TestMultiRecords(){
    date mydate = date.today();
       
        PageReference pageRef = Page.multirecords;
        Test.setCurrentPage(pageRef);
        multirecords Controller = new multirecords();
       
        account a = new account(Name='TestMultiRec',phone='4109011267',macro_industry__c='Construction',custom_account_source__c='Referral',preferred_mode_s__c='Van');
        insert a;
       
        task t = new task(subject='testmultirecord',whatid=a.id,priority='Normal',Status='Not started');
        insert t;
       
        opportunity o = new opportunity(accountid=a.id,name='testOpp',stagename='Discovered',closedate=mydate);
        insert o;
       
       
        controller.cancel();
        controller.save();   /////Removing this line gets rid of the this error::::::  System.DmlException: Insert failed. First exception on row 0; first error:

                                                                                                                       //REQUIRED_FIELD_MISSING,   Required fields are missing: [Name]: [Name]
        
    }
}