{"id":2797,"date":"2012-03-28T22:27:09","date_gmt":"2012-03-28T16:57:09","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2797"},"modified":"2012-03-28T22:27:09","modified_gmt":"2012-03-28T16:57:09","slug":"creating-trigger-on-attachment-in-salesforce","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/creating-trigger-on-attachment-in-salesforce\/","title":{"rendered":"Creating Trigger on Attachment in Salesforce"},"content":{"rendered":"<p style=\"text-align: justify;\">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.<\/p>\n<p>We cannot create trigger on Attachment from the standard salesforce UI however we will have to use the eclipse.<\/p>\n<figure id=\"attachment_2798\" aria-describedby=\"caption-attachment-2798\" style=\"width: 533px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Before-insert-Trigger-on-Attachment-Salesforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2798\" title=\"Before insert Trigger on Attachment using eclipse in Salesforce\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Before-insert-Trigger-on-Attachment-Salesforce.png?resize=533%2C510&#038;ssl=1\" alt=\"Before insert Trigger on Attachment using eclipse in Salesforce\" width=\"533\" height=\"510\" \/><\/a><figcaption id=\"caption-attachment-2798\" class=\"wp-caption-text\">Before insert Trigger on Attachment using eclipse in Salesforce<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">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 &#8220;particular object only&#8221; must be handled carefully.<!--more--><\/p>\n<p style=\"text-align: justify;\">In this article i will create the trigger which will check for the parent object &#8220;Opportunity&#8221;. If the object is opportunity then it will check let say for the file name cannot contain word &#8220;shivasoft&#8221;.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\ntrigger CheckAttachmentName on Attachment (before insert) {\n\tfor (Attachment att:Trigger.new)\n\t{\n\t\tString parentObjId = att.ParentId;\n\t\t\/\/006 is the starting sting in ID for all opportunities\n\t\tif(parentObjId.startsWith('006') &amp;&amp; att.Name.toLowerCase().contains('shivasoft'))\n\t\t{\n\t\t\t\tatt.addError('Cannot upload file with name containing - shivasoft');\n\t\t}\n\t}\n\n\t\/\/This method will return true if the id belongs to Opportunity\n\tprivate Boolean isOpportunityParent(String id)\n\t{\n\t\tfor(Opportunity opp : &#x5B;SELECT Id FROM Opportunity WHERE Id = :id])\n\t\t{\n\t\t\treturn true;\n\t\t}\n\t\treturn false;\n\t}\n}\n<\/pre>\n<p>In the above code, we are checking that the Parent id starts with &#8220;006&#8221; or not. As starting three characters in ID identifies the type of the object.<\/p>\n<p><strong>Alternate solution &#8211; Not recommended :<\/strong><br \/>\nThere is one method created named &#8220;isOpportunityParent&#8221; 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?<\/p>\n<figure id=\"attachment_2800\" aria-describedby=\"caption-attachment-2800\" style=\"width: 467px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Error-Message-on-Attachment-using-Trigger-in-Salesforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2800\" title=\"Error Message on Attachment using Trigger in Salesforce\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Error-Message-on-Attachment-using-Trigger-in-Salesforce.png?resize=467%2C356&#038;ssl=1\" alt=\"Error Message on Attachment using Trigger in Salesforce\" width=\"467\" height=\"356\" \/><\/a><figcaption id=\"caption-attachment-2800\" class=\"wp-caption-text\">Error Message on Attachment using Trigger in Salesforce<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,9],"tags":[337,331,203],"class_list":["post-2797","post","type-post","status-publish","format-standard","hentry","category-apex","category-salesforce","tag-apex","tag-salesforce","tag-trigger"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":5662,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/send-visualforce-as-an-email-attachment-from-apex-trigger-alternate-design\/","url_meta":{"origin":2797,"position":0},"title":"Send Visualforce as an email attachment from Apex Trigger &#8211; Alternate design","author":"Jitendra","date":"August 27, 2016","format":false,"excerpt":"Process Builder & InvocableMethod- Alternative to send Visualforce as an email attachment from Apex Trigger","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Send Visualforce as an email attachment from Apex Trigger","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/08\/Send-Visualforce-as-an-email-attachment-from-Apex-Trigger.jpg?fit=922%2C369&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3851,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex\/send-email-with-generated-pdf-as-attachment-from-trigger\/","url_meta":{"origin":2797,"position":1},"title":"Send Email with Generated PDF as attachment from Trigger &#8211; before Winter 16","author":"Jitendra","date":"April 16, 2014","format":false,"excerpt":"There may be scenario in Salesforce that you need to send a Visualforce page rendered as PDF as a part of Email Attachment. This will be very easy if you want to perform this using Controller or Extension class, we just have to call getContentAsPDF() method of PageReference class and\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1241,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-trigger-and-test-cases-6-of-6\/","url_meta":{"origin":2797,"position":2},"title":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and Test cases \u2013 6 of 6","author":"Jitendra","date":"October 11, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and test cases \u2013 6 of 6 tutorials series","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Creating Trigger in Salesforce using force.com IDE","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Creating-Trigger-in-Salesforce-using-force.com-IDE.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4136,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-spring-15-some-highlighted-features\/","url_meta":{"origin":2797,"position":3},"title":"Salesforce Spring 15 &#8211; some highlighted features","author":"Jitendra","date":"December 28, 2014","format":false,"excerpt":"Download PDF version of release notes from here. 1. Import Account \/ Contact from 16 different sources (previously generic csv was only option to import). 2. Salesforce Analytics Query language (SAQL) We already know about SOQL and SOSL. This is new Query language for Analytics cloud to enable ad hoc\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Case Macro - Spring 15","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2014\/12\/Case-Macro-Spring-15.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":4597,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/advance-apex-debugging-in-salesforce-and-best-practices-videos\/","url_meta":{"origin":2797,"position":4},"title":"Advance Apex debugging in Salesforce and best practices &#8211; Videos","author":"Jitendra","date":"June 27, 2015","format":false,"excerpt":"How to resolve apex debug log size limit issue, Difference between Eclipse and developer console, Interactive Apex Debugging, Keyboard shortcuts for Developer console and Other best practices","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Advance Apex debugging","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/06\/Salesforce-Advance-Apex-debugging.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2546,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/email-services-in-salesforce-with-simple-example\/","url_meta":{"origin":2797,"position":5},"title":"Email Services in Salesforce with simple example","author":"Jitendra","date":"January 11, 2012","format":false,"excerpt":"Complete tutorial of Email services in Salesforce with simple step by step example","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"How Email Services works in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/12\/Email-Services-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2797"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2797\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}