I have a custom object Orderregel__c with attachments. There is an approval process on the object Orderregel__c.
How do I write a trigger to prevent deletion of attachment (before delete trigger)?
Please some help im a newbie in this area.
I have a custom object Orderregel__c with attachments. There is an approval process on the object Orderregel__c.
How do I write a trigger to prevent deletion of attachment (before delete trigger)?
Please some help im a newbie in this area.
The only basic idea you need to grasp is that you can use the SObject.addError
method to validate records which are being acted on in the trigger context. See Trigger Exceptions:
Triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged.
So the basic idea (ignoring trigger best practice here and putting the logic directly in the trigger body for the sake of brevity):
trigger MyTrigger on Attachment (after delete)
{
for (Attachment record : trigger.old)
{
if (/*someCondition*/)
{
record.addError('Some Message');
}
}
}