You need to sign in to do that
Don't have an account?
vickySFDC
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
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
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
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
Same error thowing While saving Class........
Error: Compile Error: line breaks not allowed in string literals at line 3 column -1
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
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
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
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