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

System.LimitException: Too many DML statements
Gang,
How is this supposed to be written, I know I need to bulkify, buts its not working for me:
trigger Contact_BeforeInsertBeforeUpdate on Contact (before insert, before update) {
for(Contact c: Trigger.new){
if(c.Relo_Contact_Name__c!=null){
String ContactId=c.Id;
for(Related_Contact__c rc:[select Id,ContactId__c,Relo_Contacts__c from Related_Contact__c where ContactId__c = :ContactId]) {
rc.Relo_Contacts__c=c.Relo_Contact_Name__c;
update rc;
}
}
}
}
You should be building an array or list "Related_Contact__c" object and store them and execute the DML by issuing one DML statement rather issuing for each instance singly.
trigger Contact_BeforeInsertBeforeUpdate on Contact (before insert, before update) {
for(Contact c: Trigger.new){
if(c.Relo_Contact_Name__c!=null){
String ContactId=c.Id;
Related_Contact__c arr_rc[]= new Related_Contact__c(); // try this
interger i=0;
for(Related_Contact__c rc:[select Id,ContactId__c,Relo_Contacts__c from Related_Contact__c where ContactId__c = :ContactId]) {
rc.Relo_Contacts__c=c.Relo_Contact_Name__c;
arr_rc[i++] = rc
// update rc; //comment this line
}
update arr_c; //add this
}
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Ispita,
sorry, there is a syntax error around:
Related_Contact__c arr_rc[]= new Related_Contact__c();
doesn't seem to be working.
trigger Contact_BeforeInsertBeforeUpdate on Contact (before insert, before update) {
for(Contact c: Trigger.new){
if(c.Relo_Contact_Name__c!=null){
String ContactId=c.Id;
Related_Contact__c arr_rc[]= new Related_Contact__c(); //THIS IS ERRORING OUT Expecting a semi-colon, found '['
interger i=0;
for(Related_Contact__c rc:[select Id,ContactId__c,Relo_Contacts__c from Related_Contact__c where ContactId__c = :ContactId]) {
rc.Relo_Contacts__c=c.Relo_Contact_Name__c;
arr_rc[i++] = rc;
}
update arr_c;
}
}
}
Try the following:-
rigger Contact_BeforeInsertBeforeUpdate on Contact (before insert, before update) {
for(Contact c: Trigger.new){
if(c.Relo_Contact_Name__c!=null){
String ContactId=c.Id;
List<Related_Contact__c> listContacts = new List<Related_Contact__c>(); // try this
interger i=0;
for(Related_Contact__c rc:[select Id,ContactId__c,Relo_Contacts__c from Related_Contact__c where ContactId__c = :ContactId]) {
rc.Relo_Contacts__c=c.Relo_Contact_Name__c;
listContacts.add(rc); //change this
}
database.update(listContacts) ;
}
}
}
OK, Now:
System.Exception: Too many SOQL queries: 21
trigger Contact_BeforeInsertBeforeUpdate on Contact (before insert, before update) {
for(Contact c: Trigger.new){
if(c.Relo_Contact_Name__c!=null){
String ContactId=c.Id;
for(Related_Contact__c rc:[select Id,ContactId__c,Relo_Contacts__c from Related_Contact__c where ContactId__c = :ContactId]) {
rc.Relo_Contacts__c=c.Relo_Contact_Name__c;
update rc;
}
}
}
}
The query highlighted in red needs to be out of the outer for loop.
That puts us back to the first issue. Still not a solution.
Try this,
Trigger Contact_BeforeInsertBeforeUpdate on Contact (before insert, before update) {
List<Id> Contactids = new List<id>(); // try this
for(Contact c: Trigger.new)
{
if(c.Relo_Contact_Name__c!=null){
Contactids.add(c.Id);
}
}
List<Related_Contact__c> listContacts = new List<Related_Contact__c>(); // try this
for(Related_Contact__c rc:[select Id,ContactId__c,Relo_Contacts__c from Related_Contact__c where ContactId__c in :Contactids])
{
rc.Relo_Contacts__c=c.Relo_Contact_Name__c;
listContacts.add(rc);
}
database.update(listContacts) ;
}
I have given you pointers now you need to tweek your code accordingly.
public with sharing class GetAllAccountsCntrl {
@AuraEnabled(cacheable=true)
public static List<Account> processAllAccounts()
{
List<Account> lstAcc = new List<Account>();
for(Account acc:[SELECT Id, Name,AccountSource,Website, Createddate FROM Account Limit 10])
{
acc.Name = 'LWC Testing...';
lstAcc.add(acc);
}
Database.update(lstAcc,false);
return lstAcc;
}
}
Thanks!!
please choose best answer.