Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
Try the following apex class:
integer i = 0;
List<Contact> conlist = new List<Contact>();
List<Account> acclist = [SELECT Id FROM Account LIMIT 100];
for(Account a : acclist){
Contact c = new Contact();
c.LastName = 'ContactName'+i;
c.AccountId = a.Id;
conlist.add(c);
i++;
}
insert conlist;
Here I'm assuming you have to insert one Contact per Account if you want more you have to use one more for loop inside the current loop.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Try the following apex class:
integer i = 0;
List<Contact> conlist = new List<Contact>();
List<Account> acclist = [SELECT Id FROM Account LIMIT 100];
for(Account a : acclist){
Contact c = new Contact();
c.LastName = 'ContactName'+i;
c.AccountId = a.Id;
conlist.add(c);
i++;
}
insert conlist;
Here I'm assuming you have to insert one Contact per Account if you want more you have to use one more for loop inside the current loop.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi