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 

oppo auto closed won after attaching a file

Hello evryone,
I have requirement like, in opportunity you cant not close a oppo to closed won untill you attach a file in attachment, after that there is a one field Signed__c you have to check that field, as soon as your file is attached and your Signed__c is checked, the opportunity should attomatically moved to closed won, i have written this trigger, but it is not working,

trigger AttachmentValidate on Opportunity (before update,before Insert, after insert, after update) {
    
    public boolean noAttachment = false;
    for(Opportunity o:Trigger.New)    
        
    {
        //ContentDocument a = new ContentDocument();
        if(o.StageName == 'Closed won')
        {
            try{
                ContentDocumentLink CDL = [SELECT ContentDocumentId,Id,IsDeleted,LinkedEntityId FROM ContentDocumentLink where LinkedEntityId =: o.Id];
                  system.debug('cdl :'+CDL);
                //  system.debug('count :'+CDL);
            }
            catch(exception e)
            {
                noAttachment = true;
            }
            if (noAttachment == true){
                o.addError('Add a MSA/SOW file in Notes and Attachment before you close the Opportunity as  Closed Won');
                  system.debug('noAttachment :'+noAttachment);
            }
            else if(o.Signed__c == false){
           o.addError('Please Check the Signed MSA/SOW attached checkbox');
                system.debug('o.Signed__c :'+o.Signed__c);
            }
            else{
                system.debug('hi');
            }
           
        }
    }
}

Please help me out on this, thanks
Best Answer chosen by Samir Meshram
Maharajan CMaharajan C
Hi Samir,

Do you have any trigger to update the Signed Checkbox as true when you are uploading the file if yes in the same trigger you can add the stage update also like below code.
 
trigger DocumentLinkTrigger on ContentDocumentLink (after insert) {
    set<id> contentDocumentidset=new set<id>();
    List<Opportunity> oppList = new List<opportunity>();
    set<Id> oppIds = new Set<Id>();
    for(ContentDocumentLink cdl:Trigger.New){
        if(cdl.LinkedEntityId!=null && string.valueOf(cdl.LinkedEntityId).startswithIgnorecase('006')){
            oppIds.add(cdl.LinkedEntityId);
        }
    }
    
    for(Opportunity o : [Select Id,Name,StageName,Signed__c from Opportunity where Id IN: oppIds]){
        Boolean changeNeed = false;
        if(o.StageName != 'Closed Won'){
            o.StageName = 'Closed Won';
            changeNeed = true;
        }
        if(!o.Signed__c){
            o.Signed__c = true;
            changeNeed = true;
        }
        if(changeNeed)
            oppList.add(o);
    }
    
    if(!oppList.IsEmpty()){
        update oppList;
    }
}

Otherwise check is there any automation existing for Stage Update. If there is no update logic for stage update simpy you can create Workflow based on the Signed Checkbox field. Sample workflow is below link.
https://trailblazers.salesforce.com/answers?id=9063A000000iXq6QAE

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Samir,

Please try the below code:
 
trigger AttachmentValidate on Opportunity (before update) {
    set<Id> contentDocLinkSet = new set<Id>();
    set<Id> oppIds = new set<Id>();    
    system.debug(' ****** ');
    for(Opportunity o:Trigger.New)    
    {
        if(o.StageName == 'Closed won')
        {
            oppIds.add(o.Id);
        }
    }
    
    if(!oppIds.isEmpty()){
        system.debug(' ****** ');
        for(ContentDocumentLink cdl  : [SELECT LinkedEntityId  FROM ContentDocumentLink WHERE LinkedEntityId IN: oppIds]){
            system.debug(' ****** ');
            contentDocLinkSet.add(cdl.LinkedEntityId);
        }
    }
    
    for(Opportunity o:Trigger.New)    
    {
        system.debug(' ****** ');
        if(o.StageName == 'Closed won')
        {
            if(!contentDocLinkSet.contains(o.Id)){
                o.addError('Add a MSA/SOW file in Notes and Attachment before you close the Opportunity as  Closed Won');
                system.debug('noAttachment');
            }
            else if(o.Signed__c == false){
                o.addError('Please Check the Signed MSA/SOW attached checkbox');
                system.debug('o.Signed__c :'+o.Signed__c);
            }
            else{
                system.debug('hi');
            }
        }
    }
}

Thanks,
Maharajan.C
​​​​​​​
Samir MeshramSamir Meshram
Hi maharajan
Thank you for the response, unfortunatelly it is not working.
Samir MeshramSamir Meshram
Thing is once the file is attached and checbpx is checked as true, the oppo stage should move automatically to Closed Won, that is not happening other valdiations are working, just the satge is not updtaing.
Maharajan CMaharajan C
Hi Samir,

Do you have any trigger to update the Signed Checkbox as true when you are uploading the file if yes in the same trigger you can add the stage update also like below code.
 
trigger DocumentLinkTrigger on ContentDocumentLink (after insert) {
    set<id> contentDocumentidset=new set<id>();
    List<Opportunity> oppList = new List<opportunity>();
    set<Id> oppIds = new Set<Id>();
    for(ContentDocumentLink cdl:Trigger.New){
        if(cdl.LinkedEntityId!=null && string.valueOf(cdl.LinkedEntityId).startswithIgnorecase('006')){
            oppIds.add(cdl.LinkedEntityId);
        }
    }
    
    for(Opportunity o : [Select Id,Name,StageName,Signed__c from Opportunity where Id IN: oppIds]){
        Boolean changeNeed = false;
        if(o.StageName != 'Closed Won'){
            o.StageName = 'Closed Won';
            changeNeed = true;
        }
        if(!o.Signed__c){
            o.Signed__c = true;
            changeNeed = true;
        }
        if(changeNeed)
            oppList.add(o);
    }
    
    if(!oppList.IsEmpty()){
        update oppList;
    }
}

Otherwise check is there any automation existing for Stage Update. If there is no update logic for stage update simpy you can create Workflow based on the Signed Checkbox field. Sample workflow is below link.
https://trailblazers.salesforce.com/answers?id=9063A000000iXq6QAE

Thanks,
Maharajan.C
This was selected as the best answer
Samir MeshramSamir Meshram
Thank you Maharajan.