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
Ronaldo Costa 8Ronaldo Costa 8 

Aura Test Class - Simple DML help

Hello, I have the following simple code:
 
public class SaveAndNewC {
    
    @AuraEnabled
    public static Id saveDetails(Lead regForm1){
        // DML operation to save Lead Details   
        INSERT regForm1;
        return regForm1.Id; 
    }
}

But my current class is only covering 66%:
 
@isTest
private class SaveAndNewC_Test{
  @testSetup
  static void setupTestData(){
    test.startTest();
    Lead lead_Obj = new Lead(LastName = 'LastName836', FirstName = 'First211', Company = 'Company565', Phone = '54343-13889', Status = 'Unqualified', IsConverted = false, IsUnreadByOwner = false);
    Insert lead_Obj;
    test.stopTest();
  }
  static testMethod void test_saveDetails_UseCase1(){
    List<Lead> lead_Obj  =  [SELECT LastName,FirstName,Company,Phone,Status,IsConverted,IsUnreadByOwner from Lead];
    System.assertEquals(true,lead_Obj.size()>0);
    SaveAndNewC obj01 = new SaveAndNewC();
    SaveAndNewC.saveDetails(lead_Obj[0]); 
      //lead_Obj[0]
  }
}

Can you please help? Upon deployment is says as follows:

SaveAndNew

C_Testtest_saveDetails_UseCase1System.DmlException: Insert failed. First exception on row 0 with id 00Qf400000IsyiqEAB; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] 

Stack Trace: Class.SaveAndNewC.saveDetails: line 6, column 1 Class.SaveAndNewC_Test.test_saveDetails_UseCase1: line 14, column 1
Ankit RathorAnkit Rathor
Hi Ronaldo,

I did walkthrough your code and found some minor issues.
Please try this code:

================================================================
Class:

public class SaveAndNewC {
    
    @AuraEnabled
    public static Id saveDetails(Lead regForm1){
        // DML operation to save Lead Details   
        INSERT regForm1;
        return regForm1.Id; 
    }
}

================================================================
Test Class:

@isTest
public class SaveAndNewC_Test{
  static testMethod void setupTestData(){
    test.startTest();
    Lead lead_Obj = new Lead(LastName = 'LastName836', FirstName = 'First211', Company = 'Company565', Phone = '54343-13889', Status = 'Unqualified', IsConverted = false, IsUnreadByOwner = false);
    Id returnId = SaveAndNewC.saveDetails(lead_Obj); 
    System.assertEquals(true, returnId != null);
    test.stopTest();
  }
}

Please let me know if you have any other query. If this solution is helpful then please mark it as Best Answer.

Thanks,
Ankit Rathor 
Ajay K DubediAjay K Dubedi
Hi Ronaldo,

Aura Test Class - Simple DML help. This test class is 100% code coverage.
 
@isTest
private class SaveAndNewCTest {
    @isTest static void SaveAndNewCMethod()
    {
        Lead leadObj = new Lead();
        leadObj.Company ='hci';
        leadObj.LastName ='xyz';
        System.debug('Id::'+leadObj.Id);
        Test.startTest();
        SaveAndNewC.saveDetails(leadObj);
        Test.stopTest();
        
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com