-1

On an Opportunity, is there a way to make "Notes & Attachments" Required?

If so, please explain how as if I were a 11 year old ^_^

2
  • what is your scenario? Notes and attachment is a related list of an opportunity. Explain more when you want to make it required? – sanket kumar Sep 11 '18 at 11:28
  • We want the user to be Required to attach a "Termination Document" before being allowed to move to the next stage: – Man Sep 11 '18 at 11:30
1

Opportunity is a different Object and Note and Attachment is another object.

Note and Attachment has a Lookup to Opportunity(Polymorphic ParentID field).

As you want people to stop changing the stage of opportunity if there doesnt exist an attachment of a proper type, You have to use Apex Trigger on Opportunity Object.

Trigger OpportunityTrigger on Opportunity(before update){
    
    List<Id> changedOppStatusId = new List<Id>();
    for(Opportunity opp:Trigger.new){
        if(opp.Stage=='Closed' && Trigger.OldMap.get(opp.Id).Stage!=opp.Stage){
            //Stage has changed, Now we will check if it has an attchment. 
            changedOppStatusId.add(opp.id);
        }
    }
    if(!changedOppStatusId.isEmpt()){
        List<Attachment> attachmentList =[Select id,ParentID from Attachment where ParentId in :changedOppStatusId and Name='Termination Document'];
        Set<Id> oppIDThatContainsFile = new Set<Id>();
        
        for(Attachment att : attachmentList){
            
            oppIDThatContainsFile.add(att.ParentID);
        }
        
        for(Id changedOppId : changedOppStatusId){
            if(!oppIDThatContainsFile.contains(changedOppId)){
                //This means there is not attahment on that Opportunity. So we will add validation erro.
                Trigger.newMap.get(changedOppId).addError('You cant change Stage without adding an attachment of Name Termination Document);;
            }
        }
    }

}
0

@Man, You need to go for a trigger in this case before the update.

What you will do is check for the number of attachment related to that record. If it is greater than one and name of the attachment is 'Termination Document' then only the record can be updated otherwise you can throw an error using adderror method with your custom error message.

You need to write the trigger on Opportunity object.

Here is the code:-

    trigger attachmentopp on Opportunity (before update) {
    for(Opportunity opp:[select id,StageName,(select id, Name from Attachments) from Opportunity where id = :(trigger.newmap).keyset()]){
        if(trigger.oldmap.get(opp.id).StageName!=trigger.newmap.get(opp.id).StageName){
            if(opp.Attachments.size()<=0){
                (trigger.newmap).get(opp.id).adderror('You cannot change the status');
            }
        }
    }
}

I am checking for the records those are getting updates and have set condition weather earlier stage and current stage is different and it does not have any attachments then it will throw error.

Hope it helps You.

3
  • Hmm I don't know how to code this... Can you make something so that I can copy/paste it into the Apex Trigger? All I would need is, "What you will do is check for the number of attachment related to that record. If it is greater than one then only the record can be updated otherwise you can throw an error using adderror method with your custom error message." The doc name doesn't matter. – Man Sep 11 '18 at 11:40
  • @Man I have updated my answer with an exact code snippet which you want. – sanket kumar Sep 11 '18 at 11:49
  • @Man Welcome:), does it solves your problem? If it is, you can mark it as our answer. – sanket kumar Sep 12 '18 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.