Trigger on Salesforce Attachments

Well I know this is nothing new, I just wanted to jot it down quickly!

Force.com supports triggers @Attachments but as of now you can't create them from standard UI, but hey wait we can create them with the NEW Developer Console. Yes the Developer console(v29.0) is more like Eclipse now and allows you to create a triggers on Attachments.

So here is how you can create a trigger @ Attachment



  • Click at your Name(left top corner) and Select "Developer Console"

    Developer Console Selection

  • Now when you are inside Developer Console . Go to File >>  New >> Apex Trigger

    New Apex Trigger
  • Select "Attachment" in sObject and and give a good Name to your trigger.  

Thats it ! you are ready to go!.

Sample Attachment Trigger


Description : Say you want to create a trigger on Attachment which will update a custom field "Last_Attachment_Added_Date__c" on "Account" with the date when the Last Attachment was added.

Prerequisite : "Last_Attachment_Added_Date__c" custom  Date/Time field on Account Standard Object.


 trigger AttachmentTrigger on Attachment (before insert) {  
   Boolean isAccountAttachment = FALSE;    
      List<Account> accounts = new List<Account>();  
        Set<Id> accIds = new Set<Id>();  
        for(Attachment att : trigger.New){  
             /*Check if uploaded attachment is related to Account Attachment and same account is already added*/  
             if(att.ParentId.getSobjectType() == Account.SobjectType && (!accIds.contains(att.ParentId)){  
                  //prepare a account object for update  
                  accounts.add(  
                                      new Account(  
                                                          Id=:att.ParentId,  
                                                          Last_Attachment_Added_Date__c = System.today()  
                                                        )  
                                    );  
                     //add the accountid in set to eliminate dupe updates                                     
                  accIds.add(att.ParentId);  
             }  
        }  
        //finally update accounts  
        update accounts;  
 }  

The trigger is pretty simple and the trick is in detecting on which object the attachment is being added.

In the above code we are comparing "SobjectType" of the ParentId(for attachment which is added to account SobjectType will be same as of Account Object) and the "Account" to check whether the attachment is added to account.

By using the above code as base you can do many more things like rolling up number of Attachments to its parent object, Some custom validation using trigger on attachment etc.


18 comments:

  1. Your information is really nice and very informative. Thanks for sharing this great article.

    Salesforce Training

    ReplyDelete
  2. Currently, Android has a market share of 57% in the worldwide smart phone market. Android programming language is backed and developed by Google and Open Handset Alliance (a consortium of 84 firms)
    Android Training in Chennai

    ReplyDelete
  3. DOT NET framework has various set of libraries. These libraries will support various languages such as VB.NET, C#.NET, ASP.NET etc. These libraries are the pillars for the .NET framework. Architects of .NET framework have faced so many problems while coding in the C, C++
    Dot Net Training in Chennai

    ReplyDelete
  4. Nice blog, here I had an opportunity to learn something new in my interested domain. I have an expectation about your future post so please keep updates.

    Salesforce training in Chennai

    ReplyDelete
  5. Nowadays, most of the businesses rely on cloud based CRM tool to power their business process. They want to access the business from anywhere and anytime. In such scenarios, salesforce CRM will ensure massive advantage to the business owners. Salesforce Training | Salesforce Training in Chennai

    ReplyDelete
  6. Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.If anyone wants to get Dot Net Training in Chennai reach FITA, rated as No.1 Dot Net Training Institute in Chennai.

    ReplyDelete
  7. Your blog is really awesome and I got some useful information from your blog. This is really useful for me. Thanks for sharing such a informative blog. Keep posting.

    Regards..
    Cloud Computing Course in Chennai

    ReplyDelete
  8. The information you posted here is useful to make my career better. Thanks for sharing such a informative post. keep updates...

    Regards..
    Salesforce Developer Training in Chennai

    ReplyDelete
  9. Thanks for sharing such a great information..Its really nice and informative.
    Best SharePoint Training Institute in Chennai

    ReplyDelete
  10. Excellent post!!! In this competitive market, customer relationship management plays a significant role in determining a business success. That too, cloud based CRM product offer more flexibility to business owners to main strong relationship with the consumers. Salesforce Training Institutes in Chennai | Salesforce Training in Chennai

    ReplyDelete
  11. Thanks for sharing this excellent post. Its really very informative and interesting. Keep update your blog. For a best Android training in Chennai please refer this site.
    Regards....
    Android Training in Chennai

    ReplyDelete
  12. Well post in recent day’s customer relationship play vital role to get good platform in business industry, Salesforce crm tool helps you to maintain your customer relationship enhancement.
    Regards,
    Salesforce training in Chennai | Salesforce course in Chennai | Salesforce training institute in Chennai

    ReplyDelete
  13. There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this.

    ReplyDelete
  14. You have stated definite points about the technology that is discussed above. The content published here derives a valuable inspiration to technology geeks like me. Moreover you are running a great blog. Many thanks for sharing this in here.

    Salesforce Training
    Salesforce training in chennai
    Salesforce training institutes in chennai

    ReplyDelete
  15. This is really helpful! Any chance you have the test class written somewhere already?

    ReplyDelete
  16. Nice blog on Hadoop Technology!!! I agree with your points, Big Data Hadoop technology is ruling the whole
    organization to handle data in efficient manner. HDFS and Map reduce helps the developer in different ways to
    manage data.

    Regards:

    Best hadoop training institute in chennai |
    Big Data Hadoop Training in Chennai

    ReplyDelete
  17. Nice blog on Selenium Testing!!! I agree with your points, Testing will help whole

    organization to handle the projects in efficient manner.

    Selenium Training in chennai |
    Selenium training institute in Chennai 

    ReplyDelete