Good day everyone, I have created an apex class which will save data from csv to account and contact object in salesforce. The concept of this class is that it will read the column in my csv and will try to process the data.
If a particular data already exist in salesforce, it will ignore the data but if it doesn't exist it will create a new account or contact. Let's just say that I use the Account Name column to check if an account exist and Contact Email to check if contact exist(Identifier for Contact and Account can be one or more fields). To further explain below is some part of my code.
public with sharing class DI_batchProcessor implements Database.batchable<String>, Database.Stateful {
public DI_batchProcessor(){
// some data initialization here
}
public Iterable<String> start(Database.batchableContext batchableContext)
{
// here I return my csv data to be process.
return new DI_Utility_RowIterator(MY_CSV_FILE);
}
public void execute(Database.BatchableContext batchableContext, List<String> scope) {
List<Account> tempAccount = [Select Id,AccountName from Account];
List<Contact> tempContact = [Select Id,Email from Contact];
Map<String,Id> AllAccount = new Map<String,Id>();
Map<String,Id> AllContact = new Map<String,Id>();
for(Account acc : tempAccount) {
AllAccount.put(acc.AccountName,acc.Id);
}
for(Contact con : tempContact) {
AllContact.put(con.Email,con.Id);
}
// here I loop the data return from the CSV and check
// if the data exist using the map created above.
// If exist ignore, else process for insertion.
}
public void finish(Database.BatchableContext bc) {
}
}
Everything is working fine at first. But when I try to run this in an instance where account and contact has so many record, let say for example more 50000. I will encounter the problem "First error: Too many query rows: 50001
". I just want to ask what are the possible remedy for this problem. What are the parts of my code that needs modification ? Is there any way to query all those rows in contact and account without hitting those limits ? Am I doing this wrong ? I already did some research in this, but still I didn't find a solution. Please help.