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
Arun KArun K 

System.LimitException: Too many DML statements: 151

I tried all the possible but couldnt find the right way..!!

 

I know the reason why I am getting "System.LimitException: Too many DML statements: 151". Its because Iam updating inside for Loop.

 

I I tried to remove from inside for loop But the updation is not happening.

 

 

 " 

public void saveme()
{
System.debug('In Save Me Method===');
try
{
Account updatedAcc;
String__c String;

List<Account> updatedAccList = new List<Account>();
List<String__c> updatedStringList = new List<String__c>();

System.debug('Records in Call plan === '+ListWrapper.size());


for(Wrapper wrapObj:ListWrapper)
{
updatedAcc = new Account();
updatedAcc = wrapObj.acc;
updatedStringList = wrapObj.acc.Strings__r;
System.debug('String === '+updatedStringList);
updatedAccList.add(updatedAcc);
update updatedStringList;

}

update updatedAccList;
}
catch(Exception e)
{
System.debug('Exception in Saving records == '+e.getMessage());
}

}

 

 "

Please Help me in updating the record and Not to get governor limit error..

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You can use the addAll method to build a full list of all the String__c objects to update rather then overriding the list of string objects per iteration. Something like this should do the trick:

 

public void saveme()
{
	System.debug('In Save Me Method===');
	try
	{
		Account updatedAcc;
		String__c String;
		
		List<Account> updatedAccList = new List<Account>();
		List<String__c> updatedStringList = new List<String__c>();
		
		System.debug('Records in Call plan === '+ListWrapper.size());
		
		
		for(Wrapper wrapObj:ListWrapper)
		{
			updatedAcc = new Account();
			updatedAcc = wrapObj.acc;
			
			if (wrapObj.acc.Strings__r != null && !wrapObj.acc.Strings__r.isEmpty())
			{
				updatedStringList.addAll(wrapObj.acc.Strings__r);
			}			
			System.debug('String === '+updatedStringList);
			updatedAccList.add(updatedAcc);				
		}
		
		update updatedStringList;
		update updatedAccList;
	}
	catch(Exception e)
	{
		System.debug('Exception in Saving records == '+e.getMessage());
	}
	
}

 

All Answers

Sean TanSean Tan

You can use the addAll method to build a full list of all the String__c objects to update rather then overriding the list of string objects per iteration. Something like this should do the trick:

 

public void saveme()
{
	System.debug('In Save Me Method===');
	try
	{
		Account updatedAcc;
		String__c String;
		
		List<Account> updatedAccList = new List<Account>();
		List<String__c> updatedStringList = new List<String__c>();
		
		System.debug('Records in Call plan === '+ListWrapper.size());
		
		
		for(Wrapper wrapObj:ListWrapper)
		{
			updatedAcc = new Account();
			updatedAcc = wrapObj.acc;
			
			if (wrapObj.acc.Strings__r != null && !wrapObj.acc.Strings__r.isEmpty())
			{
				updatedStringList.addAll(wrapObj.acc.Strings__r);
			}			
			System.debug('String === '+updatedStringList);
			updatedAccList.add(updatedAcc);				
		}
		
		update updatedStringList;
		update updatedAccList;
	}
	catch(Exception e)
	{
		System.debug('Exception in Saving records == '+e.getMessage());
	}
	
}

 

This was selected as the best answer
Arun KArun K

Worked like Magic.. 

 

Thanks for the quick reply..