+ Start a Discussion

File Attachments and Triggers

Hello All,

Newbie looking for some help.
I have two objects Object1 and Object2.
Both of them are Custom Objects.
Object1 is Master and Object2 is Child.

Object1 has a checkbox called "File Present".
This checkbox gets set to TRUE only when Object2 has an attachment.

In order to solve this, I decided to do the following

a)Create a before update trigger on Object2

b) Whenever there is a file added to the Object2 this before update trigger will fire and will set the checkbox on the parent Object1 to true

My code

trigger SetCheckBox on Object2__c (before update) {
    List<Attachment> a = [SELECT Id,Name FROM Attachment WHERE ParentId = :Trigger.new[0].id];
    if(a.size() > 0)
       List<Object2__c> l = [SELECT Id,Name,Object1__c FROM Object2__c WHERE Id = :Trigger.new[0].id];
       Object1__c s = [SELECT Id FROM Object1__c WHERE Id = :l[0].Object1__c];
       s.File_Present__c = true;
       update s;


But the checkbox "File_Present__c" is not getting set whenever I add a file to the Object2.
Can someone help ?

PS :
Also I dont want to use WFR for this problem because I need to clear out the checkbox whenever there is an attached file deletion on object 2 and this cannot be achieved by WFR so I thought I might as well solve both the problems via triggers only
Attachment whose upload you are trying to capture is another object - when you create a trigger on object2 - it takes care of only updates being made on the fields on object2 - not the attachment - for the attachment to be taken into consideration- you wil have to write a code on attachment object which AFAIK is currently not possible. 
Hi Sonam,

Thanks for the general direction.
Just adding to it what you have given, it is quite possible to add trigger on Attachment object :)
I am able to add to do that.
The only tricky part is we need to get the ParentId from the Trigger.New collection and cross reference against the object where in this attachment is getting added.