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
Sachin Sharma 4089Sachin Sharma 4089 

how to insert 300 contacts for each account if i have 100 accounts

Hi, i have just started salesforce and i have a situation in which i want to insert 300 contacts for each account and i have 100 accounts in my salesforce org. i have already read some articles on batch apex but i'm unable to understand how to do it. Thanks in advance if anyone solve this for me.
Best Answer chosen by Sachin Sharma 4089
Malika Pathak 9Malika Pathak 9
Hi Sachin,

This code is working fine for me.

Please wait for atleast 30-40 mintues after excuting the batch.

Note- There are some storage limitations in Dev Org so you must check your org storage limits under Setup -> Data -> Storage Usage.
Due to these limitations the insertion of contact might fail.


Please mark it as the best answer if it helps you to fix the issue.

Thank you!

Regards,
Malika Pathak

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Sachin,

Greetings!

You need to insert all the contacts which you need to insert in one CSV file and use bulk API to insert either using Data Loader or Workbench without using any code.

Then,the contacts will be assigned to the Account based on AccountId field on the Contact records.

Reference:https://developer.salesforce.com/docs/atlas.en-us.dataLoader.meta/dataLoader/inserting_updating_or_deleting_data.htm

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
Malika Pathak 9Malika Pathak 9
Hi Sachin,

Greetings!

Create a batch class-
 
public class InsertContactToAccount implements Database.Batchable<sObject>, Database.Stateful {
    public static Integer success = 0;
    public static Integer failed = 0;
    public Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator([SELECT Id, Name FROM Account]);
    }
    public void execute(Database.BatchableContext BC, List<Account> accList) {
        List<Contact> conList = new List<Contact>();
        for(Account acc : accList) {
            for(Integer i = 1; i <= 300; i++) {
                Contact con = new Contact();
                con.LastName = acc.Name + i + ' Contact';
                con.AccountId = acc.Id;
                conList.add(con);
            } 
        }
        List<Database.SaveResult> result = Database.insert(conList, false);
        for(Database.SaveResult s : result) {
            if(s.isSuccess()) {
                success = success + 1;
            }
            else {
                failed = failed + 1;
            }
        }
    }
    public void finish(Database.BatchableContext BC){
        if(success != 0) {
            System.debug('Success : '+success+' Records');
        }
        if(failed != 0) {
            System.debug('Failed : '+failed+' Records');
        }
    }
}


Execute the above batch class using this code-

InsertContactToAccount bc = new InsertContactToAccount();
Database.executeBatch(bc, 10);

Please mark it as the best answer if it helps you to fix the issue.

Thank you!

Regards,
Malika Pathak
Sachin Sharma 4089Sachin Sharma 4089
Hi Malika Pathak 9
Thanks for your reply. But this code is not generating 30000 contacts. Please reply if you have any other solution.
Thanks and regards
Sachin Sharma
 
Malika Pathak 9Malika Pathak 9
Hi Sachin,

This code is working fine for me.

Please wait for atleast 30-40 mintues after excuting the batch.

Note- There are some storage limitations in Dev Org so you must check your org storage limits under Setup -> Data -> Storage Usage.
Due to these limitations the insertion of contact might fail.


Please mark it as the best answer if it helps you to fix the issue.

Thank you!

Regards,
Malika Pathak
This was selected as the best answer
Sachin Sharma 4089Sachin Sharma 4089
Hi Mallka Pathak
After execute the code again i have checked the storage capacity of my org. Below is the storage capacity of my org. Its limit is 5.0 MB and used space is 42 MB after execute ths code. So i think due to storage capacity 30k contacts are not generating. BTW thank u for your replies.
Storage capacity of my org
Regards
Sachin Sharma