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
EricG102938EricG102938 

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;
   }
  }
 }
}

Ispita_NavatarIspita_Navatar

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.

 

EricG102938EricG102938

Ispita,

sorry, there is a syntax error around:

Related_Contact__c arr_rc[]= new Related_Contact__c();

doesn't seem to be working.

EricG102938EricG102938

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;
}
}
}

Ispita_NavatarIspita_Navatar

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) ;
}
}
}

EricG102938EricG102938

OK, Now:

System.Exception: Too many SOQL queries: 21

Ispita_NavatarIspita_Navatar

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.

EricG102938EricG102938

That puts us back to the first issue. Still not a solution.

Ispita_NavatarIspita_Navatar

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.

Anand kurubaAnand kuruba
Hi,

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.