List<MyObject__c> delRecords = [SELECT Id FROM MyObject__c ];
delete delRecords;
My problem is that I have a custom validation in an Apex class that is preventing the deletion of about half the records. The validation essentially says if the parent of this record has a specific status value, prevent the user from deleting the child record.
Is there anyway I can modfiy the above script to tell Salesforce to ignore this Apex class?
Paul,
You can try temporarily changing the status that is causing you problems. You could first query all accounts that have that status, change it, run the delete, and then change that status back.
I would not run this without testing in a Full or Partial Sandbox first. And then, make sure you have no errors and your data all looks good. You want to really think through everything in this step. Consider all tangents that could be affected by changing the Account status temporarily, and check that everything is good to go.
Then once you are certain the code works and you have considered everything you need to. You can try it in Production.
Here's the code to test, change the object references, and statuses accordingly:
// First we got all the Parent Object records that are causing the error
List<Parent_Object__c> parentObjectRecords = [
SELECT Id
FROM Parent_Object__c
WHERE Status = 'Specific Status Value' // change this to the offending status
];
// Now we can change their statuses to something else that doesn't throw the error
List<Parent_Object__c> updateParentObjList = new List<Parent_Object__c>();
for (Parent_Object__c parentObjectRecord : parentObjectRecords) {
parentObjectRecord.Status = 'Another value'; // change this to a status that does not throw an error
updateParentObjList.add(parentObjectRecord);
}
// Next, we run the update
update updateParentObjList;
// Now that the conflicting Status value is changed, we can perform the delete
List<MyObject__c> delRecords = [SELECT Id FROM MyObject__c ];
delete delRecords;
// Finally, we run a restore to bring back the original status value
List<Parent_Object__c> restoreParentObjList = new List<Parent_Object__c>();
for (Parent_Object__c parentObjectRecord : parentObjectRecords) {
parentObjectRecord.Status = 'Specific Status Value'; // change this to the original offending status
restoreParentObjList.add(parentObjectRecord);
}
// update the Parent Objects back to their original statuses
update restoreParentObjList;
I hope that helps!