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
anuj huriaanuj huria 

System.DmlException: Insert failed. First exception in test class

controller

public class AddmultipleAccountsController {
Account account = new Account();
public list<Account> listAccount{ get; set; }

public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}

Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
for(Integer i=0; i<listAccount.size(); i++)
{
insert listAccount;
}
return Page.addmore;
}
}

test class

@isTest
public class addmul_test 
{
    static testmethod void addtest()
    {
        AddmultipleAccountsController ar=new AddmultipleAccountsController();
        pagereference pf=Page.addmore;
        ar.addAccount();
       
       
        ar.saveAccount();       
        
        
       
        
        
    }
}
Best Answer chosen by anuj huria
Srinivas SSrinivas S
Please keep a if condition in your main class for the DML stmt-
if(!Test.isRunningTest()) {
    insert listAccount;
}

Your test class won't be failed and you will get most of the code coverage. In case of failurese we cannot deploy.

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.

All Answers

Srinivas SSrinivas S
public class AddmultipleAccountsController {
    Account account = new Account(Name = 'test');
    public list<Account> listAccount {get;set;}

    public AddmultipleAccountsController() {
        listAccount=new list<Account>();
        listAccount.add(account);
    }   
    Public void addAccount() {
        Account acc = new Account(Name = 'test');
        listAccount.add(acc);
    }
    public PageReference saveAccount() {
        insert listAccount;
        return Page.addmore;
    }
}

Test Class -
@isTest
private class TestAddmultipleAccountsController {
    static testMethod void testAccContrl() {
        AddmultipleAccountsController accContrl = new AddmultipleAccountsController();
        accContrl.addAccount();
        accContrl.saveAccount();
    }
}

Please as mentioned above.

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
anuj huriaanuj huria
hi Srinivasa,

i tried this 



public class AddmultipleAccountsController {
    
Account account = new Account();
public list<Account> listAccount{ get; set; }

public AddmultipleAccountsController()
{
listAccount=new list<Account>();
listAccount.add(account);
}

Public void addAccount()
{
Account acc = new Account();
listAccount.add(acc);
}
public PageReference saveAccount() {
    
insert listAccount;
    //return null;
return Page.addmore;
}
}

****************************************

@isTest
public class addmul_test 
{
    public static testmethod void addtest()
    {
        AddmultipleAccountsController ar=new AddmultipleAccountsController();
        pagereference pf=Page.addmore;
        
        
        Account a=new Account();
        a.Name='dev';        
       ar.listAccount.add(a);
        ar.addAccount();
        ar.saveAccount();       
        
    }
}



why this is not working..
giving a faliure of required field missing 

thnks
Srinivas SSrinivas S
Account are automatically creating in your main class, not in the test class. This is the reason why your code is failing.
------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
 
anuj huriaanuj huria
hi srinivasa but my page is working in perfect manner... the problem is in test class only..
and i have a query that..if the code coverage is more than 75% and there is failure occur after clicking run test..then can we deploy the class??
Srinivas SSrinivas S
Please keep a if condition in your main class for the DML stmt-
if(!Test.isRunningTest()) {
    insert listAccount;
}

Your test class won't be failed and you will get most of the code coverage. In case of failurese we cannot deploy.

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
This was selected as the best answer
anuj huriaanuj huria
thanks
its working