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
MeekoMeeko 

When calling the method "createContact" the system throws an error "Too Many DML Statements 151", which line needs to be updated?

Best Answer chosen by Meeko
Arpit Jain7Arpit Jain7
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

All Answers

Arpit Jain7Arpit Jain7
Hello Meeko,

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
 
MeekoMeeko
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 {
        
            throw LastNameException
            
            
            
            
Arpit Jain7Arpit Jain7
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
This was selected as the best answer
MeekoMeeko
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}”/>
Arpit Jain7Arpit Jain7
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
MeekoMeeko
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?
Arpit Jain7Arpit Jain7
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.

Thanks
 
Luciano Castro 2018Luciano Castro 2018
How do I add the filter in the code to limit the number of records?