+ Start a Discussion
SFTerrSFTerr 

Tick checkbox field when attachment is added (trigger)

I'm new to APEX and I would be grateful if someone could help me with below trigger.
when there is new attachment added to custom Project_Edition__c object I need Venue_Contract_Attached__c to be ticked automatically (and unticked when attachment is deleted)
 
trigger CountAttachments on Project_Edition__c (before insert, before delete)
{
if(trigger.isinsert){
List<Project_Edition__c> co = [select id from Project_Edition__c where id =: Trigger.New[0].ParentId];
If(co.size()>0)         
{             
co[0].Venue_Contract_Attached__c = True;             
update co;         
}
}


if(trigger.isdelete){

List<Project_Edition__c> co = [select id from Project_Edition__c where id =: Trigger.old[0].ParentId];         
If(co.size()>0)         
{             
co[0].Venue_Contract_Attached__c = false;             
update co;         
}
}
}

I'm getting all sorts of errors, last one being "Error: Compile Error: Invalid field ParentId for SObject Project_Edition__c at line 4 column 93"

 
Best Answer chosen by SFTerr
StephenKennyStephenKenny

Hi there,

You are on the right track, however, the trigger needs to be on the Attachment object, not the Project_Edition__c object. The attachment will have a parentId so you will be able to use this to query the Project Edition records you wish to update.

Keep in mind that the attachment trigger will fire against all attachmnets - for example, if you load an attachment against an Account. So you will have to implement some logic to disregard those assuming you are only interested in updating Project Edition records.

Have a go at the new trigger and if you need any help just ask.

Please remember to mark this thread as solved with the answer that best helps you.

Regards
Stephen 

All Answers

StephenKennyStephenKenny

Hi there,

You are on the right track, however, the trigger needs to be on the Attachment object, not the Project_Edition__c object. The attachment will have a parentId so you will be able to use this to query the Project Edition records you wish to update.

Keep in mind that the attachment trigger will fire against all attachmnets - for example, if you load an attachment against an Account. So you will have to implement some logic to disregard those assuming you are only interested in updating Project Edition records.

Have a go at the new trigger and if you need any help just ask.

Please remember to mark this thread as solved with the answer that best helps you.

Regards
Stephen 
This was selected as the best answer
Bhanu MaheshBhanu Mahesh
Hi,

Standard Salesforce functionality does not allow any modification to Note and Attachment object.

Best Approach to create Attachment trigger can be: Create trigger from Developer console.

Need to click New: Apex Trigger: Then you can select SObject as "Attachment" form dropdown.

A trigger can be written on Attachment using Force.com IDE or ANT.

Follow the below links for reference

https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US (https://help.salesforce.com/HTViewSolution?id=000181538&language=en_US)

http://www.sfdcpoint.com/salesforce/trigger-on-attachment-in-salesforce/

Regards,
Bhanu Mahesh
SFTerrSFTerr
Thank you both for your help. Below trigger is working fine now:
trigger CountAttachment on Attachment (before insert, before delete)
{
if(trigger.isinsert){
List<Project_Edition__c> co = [select id from Project_Edition__c where id =: Trigger.New[0].ParentId];
If(co.size()>0)        
{            
co[0].Venue_Contract_Attached__c = True;            
update co;        
}
}


if(trigger.isdelete){

List<Project_Edition__c> co = [select id from Project_Edition__c where id =: Trigger.old[0].ParentId];        
If(co.size()>0)        
{            
co[0].Venue_Contract_Attached__c = false;            
update co;        
}
}
}

Plus I've added test class:
 
@isTest
public class CountAttachmentTest
{
    static testMethod void attTriggerTest1()
    {
        test.startTest();      
        Project_Edition__c pe = new Project_Edition__c(Name = 'Test Project Edition', Status__c ='Active');
        insert pe;   
        Attachment att = new Attachment(Name='textAtt', ParentId = pe.Id, body = Blob.valueOf('Some Text'), Description='TestAttachment');
        insert att;
        delete att;        
        test.stopTest();
    }
}

so again, thanks a lot!

 
Christophe Lereverend 4Christophe Lereverend 4
Does actually work on lightning page?