You need to sign in to do that
Don't have an account?
Nagesh E
can i use "delete" command to delete some records using trigger
trigger TobedeleteContact on Contact (after insert, after update) {
List<Contact> contactToBeDeleted = new List<Contact>();
Set<Id> ownerIds = new Set<Id>();
for (Contact c : Trigger.new)
ownerIds.add(c.OwnerId);
for (Contact c : Trigger.new)
{
if (c.To_Be_Deleted__c == true)
{
contactToBeDeleted.add(c);
}
}
if(!contactToBeDeleted.isEmpty())
{
delete contactToBeDeleted;
}
}
I am getting error
I updated my post to reflect a small error. You need to make sure that there's sufficient access on the method call.
All Answers
You can't delete a record involved in a trigger. Instead, create a class with a @future method, and have your asynchronous method delete the record. Your @future method should run after the lock on the rows have been released.
Thanks for quick reply,
i didnot understand what you mean by @future class, if you don't mind can you send me an example.
Thanks
You would do something like this:
The trick here is that the @future method executes sometime after the trigger has executed and the lock has been released (usually, anyways). This function can therefore delete the records that were previously marked for deletion, usually within a few seconds after processing the trigger (see the documentation on "future annotation" for timing considerations).
Edit: Added permission value missing from this post.
Thanks,
still I am getting error:
Error: Compile Error: Method is not visible: ToDeleteAsyncClass.deleteContacts(SET<Id>) at line 8 column 3
I updated my post to reflect a small error. You need to make sure that there's sufficient access on the method call.