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
Daniel B ProbertDaniel B Probert 

Too many DMS statements: 151 trigger help

hey, i'm getting a dml 151 error with the below trigger when we bulk upload:

 

trigger AR_ContactUpdate on Academic_Record__c(after update){

Map<String, String> ARIDMap = new Map<String, String>();
for(Academic_Record__c e: trigger.new) {
     ARIDMap.put(e.ID, null);
    }

List<Contact> ContactsToUpdate = [select id,current_academic_record__c from contact where current_academic_record__c in: ARIDMap.KeySet()];
    for (Academic_Record__c ar: Trigger.New){
        if (ar.parentarmatch__c==true){
            Contact c = new Contact(Id=ar.Person__c);
                c.Accommodation__c=ar.Accommodation__c;
                c.Donor__c=ar.Donor__c;
                c.Form__c=ar.Form__c;
                update c;
        }
    }
}

 i've tried updating the code to this but then the trigger doesn't seem to work:

 

trigger AR_ContactUpdate on Academic_Record__c(after update){

Map<String, String> ARIDMap = new Map<String, String>();
for(Academic_Record__c e: trigger.new) {
     ARIDMap.put(e.ID, null);
    }
public void sav(){
List<Contact> ContactsToUpdate = [select id,current_academic_record__c from contact where current_academic_record__c in: ARIDMap.KeySet()];
    for (Academic_Record__c ar: Trigger.New){
        if (ar.parentarmatch__c==true){
            Contact c = new Contact(Id=ar.Person__c);
                c.Accommodation__c=ar.Accommodation__c;
                c.Donor__c=ar.Donor__c;
                c.Form__c=ar.Form__c;
                ContactsToUpdate.add(c);
        }
        if(ContactsToUpdate.size() > 0)
        insert ContactsToUpdate;
        }
    }
}

 any ideas what i'm missing?

 

cheers

dan

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You're on the right track, but you are updating the records that you retrieved from the database rather than those you created. Given that you don't use any of the values from the Contactstoupdate records, do you actually need those?  I would think you could simply iterate the trigger records, instantiate the contacts and then update them.  You just need to add them to a list and update that:

 

trigger AR_ContactUpdate on Academic_Record__c(after update){
    List<Contact> ContactsToUpdate = new List<Contact>();
    for (Academic_Record__c ar: Trigger.New){
        if (ar.parentarmatch__c==true){
            Contact c = new Contact(Id=ar.Person__c);
            c.Accommodation__c=ar.Accommodation__c;
            c.Donor__c=ar.Donor__c;
            c.Form__c=ar.Form__c;
            contactsToUpdate.add(c);
        }
    }

    if (contactsToUpdate.size()>0)
    {
       update contactsToUpdate;
    }
}

 

 

All Answers

bob_buzzardbob_buzzard

You're on the right track, but you are updating the records that you retrieved from the database rather than those you created. Given that you don't use any of the values from the Contactstoupdate records, do you actually need those?  I would think you could simply iterate the trigger records, instantiate the contacts and then update them.  You just need to add them to a list and update that:

 

trigger AR_ContactUpdate on Academic_Record__c(after update){
    List<Contact> ContactsToUpdate = new List<Contact>();
    for (Academic_Record__c ar: Trigger.New){
        if (ar.parentarmatch__c==true){
            Contact c = new Contact(Id=ar.Person__c);
            c.Accommodation__c=ar.Accommodation__c;
            c.Donor__c=ar.Donor__c;
            c.Form__c=ar.Form__c;
            contactsToUpdate.add(c);
        }
    }

    if (contactsToUpdate.size()>0)
    {
       update contactsToUpdate;
    }
}

 

 

This was selected as the best answer
Daniel B ProbertDaniel B Probert

grrr i knew i was close - cheers pal..