You need to sign in to do that
Don't have an account?
lakshmiNarasimha Reddy
how to insert multiple records using dynamic apex in salesforce
how to insert multiple records using dynamic apex in salesforce
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
What do you mean by dynamic apex ?
Use Database.Insert(accList, false);
You can simply collect all the records in list and then you can insert all of them at a time..
Example:
List<Contact> lstContact = new list<Contact>();
for(Account objAcc: lstAccounts)
{
Contact objContact = new Contact(LastName = 'test', AccountId = objAcc.Id);
lstContact.add(objContact );
}
if(!lstContact.isEmpty())
{
insert lstContact ;
}
In above example I have one Account list which will copntain 10 account records now I need to create 10 contact for these accounts and associate them with accopunt...
SO I am iterating the accounts and createing contact for each and storing all contacts in a list and then I am inserting all of them..
Please check and let me know if this is what you wanted ti know, how to use collections and do DML..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
you can add multiple records lets say 100 in one statement by following sample code
List<Apex_Customer__c> custList = new List<Apex_Customer__c>();
for(integer i=1; i<100; i++)
{
Apex_Customer__c objCust = new Apex_Customer__c();
objCust.name='Muhammad Saad Javed'+i;
objCust.Apex_Active__c = true ;
objCust.Apex_Customers_Description__c = 'This Customer is Added by Statement';
objCust.Apex_Customer_Status__c = 'Inactive';
objCust.Email__c ='Cust'+i+'@email.com';
custList.add(objCust);
}
insert custList;
you have to coppy this code in execute anonymouse window and change object names as per your Developer edition and execute, 100 records will be added :)
I have to create multiple records and if I do it with for loop I can Apex CPU time error. if the amount of records is small, the code works fine, but if it is high -> CPU time error.
I wonder if there is some contractor to create a list like
List<Custom__c> myobj = new List<Name="asdas", Property__c="asdas">[1000] and it would create 1000 records with different ids.
Any ideas?
Try this code to create multiple records in apex:
List<Contact> ContactList = new list<Contact>();
for(Integer i=0;i<200;i++)
{
Contact con1 = new Contact()
con1.LastName = 'test';
contactList.add(con1);
}
if(!contactList.isEmpty())
{
insert contactList ;
}
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi