You need to sign in to do that
Don't have an account?
David Kerr 5
trigger to auto-check field on opportunity object once a file is attached
Hi, I'm looking to require users to upload a file to an open opportunity on the Opportunity object before marking the stage as Closed/Won.
I have the following code so far (src - https://developer.salesforce.com/forums/?id=906F00000008wHEIAY):
trigger OpptyTest on Opportunity (before insert, before update) {
Opportunity[] opp=trigger.new;
for(Opportunity oppy : [SELECT Id,
(SELECT Id, Name, ContentType FROM Attachments)
FROM Opportunity where id=:opp[0].Id]){
Attachment[] attc = oppy.Attachments;
if(!attc.isEmpty()){
opp[0].Attachment__c = true;
}
if(attc.isEmpty()){
opp[0].Attachment__c = false;
}
}}
The issue as stated in the source post is that you need to edit the opportunity before the checkbox field 'Attachment' is marked True. What would be the best solution to having the 'Attachment' field automatically check TRUE once the file is attached?
Once this is solved, the user will be able to mark the opportunity as Closed/Won through a validation rule that has been added.
I have the following code so far (src - https://developer.salesforce.com/forums/?id=906F00000008wHEIAY):
trigger OpptyTest on Opportunity (before insert, before update) {
Opportunity[] opp=trigger.new;
for(Opportunity oppy : [SELECT Id,
(SELECT Id, Name, ContentType FROM Attachments)
FROM Opportunity where id=:opp[0].Id]){
Attachment[] attc = oppy.Attachments;
if(!attc.isEmpty()){
opp[0].Attachment__c = true;
}
if(attc.isEmpty()){
opp[0].Attachment__c = false;
}
}}
The issue as stated in the source post is that you need to edit the opportunity before the checkbox field 'Attachment' is marked True. What would be the best solution to having the 'Attachment' field automatically check TRUE once the file is attached?
Once this is solved, the user will be able to mark the opportunity as Closed/Won through a validation rule that has been added.
You can write the trigger on Attachment it self, when the attachment parent is Opportunity then collect those parent Opportunitues and update them, below is the sample code for insert case.
Regards,
Rahul
Mark my answer as best if it helps you.
I don't think I was clear enough in my initial post, from what I understand I don't think you can write a trigger on that Attachment custom field.
We can write the code on attachment (I have already given), you copy paste my code and let me know if that works for you.
Thanks,
Rahul
Please run this code.
trigger OppAttachmentTrigger1 on Attachment (after insert,after update,before delete) {
if(Trigger.isAfter && Trigger.isInsert ||Trigger.isAfter && Trigger.isUpdate){
Set<Id> IdCollect = new Set<Id>();
for(Attachment att:Trigger.New){
if(att.ParentId!=null){
IdCollect.add(att.ParentId);
}
}
List<Opportunity> oppList = new List<Opportunity>();
for(Opportunity oppy : [SELECT Id,Attachment__c FROM Opportunity where id=:IdCollect]){
for(Attachment at:Trigger.New){
if(at.ParentId!=null){
oppy.Attachment__c = true;
oppList.add(oppy);
}
}
}
update oppList;
}
// After delete attachment///
if(Trigger.isBefore && Trigger.isDelete){
Set<Id> IdCollect = new Set<Id>();
for(Attachment att:Trigger.Old){
if(att.ParentId!=null){
IdCollect.add(att.ParentId);
}
}
List<Opportunity> oppList1 = new List<Opportunity>();
for(Opportunity oppy : [SELECT Id,Attachment__c FROM Opportunity where id=:IdCollect]){
oppy.Attachment__c = false;
oppList1.add(oppy);
}
update oppList1;
}
}
Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.
Thank You
Ajay Dubedi
Hi Ajay,
Thanks for the assistance here. Where exactly would I be able to run a trigger on the custom field "Attachment__c"? I thought you can only write a trigger on the objects.
There is no Attachment object so I can't run this trigger 'on Attachment'. This should be running on the Opportunity object. Additionally, I'm not sure what the ParentId is referring to. Sorry for any confusion