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

System.LimitException: Too many DML statements: 151
Hi
I am getting the above error but can't seem to locate exactly what the issue is. A few other posts suggest that I need to remove the DML from within the For loops. I believe I have done this unless I am missing something.
Can the community assist me in this please?
I am getting the above error but can't seem to locate exactly what the issue is. A few other posts suggest that I need to remove the DML from within the For loops. I believe I have done this unless I am missing something.
Can the community assist me in this please?
//---------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- // // Name: // CI_Contact // // Purpose: // This will process changes that need to be made based on fields that may change when // contacts are inserted, edited, deleted and undeleted // // Created by: // Andrew Telford // // Date: // 2015-07-31 // //---------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- trigger CI_Contact on Contact ( after insert, after update, after delete, after undelete) { // Let us prepare some common variables //---------------------------------------------------------------------------------------------- system.debug('Set the variables'); // Common items STRING thisContactID; STRING thisContactOwnerID; STRING userID; STRING userProfileID; STRING errMsg = ''; //-- Let us get the details relating to this contact change //---------------------------------------------------------------------------------------------- system.debug('Contact[]'); //-- Set the details of the trigger to a variable for use through the trigger //---------------------------------------------------------------------------------------------- Contact[] strContact; if (Trigger.isDelete) { strContact = Trigger.old; } else { strContact = Trigger.new; } Set<ID> contactID = new Set<ID>(); Set<ID> ownerIds = new Set<ID>(); Set<ID> oldOwnerIds = new Set<ID>(); for(Contact objCont: strContact) { ownerIds.add(objCont.ownerId); if ( !TRIGGER.isInsert && !TRIGGER.isunDelete ) { oldOwnerIds.add(TRIGGER.oldMap.get(objCont.Id).ownerId); } } LIST<Contact> contsToUpdate = [SELECT Id, OwnerId, Approved_Owner_Change__c, Approved_Owner_Name__c FROM Contact WHERE Id IN :strContact AND Approved_Owner_Change__c = TRUE]; LIST<User> thisOwner = [Select Id FROM User WHERE ID IN :ownerIds]; LIST<Contact> ownerMMCount = [SELECT Id FROM Contact WHERE ownerId IN :ownerIds AND CCC_Adviser__c = TRUE]; LIST<User> oldOwner = [Select Id FROM User WHERE ID IN :oldOwnerIds]; LIST<Contact> oldOwnerMMCount = [SELECT Id FROM Contact WHERE ownerId IN :oldOwnerIds AND CCC_Adviser__c = TRUE]; FOR(Contact objCont: strContact) { //---------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- // // Owner Change Approved // //---------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------- system.debug('Set the variable values'); //-- Define any variables required //---------------------------------------------------------------------------------------------- BOOLEAN approvedOwnerChange; STRING newContactID = ''; //-- Assign Values to Variables //---------------------------------------------------------------------------------------------- contactID.add(objCont.Id); thisContactID = objCont.Id; thisContactOwnerID = objCont.OwnerId; userID = userinfo.getUserId(); userProfileID = userinfo.getProfileId(); approvedOwnerChange = objCont.Approved_Owner_Change__c; system.debug('Determine if owner is to change'); //-- Check for change forced by workflow //---------------------------------------------------------------------------------------------- if ( !TRIGGER.isAfter && !TRIGGER.isDelete ) { if( objCont.Approved_Owner_Change__c ) { //-- Check to see if we are updating the owner //---------------------------------------------------------------------------------------------- if( TRIGGER.isupdate && objCont.Approved_Owner_Change__c ) { system.debug('Owner is to change'); //-- Set the update list //---------------------------------------------------------------------------------------------- List<Contact> updateOwner = new List<Contact>(); FOR (Contact cont: contsToUpdate) { Boolean alterOwner = FALSE; IF ( cont.Approved_Owner_Change__c = TRUE ) { alterOwner = TRUE; } if (alterOwner) { cont.OwnerID = cont.Approved_Owner_Name__c; cont.Approved_Owner_Change__c = FALSE; newContactID = cont.Approved_Owner_Name__c; } updateOwner.add(cont); } //-- Execute the update //---------------------------------------------------------------------------------------------- if (!updateOwner.isEmpty()) { try{ update updateOwner; } catch (Exception ex) { System.debug('Could not update New Contact Owner [' + newContactID + '] cause: ' + ex.getCause() + 'APEX TRIGGER: CI_Trigger_Contact');} } } } //-- End Check for automated change after insert } // // End Approve Owner Change // //-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- // // Magic Movers // // This works in conjunction with: // Validations // Magic Mover Addition (03dc00000009014) // Magic Mover REmoval (03dc00000009080) // // Workflow Field Update // New Contact (01Q20000000Es9Z) // Set Magic Mover to False (04Yc000000090G1) // //-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- //-- Magic Movers BOOLEAN magicMover; DECIMAL userMagicCount; //-- Select the Owner Information LIST<User> updateMM = new LIST<User>(); system.debug( 'Size of thisUser: ' + thisOwner.size()); FOR ( User mm1: thisOwner ) { mm1.Magic_Movers_Count__c = ownerMMCount.size(); updateMM.add(mm1); } //-- For Current Owner Loop //-- Process the updates //------------------------------------------------------------------------------------------------------------------------------ system.debug('size of: ' + updateMM.size()); if (!updateMM.isEmpty()) { try{ update updateMM; } catch (Exception ex) { objCont.addError(' Error Updating ' + ex.getCause()); System.debug('Could not update User [' + thisContactOwnerID + '] cause: ' + ex.getCause() + 'APEX TRIGGER: CI_Trigger_Contact'); } } //-- Check to see if the Owner has changed //----------------------------------------------------------------------------------------------------------------------------- LIST<User> updateOldMM = new LIST<User>(); system.debug( 'Size of oldOwner: ' + oldOwner.size()); FOR ( User mm2: oldOwner ) { mm2.Magic_Movers_Count__c = oldOwnerMMCount.size(); updateOldMM.add(mm2); } //-- End For Old Owner Loop //-- Process the updates //------------------------------------------------------------------------------------------------------------------------------ system.debug('size of: ' + updateOldMM.size()); if (!updateOldMM.isEmpty()) { try{ update updateOldMM; } catch (Exception ex) { objCont.addError(' Error Updating ' + ex.getCause()); System.debug('Could not update User [' + thisContactOwnerID + '] cause: ' + ex.getCause() + 'APEX TRIGGER: CI_Trigger_Contact'); } } // // End Magic Movers // //-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- } // // End Contact Loop // //-------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------------- }
Move the below code out of the FOR loop(line 48 FOR(Contact objCont: strContact)).
if (!updateOwner.isEmpty())
{
try{ update updateOwner; }
catch (Exception ex)
{ System.debug('Could not update New Contact Owner [' + newContactID + '] cause: ' + ex.getCause() + 'APEX TRIGGER: CI_Trigger_Contact');}
}
Move the below line out of the for loop.
List<Contact> updateOwner = new List<Contact>();
Thanks
Kumar
PS: If this answers your question, Please mark this as 'Best Answer'.
I tried that and I am now getting the following error when using Apex Data Loader.
New Code:
When I do the upload, it updates the first item in each batch.ie I upload to SalesForce via Apex DataLoader in batches of 200 or 10 and 1 in each batch is successful while the rest of the item error with the