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
Samir MeshramSamir Meshram 

How to auto closed/won stage after attaching a file in attachments

Hello Evryone, 
We have requirement like, whenever we attche a file or document in particular opportunity, that opprtunity stage should be automatically updated to closed/won.
Please help me on this if anybody has any idea about it.

Thanks
Suraj Tripathi 47Suraj Tripathi 47

Hi Samir,

We can write a trigger for this.Whenever first time attachment is add to that Opportunity then it become Closed won

is this work for you??

Samir MeshramSamir Meshram
Hi Suraj,
Thank you for your response, yes it will work, but i am not that good at writting trigger or code, can you please share a snippet ot the trigger?

Thanks
Suraj Tripathi 47Suraj Tripathi 47
trigger OpportunityTrigger on Opportunity (after insert,After Update) {
    
    if(trigger.isAfter && trigger.isInsert){
	
	List<Opportunity> oppList = [select id, StageName from Opportunity where id =: Trigger.New[0].ParentId];          
If(oppList.size()>0)          
{              
oppList[0].StageName = 'Closed Won';              
update oppList;          
} 
}

Something like this
mukesh guptamukesh gupta
Hi Samir,

Please follow below code:- 

ContentDocumentLinkTrigger
trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
 Set<Id> oppIds = new Set<Id>();
    if(trigger.isAfter) {
        if(trigger.isInsert) {
          for(ContentDocumentLink cdl : Trigger.New){
              string oppId  = cdl.LinkedEntityId;
            if(oppId.subString(0,3) == '006'){
             oppIds.add(oppId);

        }

       }
 list<Opportunity> oppList = [select id,stagename from opportunity where id IN:oppIds ];
            for(Opportunity opp : oppList){
                opp.stagename = 'Closed Won';
            }      
            update oppList;    
            
        }
    }
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh



 
Samir MeshramSamir Meshram
Hi mukesh,
i have this trigger

trigger FilesTrigger on ContentDocumentLink (after insert, after update) {
     List<Opportunity> oppList = new List<Opportunity>();
    Set<Id> oppIds = new Set<Id>();
    for(ContentDocumentLink cdl : trigger.new) {
        if(cdl.linkedentityid.getSobjectType() == Opportunity.SObjectType){
            oppIds.add(cdl.linkedentityid);
        }
    }
    oppList = [select id, StageName from Opportunity where id in :oppIds];
    
    if(oppList!=null && oppList.size()>0 ){
        for(Opportunity op : oppList){
            
            op.StageName = 'Closed Won';
        }
        update oppList;
    } 

}

this was working fine .

But now one more thing is added in scenario, now there is one checkbox named 'Signed', now i want after attching a file only  if that checkbox is clicked to true  then stage should move to closed/won.
mukesh guptamukesh gupta
Hi Samir,

Check box on which object?
this check box should be auto check by code or manually

Please confirm
Samir MeshramSamir Meshram
Checkbox is on opportunity object, and it will be checked manually.
mukesh guptamukesh gupta
Hi Samir,

Please use below code
trigger FilesTrigger on ContentDocumentLink (after insert, after update) {
     List<Opportunity> oppList = new List<Opportunity>();
    Set<Id> oppIds = new Set<Id>();
    for(ContentDocumentLink cdl : trigger.new) {
        if(cdl.linkedentityid.getSobjectType() == Opportunity.SObjectType){
            oppIds.add(cdl.linkedentityid);
        }
    }
    oppList = [select id, StageName from Opportunity where id in :oppIds];
    
    if(oppList!=null && oppList.size()>0 ){
        for(Opportunity op : oppList){
           if(op.Signed__c ==  true){
             op.StageName = 'Closed Won';
          }
        }
        update oppList;
    } 

}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Samir MeshramSamir Meshram
Hi mukesh,
I used above code, but it is not working,O you  think the trigger should be on opprtunity.?