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
Zuri LinnZuri Linn 

Help with test class error

I have written the following test class and receive error "FATAL_ERROR System.DmlException: Insert failed. First exception on row 0 with id 0015B000015c9L1QAI; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]" when I run it, can anyone tell me what I am doing wrong?

@isTest
private class test_SaveAccountDetails {
    
    static testmethod void SaveAccountDetailTest() {
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account acctForm = new Account();
        acctForm.RecordTypeId=RecordTypeIdPropAccount;
        acctForm.Name = 'Test Account';
        acctForm.PropertyNumber__c = '123456';
        acctForm.PropertyType__c = 'Retail';
        acctForm.Phone = '8885551212';
        acctForm.PropertyStatus__c = 'Potential Acquisition';
        acctForm.Website = 'www.testsite.com';
        acctForm.Description = 'test account description';
        acctForm.Region__c = 'test region';
        acctForm.PurchaseDate__c = date.parse('2/2/2000');
        acctForm.PurchasePrice__c = 350000;    
        acctForm.DateSold__c = date.parse('2/2/2019');
        acctForm.SalePrice__c = 500000;
        acctForm.BillingCountry = 'United States';   
        acctForm.ShippingCountry = 'United States';
        acctForm.BillingStreet = '1313 Mockingbird Lane';
        acctForm.ShippingStreet = '100 Art Rooney Ave';
        acctForm.BillingCity = 'Springfield';
        acctForm.BillingState = 'Missouri';    
        acctForm.ShippingCity = 'Pittsburgh';
        acctForm.ShippingState = 'Pennsylvania';
        acctForm.BillingPostalCode = '12345';   
        acctForm.ShippingPostalCode = '15212';
        
        insert acctForm;
        
        Test.startTest();        
        SaveAccountDetails.SaveAccountDetail(acctForm);
        Test.stopTest();
            
        
        
    }
}

Here is the class I am trying to provide code coverage for:

public class SaveAccountDetails {
    
    @AuraEnabled
    public static id SaveAccountDetail(Account acctForm){
        //DML operation to save AcctForm Details
        system.debug(acctForm);
        Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        acctForm.RecordTypeId=recTypeId;
        insert acctForm;
        return acctForm.id;
    }
}

I would really appreciate any help provided!
Best Answer chosen by Zuri Linn
Maharajan CMaharajan C
Hi Zuri,

Don't insert the Account in Test Class. Account insertion will be handled by Apex Class. So ,Just remove the  insert acctForm; line from Test Class and pass the Account Record Sample then you will get  the coverage.


@isTest
private class test_SaveAccountDetails {
    
    static testmethod void SaveAccountDetailTest() {
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account acctForm = new Account();
        acctForm.RecordTypeId=RecordTypeIdPropAccount;
        acctForm.Name = 'Test Account';
        acctForm.PropertyNumber__c = '123456';
        acctForm.PropertyType__c = 'Retail';
        acctForm.Phone = '8885551212';
        acctForm.PropertyStatus__c = 'Potential Acquisition';
        acctForm.Website = 'www.testsite.com';
        acctForm.Description = 'test account description';
        acctForm.Region__c = 'test region';
        acctForm.PurchaseDate__c = date.parse('2/2/2000');
        acctForm.PurchasePrice__c = 350000;    
        acctForm.DateSold__c = date.parse('2/2/2019');
        acctForm.SalePrice__c = 500000;
        acctForm.BillingCountry = 'United States';   
        acctForm.ShippingCountry = 'United States';
        acctForm.BillingStreet = '1313 Mockingbird Lane';
        acctForm.ShippingStreet = '100 Art Rooney Ave';
        acctForm.BillingCity = 'Springfield';
        acctForm.BillingState = 'Missouri';    
        acctForm.ShippingCity = 'Pittsburgh';
        acctForm.ShippingState = 'Pennsylvania';
        acctForm.BillingPostalCode = '12345';   
        acctForm.ShippingPostalCode = '15212';
                
        Test.startTest();        
        SaveAccountDetails.SaveAccountDetail(acctForm);
        Test.stopTest(); 
    }
}


Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Zuri,

Don't insert the Account in Test Class. Account insertion will be handled by Apex Class. So ,Just remove the  insert acctForm; line from Test Class and pass the Account Record Sample then you will get  the coverage.


@isTest
private class test_SaveAccountDetails {
    
    static testmethod void SaveAccountDetailTest() {
        Id RecordTypeIdPropAccount = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Property').getRecordTypeId();
        
        Account acctForm = new Account();
        acctForm.RecordTypeId=RecordTypeIdPropAccount;
        acctForm.Name = 'Test Account';
        acctForm.PropertyNumber__c = '123456';
        acctForm.PropertyType__c = 'Retail';
        acctForm.Phone = '8885551212';
        acctForm.PropertyStatus__c = 'Potential Acquisition';
        acctForm.Website = 'www.testsite.com';
        acctForm.Description = 'test account description';
        acctForm.Region__c = 'test region';
        acctForm.PurchaseDate__c = date.parse('2/2/2000');
        acctForm.PurchasePrice__c = 350000;    
        acctForm.DateSold__c = date.parse('2/2/2019');
        acctForm.SalePrice__c = 500000;
        acctForm.BillingCountry = 'United States';   
        acctForm.ShippingCountry = 'United States';
        acctForm.BillingStreet = '1313 Mockingbird Lane';
        acctForm.ShippingStreet = '100 Art Rooney Ave';
        acctForm.BillingCity = 'Springfield';
        acctForm.BillingState = 'Missouri';    
        acctForm.ShippingCity = 'Pittsburgh';
        acctForm.ShippingState = 'Pennsylvania';
        acctForm.BillingPostalCode = '12345';   
        acctForm.ShippingPostalCode = '15212';
                
        Test.startTest();        
        SaveAccountDetails.SaveAccountDetail(acctForm);
        Test.stopTest(); 
    }
}


Thanks,
Maharajan.C
This was selected as the best answer
Zuri LinnZuri Linn
Duh!! Thank you so much Maharajan!