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
Tony MontanaTony Montana 

i am trying to add multiple contacts in one single dml using lists into the contact object. this is what i have so far

Account a = new Account(name = 'Microtek Inc');
insert a;
List<Contact> ContactList = new List<Contact>();
for (Integer i = 0; i < 5; i++) {
       contactList.add(new Contact
            (
            accountID = a.id;
            FirstName = 'Charles ' + i;
            LastName = 'Test Contact' + i;
            NT_Login_Name__c = 'testuser' + i;
            Management_Level_Type__c = 'Director';
            Former_Employee__c = false;
    ));

List<sObject> objects = new List<sObject>();
objects.addAll((List<sObject>)(contactList));
insert objects;

will above work ?
Best Answer chosen by Tony Montana
Andrew GAndrew G
Nearly
change the semi colons;  to commas , in the new Contact element
and close the ellipse {}
and i would remove the attempt to cast from contact to sObject.  Not really needed.  But if you were going the other way, then yes. (e.g sObject > Contact)
 
Account a = new Account(name = 'Microtek Inc');
insert a;
List<Contact> ContactList = new List<Contact>();
for (Integer i = 0; i < 5; i++) {
       contactList.add(new Contact
            (
            accountID = a.id,
            FirstName = 'Charles ' + i,
            LastName = 'Test Contact' + i,
            NT_Login_Name__c = 'testuser' + i,
            Management_Level_Type__c = 'Director',
            Former_Employee__c = false
    ));
}

List<SObject> objects = new List<SObject>();
objects.addAll(contactList);
insert objects;

regards
Andrew

All Answers

Andrew GAndrew G
Nearly
change the semi colons;  to commas , in the new Contact element
and close the ellipse {}
and i would remove the attempt to cast from contact to sObject.  Not really needed.  But if you were going the other way, then yes. (e.g sObject > Contact)
 
Account a = new Account(name = 'Microtek Inc');
insert a;
List<Contact> ContactList = new List<Contact>();
for (Integer i = 0; i < 5; i++) {
       contactList.add(new Contact
            (
            accountID = a.id,
            FirstName = 'Charles ' + i,
            LastName = 'Test Contact' + i,
            NT_Login_Name__c = 'testuser' + i,
            Management_Level_Type__c = 'Director',
            Former_Employee__c = false
    ));
}

List<SObject> objects = new List<SObject>();
objects.addAll(contactList);
insert objects;

regards
Andrew
This was selected as the best answer
Tony MontanaTony Montana
Thanks Andrew G