Creating Trigger on Attachment in Salesforce

Author posted by Jitendra on Posted on under category Categories Apex, Salesforce and tagged as Tags , , with 8 Comments on Creating Trigger on Attachment in Salesforce

Example and tutorial on creating the trigger for attachment in salesforce which will not allow to upload the file in opportunity if file contains some predefined text

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 att:Trigger.new)
	{
		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 thoughts on “Creating Trigger on Attachment in Salesforce”

  1. 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. 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. hai sir,
    i am having a doubt on triggers.how 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. 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.

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

  5. 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. 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.