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
vickySFDCvickySFDC 

Using Batch apex I want to insert 1 million contact records for an Specific Account?Give Examples

Hi All,

 

In my requirement Using Batch apex I want to insert  1 million contact records for an Specific Account?how to do this.pls give some examples.Urgent help neede.

 

 

Thanks,

 

 

Vicky

 

souvik9086souvik9086

Try this

 

global with sharing class BatchApex implements Database.Batchable<SObject>{
global Iterable start(Database.BatchableContext info){
String query = 'Select Id, name, Accountid from Contact where AccountId = ://Your specific acc Id];
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<SObject> sObjectRecords){

List<Contact> consToUpdate = new List<Contact>();
for(SObject sObj : sObjectRecords){

Contact c = (Contact)sObj;
c.Name = 'true';
c.NumberOfEmployees = 70;
accsToUpdate.add(c);
}
if(consToUpdate.size()>0{
update consToUpdate;
}
}
global void finish(Database.BatchableContext info){
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

vickySFDCvickySFDC

Hi,

 

 

Its not working. while saving class it showing error

:Error: Compile Error: line breaks not allowed in string literals at line 3 column -1

 This code:

global with sharing class BatchApex implements Database.Batchable<SObject>{
global Iterable start(Database.BatchableContext BC){
String query = 'Select Id, name, Accountid from Contact where AccountId = :'0019000000Pfu6L';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<SObject> sObjectRecords){

List<Contact> consToUpdate = new List<Contact>();
for(SObject sObj : sObjectRecords){

Contact c = (Contact)sObj;
c.Name = 'true';
c.NumberOfEmployees = 70;
consToUpdate.add(c);
}
if(consToUpdate.size()>0{
update consToUpdate;
}
}
global void finish(Database.BatchableContext BC){
}
}

 

 

pls help me ....

 

 

 

Thanks,

 

 

Vicky

souvik9086souvik9086

Change like this

 

global with sharing class BatchApex implements Database.Batchable<SObject>{
global Iterable start(Database.BatchableContext BC){
String query = 'Select Id, name, Accountid from Contact where AccountId = \'0019000000Pfu6L\'';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<SObject> sObjectRecords){

List<Contact> consToUpdate = new List<Contact>();
for(SObject sObj : sObjectRecords){

Contact c = (Contact)sObj;
c.Name = 'true';
c.NumberOfEmployees = 70;
consToUpdate.add(c);
}
if(consToUpdate.size()>0{
update consToUpdate;
}
}
global void finish(Database.BatchableContext BC){
}
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Avidev9Avidev9
THis code wont wok for 1 million records!, You need a custom iterator for this. Have a look here http://boards.developerforce.com/t5/Apex-Code-Development/Using-Batch-Apex-to-Insert-a-large-number-of-records-instead-of/td-p/599549
vickySFDCvickySFDC
Hi Souvik,

Same error thowing While saving Class........
Error: Compile Error: line breaks not allowed in string literals at line 3 column -1


vickySFDCvickySFDC
Hi Souvik,

Now class error is not throwing.but it showing ...


Compile Error: Field is not writeable: Contact.Name at line 10 column 1

global with sharing class BatchApex implements Database.Batchable<SObject>{
global Iterable<Sobject> start(Database.BatchableContext BC){
String query = 'Select Id, name, Accountid from Contact where AccountId = \'0019000000Pfu6L\'';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<SObject> sObjectRecords){
List<Contact> consToUpdate = new List<Contact>();
for(SObject sObj : sObjectRecords){
Contact c = (Contact)sObj;
c.Name = 'true';
c.NumberOfEmployees = 70;
consToUpdate.add(c);
}
if(consToUpdate.size()>0){
update consToUpdate;
}
}
global void finish(Database.BatchableContext BC){
}
}


Thanks,


Vicky
souvik9086souvik9086

 Field is not writeable means: You cannot write on this field. If the field is auto-number field then this type of error will come. But contact name is not a auto number field. Please in your organisation is there any filters associalted with the name field of contact.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

vickySFDCvickySFDC

Hi Souvik,

 

 

No error throwing..But I run bach class for Particular Account no contact is inserted.

 

global with sharing class BatchApex implements Database.Batchable<SObject>{
global Iterable<Sobject> start(Database.BatchableContext BC){
String query = 'Select Id, name, Accountid from Contact where AccountId = \'0019000000Pfu6L\'';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<SObject> sObjectRecords){
List<Contact> consToUpdate = new List<Contact>();
for(SObject sObj : sObjectRecords){
Contact c = (Contact)sObj;
c.lastname = 'true';
//c.NumberOfEmployees = 70;
consToUpdate.add(c);
}
if(consToUpdate.size()>0){
update consToUpdate;
}
}
global void finish(Database.BatchableContext BC){
}
}

 

 

Thanks,

 

Vicky

souvik9086souvik9086

We have to change code here. this is for update. For insert we have to think other way.

You can check the link mentioned by Avidev.

 

Thanks