function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
David Kerr 5David 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.
Rahul.MishraRahul.Mishra
Hi ,

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.
 trigger TiggerName on attachment (after insert) {
     
    Set<Id> setOpportunityId = new Set<Id>(); 
    List <Opportunity> lstParentToUpdate = new List<Opportunity>();
    
     for (attachment atchmnt : trigger.new) {
         
         String str = atchmnt.ParentId;
         if (str.startsWith('006')) {
             setOpportunityId.add(atchmnt.ParentId);
         }
     }
     
   if(!setOpportunityId.isEMpty()) {
    for (Id oppId : setOpportunityId)  {
        lstParentToUpdate.add(new Opportunity(id = oppId, Attachment__c  = True));
    }
  }
  
  if(!lstParentToUpdate.isEMpty())
   update lstParentToUpdate;
}

Regards,
Rahul

Mark my answer as best if it helps you.
David Kerr 5David Kerr 5
Sorry for the confusion I'm relatively new to APEX development and trying to understand the code.  The custom field Attachment__c is in the Opportunity object.       User-added image

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.  
David Kerr 5David Kerr 5
As of now, I can attach the file and click save.  However, the checkbox field 'Attachment' won't auto populate to TRUE.  Once I change something in the opportunity and save again, the checkbox will populate correctly
Rahul.MishraRahul.Mishra
Hi David,

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
Ajay K DubediAjay K Dubedi
Hi David,

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

 
David Kerr 5David Kerr 5

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