You need to sign in to do that
Don't have an account?
Andrew84
Delete records arbitrarily by id
Is there a way to delete records with apex by passing only the id? I see that you can use the API by sending an ID[] array, but I'm not sure how to use that through Apex.
This should explain what I'm attempting to do...
public void deleteRecords() { list<Id> ids; // add an account ids.add('001____________'); // add a contact ids.add('003____________'); // add a custom object ids.add('a0Y____________'); // delete them all delete(ids); }
You're on the right track... you can do something like this:
Now keep in mind, though this will work, all your triggers for the obejcts in the list being deleted will fire (the delete triggers that is), so if theres any conflicts with them (e.g. you're expecting the triggers to fire in a certain order) then its probably not a good idea to use this approach. However if there are no dependencies then it should be fine.
All Answers
You're on the right track... you can do something like this:
Now keep in mind, though this will work, all your triggers for the obejcts in the list being deleted will fire (the delete triggers that is), so if theres any conflicts with them (e.g. you're expecting the triggers to fire in a certain order) then its probably not a good idea to use this approach. However if there are no dependencies then it should be fine.
So the deletes are handled asynchronously? Good information to know. Thank you!
I wouldn't really use the word async, they're not happening async in the Apex context, just the records are being deleted in "parallel" relative to the object types being deleted (I doubt it's really parallel parallel).
I just tested this out. It appears to be deleting records syncronously based on the order of the list, although this could just be a coincidence.
This does not work because the contacts related to accounts will be deleted once the accounts are deleted.
This however does work.
Thank you for your help!