Loading

How to Create a Salesforce Apex Trigger on the Attachment or Note Object

Date de publication: May 30, 2026
Description

There are scenarios in Salesforce where you need to perform an action automatically when an Attachment or Note is added to a record. Standard Salesforce functionality does not support automation rules (such as Workflow Rules or Process Builder) directly on the Attachment or Note objects. 

However, you can use an Apex trigger to accomplish this.

Common Use Cases

  • Send an email to the Account Owner when a Note is added to an Account record (using the Note's ParentId to identify the Account).
  • Update a field on a Case record with the name of the most recently attached file when a new Attachment is added to that Case.
  • Log or audit Attachment activity across any Salesforce object.

In these scenarios, an Apex trigger on the Note or Attachment object is the supported approach.

Résolution

Important Considerations Before Creating a Trigger on Attachment or Note

  • Standard Salesforce does not allow modifications to the Note or Attachment object through the Salesforce UI (Setup). Triggers on these objects must be created using the Developer Console, VS Code with the Salesforce Extension Pack, or the Force.com IDE.
  • Apex triggers on the Attachment object fire for any parent object — not just the specific object you intend to target (for example, Cases). You must include object-type checking in your trigger logic to ensure it only runs for the correct parent object type.
  • The Note and Attachment objects are legacy objects. For new implementations, Salesforce recommends using Salesforce Files (ContentDocument and ContentVersion) instead, which support more modern automation patterns.

How to Create an Apex Trigger on the Attachment or Note Object

  1. Open the Developer Console (App Launcher | Developer Console, or via Setup).
  2. Click File | New | Apex Trigger.
  3. Give the trigger a name (for example, SetTitleToAttachment).
  4. Select "Attachment" or "Note" from the SObject dropdown.
  5. Click Submit. The trigger editor opens with a basic trigger template.
  6. Write your trigger logic. The trigger runs after insert by default.

Alternatively, you can create the trigger using VS Code with the Salesforce Extension Pack by running the "SFDX: Create Apex Trigger" command, or using the Force.com ANT Migration Tool.

 

How the Trigger Logic Works — Case Title Update Example

The following describes the logic for a trigger that updates a Case's custom Title field with the name of the most recently attached file. When a new Attachment record is inserted, the trigger reads the Attachment's Name (the file name) and its ParentId (the ID of the record the file was attached to). It then queries the Case record where the Case ID matches the ParentId and updates the Case's custom Title__c field with the Attachment's Name.
The trigger includes a check to ensure it targets only Case parent records. This is essential because Attachment triggers fire for all parent object types — without this check, the trigger would attempt to query Cases for Attachments added to Accounts, Contacts, Opportunities, or any other object, which would produce incorrect results or errors.


Sample code:

trigger SetTitleToAttachment on Attachment (after insert) {

String Title;
Id pId;

for(Attachment att: Trigger.new){
Title=att.Name;
pId=att.ParentId;
}

List<Case> c=[select Id , Title__c from Case where Id=:pId];

//assuming one record is fetched.
c[0].Title__c=Title;

update c[0];

}

 

Numéro d’article de la base de connaissances

000385957

 
Chargement
Salesforce Help | Article