Print this page

How to add trigger on Attachment and Note?

Knowledge Article Number 000181538
Description There would be a condition when an action is to be taken on Insert of an Attachment.

For example:
>> One wants to send email to Account Owner when a Note is added with Parent ID as of Account.
>> One wants to update the field on Case with the name of latest Note added on the particular Case.

In these scenarios, we would require a trigger on Note or Attachment object.
Resolution Standard Salesforce functionality does not allow any modification to Note and Attachment object.

Best Approach to create Attachment trigger can be: Create trigger from Developer console.

Need to click New: Apex Trigger: Then you can select SObject as "Attachment" form dropdown.

A trigger can be written on Attachment using Force.com IDE or ANT.

A sample code would be:
 
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];
 
}

Note: There is no way to write a trigger using the Salesforce.com UI (Portal).




promote demote