Creating Trigger on Attachment in Salesforce

Lots of time i have seen the question on salesforce developer forum regarding the trigger on Attachment. As this is not a straightway, still we can have trigger on Attachment.

We cannot create trigger on Attachment from the standard salesforce UI however we will have to use the eclipse.

Before insert Trigger on Attachment using eclipse in Salesforce
Before insert Trigger on Attachment using eclipse in Salesforce

While creating the trigger, we have to keep some important points. We should be very careful while writing the trigger on Attachment, as it will be used by any standard or custom Object and therefore the criteria to run trigger for “particular object only” must be handled carefully.

In this article i will create the trigger which will check for the parent object “Opportunity”. If the object is opportunity then it will check let say for the file name cannot contain word “shivasoft”.

trigger CheckAttachmentName on Attachment (before insert) {
	for (Attachment
		String parentObjId = att.ParentId;
		//006 is the starting sting in ID for all opportunities
		if(parentObjId.startsWith('006') && att.Name.toLowerCase().contains('shivasoft'))
				att.addError('Cannot upload file with name containing - shivasoft');

	//This method will return true if the id belongs to Opportunity
	private Boolean isOpportunityParent(String id)
		for(Opportunity opp : [SELECT Id FROM Opportunity WHERE Id = :id])
			return true;
		return false;

In the above code, we are checking that the Parent id starts with “006” or not. As starting three characters in ID identifies the type of the object.

Alternate solution – Not recommended :
There is one method created named “isOpportunityParent” which uses the SOQL approach but it is not recommended and may cause SOQL limit exception in bulk processing. We can supply the list of Id in method and return the Map which will contain ParentId as Key and Boolean as value identifying that the particular id belongs to opportunity or not?

Error Message on Attachment using Trigger in Salesforce
Error Message on Attachment using Trigger in Salesforce





Related Posts


8 responses to “Creating Trigger on Attachment in Salesforce”

  1. Suri Avatar

    hi sir how to get time of hour,month of date in trigger
    pls write example from datetime
    and how many methods in datetime,creation of datetime

    1. JitendraZaa Avatar

      Hi Suri,
      what do u want to say by “ get time of hour,month of date in trigger”.
      Please explain in detail abt ur problem

  2. lahari Avatar

    hai sir,
    i am having a doubt on to add an relationship field in triggers if some field is equal to some value.for example if i have given type=prospect in account object then i want to display oppurtunities who related to accounts objects.

  3. Suma Anand Avatar

    I am trying to create a Trigger on the parent object to see if the attachment is provided by the user or not. If it is, then check box field needs to be TRUE on the Parent object.

    1. Lisa E Avatar
      Lisa E

      Did you find the resolution to this? I am trying to do the exact same thing.

  4. arpit Avatar

    Hi Jitendra, is it possible to have something on Notes & Attachment to log a event when someone downloaded/viewed the attached?

  5. The Jhong Avatar
    The Jhong

    Hi Jitu,

    How do you create a trigger that prevents a user from either deleting or editing attachment also is it possible for you to lock down attachment ie Read Only after attaching documents?

    Thanks and more power !

  6. vkm Avatar

    Hi Jitendra, How to write after insert trigger for adding custom fields on a child object of account by using attachments which will be create multiple records in that object.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading