You are doing DML within for loop which is causing this issue.
Try below code
public with sharing class ApexClassHomeWork { public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page public ApexClassHomeWork() { accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10]; }
public void createContact(Id acctId) { list<contact>contList = newlist<contact>(); for(Integer i=0; i<500; i++) { Contact c = new Contact(AccountId=acctId, "last name"); contList.add(c); } if(contList.size()>0) update contList; } }
This is salesforcelimitation. Once check your code that you are not doing any DML operation inside for loop which generally cause this error. Also if you can post your code that would be easy to trouble shoot the issue.
public with sharing class ApexClassHomeWork { public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page public ApexClassHomeWork() { accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10]; }
public void createContact(Id acctId) { for(Integer i=0; i<500; i++) { Contact c = new Contact(AccountId=acctId, "last name"); insert c; } } }
public class Contact P public contact(ID.AccountID) throw LastNameException if lastName.isEmpty {
You are doing DML within for loop which is causing this issue.
Try below code
public with sharing class ApexClassHomeWork { public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page public ApexClassHomeWork() { accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10]; }
public void createContact(Id acctId) { list<contact>contList = newlist<contact>(); for(Integer i=0; i<500; i++) { Contact c = new Contact(AccountId=acctId, "last name"); contList.add(c); } if(contList.size()>0) update contList; } }
Thanks Arpit. However, when calling the method "createContact", the system throws an error "LastName is required", which line in this case needs to updated? Would the correct syntax be - <apex:inputField value = “{!con.lastname}”/>
Hello Meeko, You are not passing value in lastName field. Please try below updated code.
public with sharing class ApexClassHomeWork { public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page public ApexClassHomeWork() { accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10]; }
public void createContact(Id acctId) { list<contact>contList = newlist<contact>(); for(Integer i=0; i<500; i++) { Contact c = new Contact(AccountId=acctId, lastName = 'last name'+i); contList.add(c); } if(contList.size()>0) update contList; } }
Thanks Arpit! Thanks so much! When the page loads, the system throws this error "Too many query rows: 500,001", please explain and identify the line that needs to be updated to resolve this error. Would this be because there's an exception that it returns 50000 records?
Yes Meeko, In salesforce there is governor limit that you can not query more then 50000 records in single query. Put filter in your query along with limit on number of records.
Try below code
public with sharing class ApexClassHomeWork
{
public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page
public ApexClassHomeWork()
{
accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10];
}
public void createContact(Id acctId)
{
list<contact>contList = newlist<contact>();
for(Integer i=0; i<500; i++)
{
Contact c = new Contact(AccountId=acctId, "last name");
contList.add(c);
}
if(contList.size()>0)
update contList;
}
}
Thanks
Arpit
All Answers
This is salesforcelimitation. Once check your code that you are not doing any DML operation inside for loop which generally cause this error. Also if you can post your code that would be easy to trouble shoot the issue.
Thanks
Arpit
{
public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page
public ApexClassHomeWork()
{
accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10];
}
public void createContact(Id acctId)
{
for(Integer i=0; i<500; i++)
{
Contact c = new Contact(AccountId=acctId, "last name");
insert c;
}
}
}
public class Contact P
public contact(ID.AccountID) throw LastNameException
if lastName.isEmpty {
throw LastNameException
Try below code
public with sharing class ApexClassHomeWork
{
public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page
public ApexClassHomeWork()
{
accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10];
}
public void createContact(Id acctId)
{
list<contact>contList = newlist<contact>();
for(Integer i=0; i<500; i++)
{
Contact c = new Contact(AccountId=acctId, "last name");
contList.add(c);
}
if(contList.size()>0)
update contList;
}
}
Thanks
Arpit
<apex:inputField value = “{!con.lastname}”/>
You are not passing value in lastName field. Please try below updated code.
public with sharing class ApexClassHomeWork
{
public List<Account> accs {get; set;}
// retrieves the list of accounts backing the page
public ApexClassHomeWork()
{
accs=[select id, Name, BillingStreet, BillingCity, BillingPostalCode FROM Account WHERE Name LIKE 'Test%' LIMIT 10];
}
public void createContact(Id acctId)
{
list<contact>contList = newlist<contact>();
for(Integer i=0; i<500; i++)
{
Contact c = new Contact(AccountId=acctId, lastName = 'last name'+i);
contList.add(c);
}
if(contList.size()>0)
update contList;
}
}
Thanks
Arpit
When the page loads, the system throws this error "Too many query rows: 500,001", please explain and identify the line that needs to be updated to resolve this error. Would this be because there's an exception that it returns 50000 records?
Thanks