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
Alex SidlinskiyAlex Sidlinskiy 

Invalid type: Database.InsertResults

I'm writting a unit test on apex snippet that restricts contact by name or last name to be specific. I'm getting error "Invalid type: database.InsertResults" on line 18. Thanks in advance.
 
@isTest
public class TestRestrictContactByName {
 	
    @isTest static void TestInvalidName() {
        // Test data setup
        // Create empty list for contacts
        List<Contact> contacts = new List<Contact>{};
        
        //Iterate over each contact
        for(Integer i=0; i<200; i++){
            Contact ct = new Contact(LastName='INVALIDNAME' + i);
            contacts.add(ct);
        }
        
        // Perform test
        Test.startTest();
        insert contacts;
       	Database.InsertResults result = Database.insert(contacts, false);
        Test.stopTest();

        System.assert(!result.isSuccess());
        System.assert(result.getErrors().size() == 200);
        System.assertEquals('The Last Name "'+contacts.LastName+'" is not allowed for DML',
                             result.getErrors()[0].getMessage());
    }	
}

 
Best Answer chosen by Alex Sidlinskiy
3 Creeks3 Creeks
Try:
 
Database.SaveResult[] result = Database.insert(contacts, false);

 

All Answers

3 Creeks3 Creeks
Try:
 
Database.SaveResult[] result = Database.insert(contacts, false);

 
This was selected as the best answer
Alex SidlinskiyAlex Sidlinskiy
That resolved the error. My changed code looks like this now.
@isTest
public class TestRestrictContactByName {
 	
    @isTest static void TestInvalidName() {
        // Test data setup
        // Create empty list for contacts
        List<Contact> contacts = new List<Contact>{};
        
        //Iterate over each contact
        for(Integer i=0; i<200; i++){
            Contact ct = new Contact(LastName='INVALIDNAME' + i);
            contacts.add(ct);
        }
        
        // Perform test
        Test.startTest();
        insert contacts;
       	Database.SaveResult[] result = Database.insert(contacts, false);
        Test.stopTest();

        System.assert(!result[0].isSuccess());
        System.assert(result[0].getErrors().size() > 0);
        System.assertEquals('The Last Name "'+contacts.LastName+'" is not allowed for DML',
                             result.getErrors()[0].getMessage());
    }	
}

And I'm getting error on line 23 "Initial term of field expression must be a concrete SObject: List".
 
3 Creeks3 Creeks
contacts is a List so you would have reference it something like contacts[0].LastName.
Alex SidlinskiyAlex Sidlinskiy
Cool that solved it.