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
DManelskiDManelski 

Problems upgrading to API version 14.0

Hello,

We're trying to get all of our code up to API version 14.0 but I'm running into a problem with one of our triggers. 

It was originally created in API version 10.0.  The after update trigger grabs a trigger set via trigger.new, makes changes or deletes the records according to some basic logic.  This worked fine in API version 10.0 but under version 14.0, tests are now failing and I'm receiving this error:

"System.SObjectException: DML statment cannot operate on trigger.new or trigger.old"

This particular error is being thrown on a delete DML in the trigger. Any ideas?

JeremyKraybillJeremyKraybill
You'll need to post your code. The error is probably exactly as described in the error message: you cannot directly perform DML operations on the trigger sets.

Jeremy Kraybill
Austin, TX
DManelskiDManelski
Here is the code in question:

Code:
trigger ONEN_HouseholdChangeInHouseholding on ONEN_Household__c (after update) {
 //system.debug('ONEN_HouseholdChangeInHouseholding FIRED');
 //list to hold HH's that are changing membership count
 List<Id> HHChanging = new List<Id>();
 //list to hold HH's that now have 0 members, for deleting
 List<ONEN_Household__c> HHForDelete = new List<ONEN_Household__c>();
 //list to hold Contacts that need to be updated with new Household Name and Greeting
 List<Contact> ContactsForUpdate = new List<Contact>();
 
 ONEN_Household__c HouseholdBeforeChange;
 
 //put all changing HH's in the map
 for (ONEN_Household__c HouseholdAfterChange : Trigger.new) {
  boolean householdAdded = false;
  HouseholdBeforeChange = trigger.oldmap.get(HouseholdAfterChange.id);
  boolean addressChange = HouseholdBeforeChange.MailingStreet__c!= HouseholdAfterChange.MailingStreet__c|| HouseholdBeforeChange.MailingCity__c!= HouseholdAfterChange.MailingCity__c|| HouseholdBeforeChange.MailingState__c!=HouseholdAfterChange.MailingState__c|| HouseholdBeforeChange.MailingPostalCode__c!=HouseholdAfterChange.MailingPostalCode__c|| HouseholdBeforeChange.MailingCountry__c!=HouseholdAfterChange.MailingCountry__c|| HouseholdBeforeChange.MailingCounty__c!= HouseholdAfterChange.MailingCounty__c;
  boolean nameOverride = HouseholdBeforeChange.Override_Household_Name__c!= HouseholdAfterChange.Override_Household_Name__c||HouseholdBeforeChange.Override_Greeting__c!= HouseholdAfterChange.Override_Greeting__c;
  boolean memberCountChange = HouseholdBeforeChange.Member_Count__c!=HouseholdAfterChange.Member_Count__c;
  boolean emptyHousehold = HouseholdAfterChange.Member_Count__c==0;
  
  //if member count changes
  if ((memberCountChange&&!emptyHousehold)||addressChange||nameOverride) {
   //add to the changing list
   //HHChangingMap.put(HouseholdAfterChange.id,HouseholdAfterChange);
   HHChanging.add(HouseholdAfterChange.id);
   householdAdded=true;     
  } 
  
  if (emptyHousehold) {
   HHForDelete.add(HouseholdAfterChange);
  }

 }
 if (HHForDelete.size()>0) {    
delete HHForDelete; } } else { //system.debug('ONEN_HouseholdChangeInHouseholding DID NOT EXECUTE'); } }

 



Message Edited by DManelski on 12-30-2008 02:48 PM