You need to sign in to do that
Don't have an account?

ERROR: CreateAccountForContact: System.LimitException: Too many query rows: 50001
Hi,
We recently made the change to work from contacts instead of leads in Salesforce. We get all our contacts from our marketing generation tool without an Account. The company name of these contacts is stored in a field called "Company__c". I wrote a code that looks if the contact is from our existing customer (Zlien_User_ID__c) and associates with the same Salesforce Account(User_ID__c), if not it will look for the matching Account name and associates with it, and if not finally it will create an account and associate with it. The following code works fine when one contact is created by the lead gen tool at a time. Sometimes, we may have to bulk upload contacts using dataloader which is when I encounter the error message: System.LimitException: Too many query rows: 50001.
Please find the Code below:
public class assignAccount
{
public static void assignAccounts(Set<Id> contactIds)
{
Map<String,Account> matchUserIdMap = new Map<String,Account>();
Map<String,Account> matchCompanyMap = new Map<String,Account>();
for(Account acc : [Select User_ID__c, name, Id from Account])
{
if(acc.User_ID__c != null)
{
// Contact map when Zlien user id is not null.
matchUserIdMap.put(acc.User_ID__c, acc);
}
else
{
// Contact map when Zlien user id is null.
matchCompanyMap.put(acc.name, acc);
}
}
for(Contact con : [Select Zlien_User_ID__c, Company__c, accountId, ownerId from Contact where Id IN :contactIds])
{
if(con.Zlien_User_ID__c != null)
{
if(matchUserIdMap.containsKey(con.Zlien_User_ID__c))
{
con.accountId = matchUserIdMap.get(con.Zlien_User_ID__c).Id;
}
else if(matchCompanyMap.containsKey(con.Company__c))
{
con.accountId = matchCompanyMap.get(con.Company__c).Id;
}
else
{
Account A = new Account(name = con.Company__c, ownerId = con.ownerId);
insert A;
con.accountId = A.Id;
}
}
else if(con.Company__c != null)
{
if(matchCompanyMap.containsKey(con.Company__c))
{
con.accountId = matchCompanyMap.get(con.Company__c).Id;
}
else
{
Account A = new Account(name = con.Company__c, ownerId = con.ownerId);
insert A;
con.accountId = A.Id;
}
}
update con;
}
}
}
Thanks in Advance,
Teja
We recently made the change to work from contacts instead of leads in Salesforce. We get all our contacts from our marketing generation tool without an Account. The company name of these contacts is stored in a field called "Company__c". I wrote a code that looks if the contact is from our existing customer (Zlien_User_ID__c) and associates with the same Salesforce Account(User_ID__c), if not it will look for the matching Account name and associates with it, and if not finally it will create an account and associate with it. The following code works fine when one contact is created by the lead gen tool at a time. Sometimes, we may have to bulk upload contacts using dataloader which is when I encounter the error message: System.LimitException: Too many query rows: 50001.
Please find the Code below:
public class assignAccount
{
public static void assignAccounts(Set<Id> contactIds)
{
Map<String,Account> matchUserIdMap = new Map<String,Account>();
Map<String,Account> matchCompanyMap = new Map<String,Account>();
for(Account acc : [Select User_ID__c, name, Id from Account])
{
if(acc.User_ID__c != null)
{
// Contact map when Zlien user id is not null.
matchUserIdMap.put(acc.User_ID__c, acc);
}
else
{
// Contact map when Zlien user id is null.
matchCompanyMap.put(acc.name, acc);
}
}
for(Contact con : [Select Zlien_User_ID__c, Company__c, accountId, ownerId from Contact where Id IN :contactIds])
{
if(con.Zlien_User_ID__c != null)
{
if(matchUserIdMap.containsKey(con.Zlien_User_ID__c))
{
con.accountId = matchUserIdMap.get(con.Zlien_User_ID__c).Id;
}
else if(matchCompanyMap.containsKey(con.Company__c))
{
con.accountId = matchCompanyMap.get(con.Company__c).Id;
}
else
{
Account A = new Account(name = con.Company__c, ownerId = con.ownerId);
insert A;
con.accountId = A.Id;
}
}
else if(con.Company__c != null)
{
if(matchCompanyMap.containsKey(con.Company__c))
{
con.accountId = matchCompanyMap.get(con.Company__c).Id;
}
else
{
Account A = new Account(name = con.Company__c, ownerId = con.ownerId);
insert A;
con.accountId = A.Id;
}
}
update con;
}
}
}
Thanks in Advance,
Teja
Use filter in the for(Account acc : [Select User_ID__c, name, Id from Account])
All Answers
Use filter in the for(Account acc : [Select User_ID__c, name, Id from Account])
Thanks, I did use it and was able to figure it out. Used sets to get the contact fields and added those to the accounts filter.
Teja.