You need to sign in to do that
Don't have an account?
SFTerr
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)
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"
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"
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
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
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
Plus I've added test class:
so again, thanks a lot!