+ Start a Discussion
tmbarrytmbarry 

Developer script exception

Hello All, 

 

I am getting a Developer script exception error on one of my triggers.  The error states:

 

Apex script unhandled trigger exception by user/organization: XXXXXXXXXXXXXX/xxxxxxxxxxxxxx

 

Update_NCOA_Address_v01: System.LimitException: Too many DML statements: 151

 

trigger Update_NCOA_Address_v01 on printsf__Collateral_Send_History__c (after insert, after update) {
// Update the appropriate SD_Member__c record with NCOA address from PrintSF

For(printsf__Collateral_Send_History__c CSH : Trigger.new){
// Get the SD Member Id from the recipient id field 
//String strSDId = CSH.printsf__Recipient_ID__c;

If(CSH.printsf__NCOA_Result__c=='None'){
SD_Member__c SD = New SD_Member__c (id=CSH.printsf__Recipient_ID__c,
                  NCOA_Address__c = CSH.printsf__Street__c,
                  NCOA_City__c = CSH.printsf__city__c,
                  NCOA_State__c = CSH.printsf__State__c,
                  NCOA_Zip__c = CSH.printsf__Zip_Code__c,
                  NCOA_Error_Code__c = CSH.printsf__NCOA_Result__c,
                  NCOA_Error_Description__c = CSH.printsf__NCOA_Result_Description__c);
Update SD;}

If(CSH.printsf__NCOA_Result__c=='Fowarded'){
SD_Member__c SD = New SD_Member__c (id=CSH.printsf__Recipient_ID__c,
                  NCOA_Address__c = CSH.printsf__Street__c,
                  NCOA_City__c = CSH.printsf__city__c,
                  NCOA_State__c = CSH.printsf__State__c,
                  NCOA_Zip__c = CSH.printsf__Zip_Code__c);
Update SD;}

If(CSH.printsf__NCOA_Result__c=='Failed'){
SD_Member__c SD = New SD_Member__c (id=CSH.printsf__Recipient_ID__c,
                  NCOA_Address__c = CSH.printsf__Street__c,
                  NCOA_City__c = CSH.printsf__city__c,
                  NCOA_State__c = CSH.printsf__State__c,
                  NCOA_Zip__c = CSH.printsf__Zip_Code__c,
                  Address_Bad__c = True);
                  
Update SD;}

}}

What happens is, I send a between 3,000 and 5,000 records to a third party print vendor and they return their validity check results, the "none", "fowarded" or "failed" to their printsf_collateral_send_history__c object.  As each record returns, the trigger updates fields on my custom object SD_Member__c.  

 

I thought I had "bulkified" the trigger, but maybe not. 

 

Thanks for the help,

 

Todd B.

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Try this.

trigger Update_NCOA_Address_v01 on printsf__Collateral_Send_History__c(after insert, after update) {
    // Update the appropriate SD_Member__c record with NCOA address from PrintSF
    List<SD_Member__c > recordsToUpdate = new List<SD_Member__c >();
    For(printsf__Collateral_Send_History__c CSH: Trigger.new) {
        // Get the SD Member Id from the recipient id field 
        //String strSDId = CSH.printsf__Recipient_ID__c;

        If(CSH.printsf__NCOA_Result__c == 'None') {
            SD_Member__c SD = New SD_Member__c(id = CSH.printsf__Recipient_ID__c,
                NCOA_Address__c = CSH.printsf__Street__c,
                NCOA_City__c = CSH.printsf__city__c,
                NCOA_State__c = CSH.printsf__State__c,
                NCOA_Zip__c = CSH.printsf__Zip_Code__c,
                NCOA_Error_Code__c = CSH.printsf__NCOA_Result__c,
                NCOA_Error_Description__c = CSH.printsf__NCOA_Result_Description__c);
            recordsToUpdate.add(SD);
        }

        If(CSH.printsf__NCOA_Result__c == 'Fowarded') {
            SD_Member__c SD = New SD_Member__c(id = CSH.printsf__Recipient_ID__c,
                NCOA_Address__c = CSH.printsf__Street__c,
                NCOA_City__c = CSH.printsf__city__c,
                NCOA_State__c = CSH.printsf__State__c,
                NCOA_Zip__c = CSH.printsf__Zip_Code__c);
            recordsToUpdate.add(SD);
        }

        If(CSH.printsf__NCOA_Result__c == 'Failed') {
            SD_Member__c SD = New SD_Member__c(id = CSH.printsf__Recipient_ID__c,
                NCOA_Address__c = CSH.printsf__Street__c,
                NCOA_City__c = CSH.printsf__city__c,
                NCOA_State__c = CSH.printsf__State__c,
                NCOA_Zip__c = CSH.printsf__Zip_Code__c,
                Address_Bad__c = True);

            recordsToUpdate.add(SD);
        }

    }
  
  update recordsToUpdate;
}

 we just removed the DML from inside of the loop.

 

 

 

 

All Answers

Bhawani SharmaBhawani Sharma
You are updating SD records in loop. Create a list and add records in that list. Update the list outside the lopp.

List<SD_Member__c > records = new List<SD_Member__c >();

for() {
records.add(SD);
}

update records;
tmbarrytmbarry

Thank you for your help Bhawani, but could you be a little more specific?  I am new to coding.  

Avidev9Avidev9

Try this.

trigger Update_NCOA_Address_v01 on printsf__Collateral_Send_History__c(after insert, after update) {
    // Update the appropriate SD_Member__c record with NCOA address from PrintSF
    List<SD_Member__c > recordsToUpdate = new List<SD_Member__c >();
    For(printsf__Collateral_Send_History__c CSH: Trigger.new) {
        // Get the SD Member Id from the recipient id field 
        //String strSDId = CSH.printsf__Recipient_ID__c;

        If(CSH.printsf__NCOA_Result__c == 'None') {
            SD_Member__c SD = New SD_Member__c(id = CSH.printsf__Recipient_ID__c,
                NCOA_Address__c = CSH.printsf__Street__c,
                NCOA_City__c = CSH.printsf__city__c,
                NCOA_State__c = CSH.printsf__State__c,
                NCOA_Zip__c = CSH.printsf__Zip_Code__c,
                NCOA_Error_Code__c = CSH.printsf__NCOA_Result__c,
                NCOA_Error_Description__c = CSH.printsf__NCOA_Result_Description__c);
            recordsToUpdate.add(SD);
        }

        If(CSH.printsf__NCOA_Result__c == 'Fowarded') {
            SD_Member__c SD = New SD_Member__c(id = CSH.printsf__Recipient_ID__c,
                NCOA_Address__c = CSH.printsf__Street__c,
                NCOA_City__c = CSH.printsf__city__c,
                NCOA_State__c = CSH.printsf__State__c,
                NCOA_Zip__c = CSH.printsf__Zip_Code__c);
            recordsToUpdate.add(SD);
        }

        If(CSH.printsf__NCOA_Result__c == 'Failed') {
            SD_Member__c SD = New SD_Member__c(id = CSH.printsf__Recipient_ID__c,
                NCOA_Address__c = CSH.printsf__Street__c,
                NCOA_City__c = CSH.printsf__city__c,
                NCOA_State__c = CSH.printsf__State__c,
                NCOA_Zip__c = CSH.printsf__Zip_Code__c,
                Address_Bad__c = True);

            recordsToUpdate.add(SD);
        }

    }
  
  update recordsToUpdate;
}

 we just removed the DML from inside of the loop.

 

 

 

 

This was selected as the best answer
tmbarrytmbarry
Thank you very much!!!