0

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.

1
  • 2
    Hi Gianni, welcome to SFSE! Please take the time to visit the Help center and read How do I ask a good question. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you. SFSE is a Q&A forum where we look to help each other by asking and answering questions about real problems or errors with our code. I recommend you visit Trailhead and go through the Trigger Module. Posting requests to write code for you generally get closed.
    – crmprogdev
    Commented Jan 19, 2017 at 13:46

1 Answer 1

1

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');
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .